【问题标题】:How to implement drawing tool in iOS?如何在 iOS 中实现绘图工具?
【发布时间】:2016-09-19 04:49:12
【问题描述】:

我正在尝试实现一个手绘工具,用户可以通过它使用触摸开始和触摸移动方法在 PDF 上动态绘制多个形状,如矩形、日食、圆形等。 所以任何做过这种工具或知道如何完成它的人,请帮助我。

提前谢谢你。

【问题讨论】:

  • 这个问题对于 Stack Overflow 来说太宽泛了。但是,您可能希望从实现 UIGestureRecognizer 开始,以便捕获用户触摸屏幕的位置。
  • 1.使用 UIGestureRecognier 检测用户交互 2. 创建 UIBezierPath 保存点,然后通过覆盖 drawInRect 方法在视图上绘制它
  • 非常感谢您的建议。 Stack Overflow 是为开发人员提供实际帮助的唯一平台。这就是我问这个问题的原因。实际上我想实现一个类似于“FieldWire”应用程序的工具。所以如果你知道的话,你可以建议我一些教程链接,示例项目。 @Tibrogargan :)

标签: ios objective-c uibezierpath cashapelayer


【解决方案1】:

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

1) 在UIViewController 中声明两个全局变量BOOL mouseSwipedCGPoint 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();
}

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-14
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 2017-01-17
    • 2015-08-11
    • 1970-01-01
    相关资源
    最近更新 更多