0
我试图实现一个徒手绘制工具,通过该工具,用户可以使用触摸开始和触摸移动方法,在PDF上动态绘制多个形状,如矩形,日食,圆等。 那么任何人谁做了这种工具或有知识如何做到这一点,请帮助我。如何在iOS中实现绘图工具?
预先感谢您。
我试图实现一个徒手绘制工具,通过该工具,用户可以使用触摸开始和触摸移动方法,在PDF上动态绘制多个形状,如矩形,日食,圆等。 那么任何人谁做了这种工具或有知识如何做到这一点,请帮助我。如何在iOS中实现绘图工具?
预先感谢您。
这是一个非常宽泛的问题,需要在StackOverflow上解答。我仍然试图在这里给你一个开始。
1)在您的UIViewController
中声明两个全局变量BOOL mouseSwiped
,CGPoint lastPoint
。
2)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
}
3)
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
UIGraphicsBeginImageContext(yourSubview.frame.size);
[yourSubview drawInRect:CGRectMake(0, 0, yourSubview.frame.size.width, yourSubview.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
yourSubview = UIGraphicsGetImageFromCurrentImageContext();
[yourSubview setAlpha:1.0];
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
4)
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UIGraphicsBeginImageContext(yourSubview.frame.size);
[yourSubview drawInRect:CGRectMake(0, 0, yourSubview.frame.size.width, yourSubview) blendMode:kCGBlendModeNormal alpha:1.0];
yourSubview = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
希望这有助于
这个问题是远远堆栈溢出过于宽泛。但是,您可能需要首先实施[UIGestureRecognizer](https://developer.apple.com/reference/uikit/uigesturerecognizer)以捕获用户触摸屏幕的位置。 – Tibrogargan
1.使用UIGestureRecognier检测用户交互 2.创建UIBezierPath保存点,然后通过覆盖drawInRect方法在视图上绘制 – lee5783
非常感谢您的建议。堆栈溢出是给予实际帮助的开发人员的唯一平台。这就是我问这个问题的原因。其实我想实现一个类似于'FieldWire'app的工具。因此,如果你了解它,你可以给我建议一些教程链接,Sample Projects。 @Tibrogargan :) –