Get Touch point in Arc drawn by CGContext

I drew so many arcs using the code below:

CGContextAddArc(context,
                        e.x,
                        e.y,
                        Distance/2,
                        M_PI+angle1,
                        angle1,
                        aClock); 
        CGContextStrokePath(context)

Now I want that when touching any arch, I would like to find out which arc was affected

How can i do this?

+5
source share
1 answer

You can do the following:

1. connect your arc to the path,

_path = CGPathCreateMutable();
CGPathAddArc(_path, NULL, e.x, e.y, Distance/2, M_PI + angle1, angle1, aClock);
CGContextAddPath(context, _path);
CGContextStrokePath(context);

2.Presence when touched Began: withEvent:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSSet *allTouches = [event allTouches];
    UITouch *touch = [allTouches anyObject];
    CGPoint point = [touch locationInView:[touch view]];

    if (CGPathContainsPoint(_path, NULL, point, NO)) {
        NSLog(@"point:(%f, %f), Touch arc.", point.x, point.y);
    }
    else {
        NSLog(@"point:(%f, %f), Touch other.", point.x, point.y);
    }
}

You will see "Touch arc". log when touching the arc.

0
source

All Articles