如何在iOS中实现绘图工具



我正在尝试实现一个徒手绘图工具,通过该工具,用户可以在PDF上动态地绘制几个形状,如矩形,eclipse,圆形等,使用触摸开始和触摸移动方法。所以有谁做过这种工具或有知识如何实现这一点,请帮助我。

提前感谢。

这是一个非常广泛的问题,要在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();
}

希望能有所帮助

最新更新