iOS绘图功能到我的应用

时间:2017-11-14 23:16:46

标签: ios objective-c

我正在尝试向我的应用添加绘图功能。我有两个UIImageViews ...底部的一个包含一张图片,让我们说一张照片,第二个在它上面是我要画的。

- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
    UIView *tappedView = [gesture.view hitTest:[gesture locationInView:gesture.view] withEvent:nil];

    CGPoint currentPoint = [gesture locationInView:_paintOverlay];

    UIGraphicsBeginImageContext(_paintOverlay.frame.size);
    [_paintOverlay.image drawInRect:CGRectMake(0, 0, _paintOverlay.frame.size.width, _paintOverlay.frame.size.height)];
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 5, 5);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);

    CGContextStrokePath(UIGraphicsGetCurrentContext());
   _paintOverlay.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();


    NSLog(@"Touch event on view: %@",[tappedView class]);
}

这根本不起作用。我找不到任何教程可以帮助我解决这个问题,我发现的那个(我从中派生出这个代码)并不是那么容易理解。

2 个答案:

答案 0 :(得分:0)

您的代码(例如它)运行良好。我没有image所以我省略了它,但是通过提供brush的值等等,我发现点击视图会出现一行:

enter image description here

这是一种可怕的绘画方式(通过点击来绘画?只是从一个点开始制作线条?),但它确实会产生线条。

然而,我自然地配置了我的轻拍手势识别器和正确的视图。你不说你做了什么,谁知道呢?您是否将点击手势识别器挂钩到其动作处理程序?你有没有把它添加到视图中?你还记得打开视图的userInteractionEnabled吗?你点击了你的手势吗?很多事情都可能出错;你需要调试,看看发生了什么,并告诉我们更多相关信息。

答案 1 :(得分:-1)

尝试使用触摸方法而不是轻击手势,因为可以移动手指来绘制,触摸将很好地实现。 请参阅以下代码:https://www.raywenderlich.com/18840/how-to-make-a-simple-drawing-app-with-uikit

相关问题