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/49059630/

相关文章:

ios - 关于转义 URL 参数中的特殊字符的困惑

ios - 使用 class_getInstanceMethod - 在类层次结构中实现的方法在哪里?

objective-c - NSKeyedArchiver .plist 附加问题

ios - 为什么 MagnificationGesture 在 SwiftUI 中只更新一次状态?

ios - UITableViewCell 无法根据它包含的动态 CollectionView 调整高度

ios - 通过alloc方法初始化单例和普通对象有什么区别?

ios - 在 Tabbar 内容 ViewController 之前添加登录 View

ios - RegexKitLite iOS : Password validation - must include one number

ios - 无法显示uilabel的文字,自定义 View

iphone - iPhone 在 Z 轴上时的 UIImage 方向