如何在iOS中实现绘图工具?

时间:2016-09-19 04:49:13

标签: ios objective-c uibezierpath cashapelayer

我试图实现一个徒手绘图工具,用户可以使用触摸开始和触摸移动方法在PDF上动态绘制多个形状,如矩形,日食,圆形等。   所以那些曾经做过这种工具或知道如何实现这一目标的人,请帮助我。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

这是StackOverflow要回答的一个非常广泛的问题。我仍然想在这里给你一个开始。

1)在BOOL mouseSwiped中声明两个全局变量CGPoint lastPointUIViewController

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();
}

希望这有帮助