ios - 检测贝塞尔曲线路径内的水龙头

标签 ios objective-c core-graphics uibezierpath

我有一个 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/

相关文章:

ios - 带有 Core Graphics 的内阴影

ios - isKindOfClass Bool if 语句记录 NO

ios - 为什么按钮未启用?

iphone - 如何在 Objective-C 中获取外部 ip

ios - 贝塞尔路径的透明区域不允许用户交互

iOS 改变一半图像的颜色

iphone - 通过 CMake 构建时如何指定通用 iOS 应用程序?

设备和 iOS 模拟器上的 iPhone map 平铺问题

ios - Swift 中的 MobileWiFi 框架回调

ios - 混合模式 kCGBlendModeMultiply 没有正确地将图像与颜色相乘