在CGContext绘制的弧线中获取触摸点

5
我使用以下代码画了许多弧形:
CGContextAddArc(context,
                        e.x,
                        e.y,
                        Distance/2,
                        M_PI+angle1,
                        angle1,
                        aClock); 
        CGContextStrokePath(context)

现在我想要当我点击任何一个弧形图形时,能够检测出哪个弧形被点击了。

我该如何实现这个功能?


使用旧的触摸方法(touchbegan,touchmoved,toucheended)来检测触摸发生在屏幕的哪个位置,然后查找附近的内容。 - Duck
1个回答

0
你可以这样做:
1.将你的弧添加到路径中,
_path = CGPathCreateMutable();
CGPathAddArc(_path, NULL, e.x, e.y, Distance/2, M_PI + angle1, angle1, aClock);
CGContextAddPath(context, _path);
CGContextStrokePath(context);

2.重写touchesBegan: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);
    }
}

当您触摸弧线时,将会看到“Touch arc.”日志。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接