iOS paint features to my app

I'm trying to add a drawing feature to my app. I have two UIImageViews... the bottom one contains a picture, let's say a photograph, and the second one on top of it is the one I want to paint on.

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

    CGPoint currentPoint = [gesture locationInView:_paintOverlay];

    [_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);

   _paintOverlay.image = UIGraphicsGetImageFromCurrentImageContext();

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

This simply isn't working. I can't find any tutorials to help me with this, the one I found (where I derived this code from) wasn't so understandable.

2 answers

  • answered 2017-11-15 00:19 matt

    Your code, such as it is, works well enough. I didn't have an image so I omitted that, but by supplying values for brush and so forth I found that tapping on the view made a line appear:

    enter image description here

    It's a terrible way to paint (paint by tapping? only making lines coming from a single point???), but it does make lines.

    However, I naturally configured my tap gesture recognizer and views correctly. You don't say what you did, so who knows? Did you hook the tap gesture recognizer to its action handler? Did you add it to the view? Did you remember to turn on the view's userInteractionEnabled? Did you tap as your gesture? A lot of things can go wrong; you need to debug, see what's happening, and tell us more about it.

  • answered 2017-11-15 06:11 Van

    try with touches methods instead of tap gesture since one can move the fingers around to draw, touches will be good to implement. refer the sample at: