我有一个 UIView,它作为 subview 添加到我的 View Controller 中。我在该 View 上绘制了一条贝塞尔曲线路径。我的 drawRect 实现如下
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
UIBezierPath *bpath = [UIBezierPath bezierPath];
[bpath moveToPoint:CGPointMake(50, 50)];
[bpath addLineToPoint:CGPointMake(100, 50)];
[bpath addLineToPoint:CGPointMake(100, 100)];
[bpath addLineToPoint:CGPointMake(50, 100)];
[bpath closePath];
CGContextAddPath(context, bpath.CGPath);
CGContextSetStrokeColorWithColor(context,[UIColor blackColor].CGColor);
CGContextSetLineWidth(context, 2.5);
CGContextStrokePath(context);
UIColor *fillColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.5 alpha:0.7];
[fillColor setFill];
[bpath fill];
}
我想检测这条贝塞尔曲线路径内的点击,而不是 UIView 内和路径外的点。例如,在这种情况下,如果我的触摸坐标是 (10, 10),则不应检测到它。我知道 CGContextPathContainsPoint 但是当触摸在路径内时它没有帮助。有没有办法检测贝塞尔曲线路径内的触摸事件?
最佳答案
有一个函数 CGPathContainsPoint()
它可能对您有用。
如果您从 superview 获取手势点,也要小心,坐标可能与您的测试不正确。您有一个方法可以将 convertPoint 从特定 View 的坐标系转换为或转换为特定 View 的坐标系:
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view
关于ios - 检测贝塞尔曲线路径内的水龙头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22691891/