检测bezier路径内的水龙头 [英] Detecting tap inside a bezier path

查看:111
本文介绍了检测bezier路径内的水龙头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIView,它作为子视图添加到我的视图控制器。我在这个观点上画了一条更好的路径。我的drawRect实现在

I have a UIView which is added as a subview to my view controller. I have drawn a bezier path on that view. My drawRect implementation is below

- (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];
}

我希望在这个bezier路径中检测点击但不是在UIView和路径之外。例如,在这种情况下,如果我的触摸坐标是(10,10),则不应检测到它。我知道CGContextPathContainsPoint但是当触摸在路径内时它没有帮助。有没有办法检测贝塞尔路径内的触摸事件?

I want detect tap inside this bezier path but not the point which is inside the UIView and outside the path. For example in this case if my touch coordinate is (10, 10), it should not be detected. I know about CGContextPathContainsPoint but it does not help when touch is inside the path. Is there a way to detect touch events inside bezier path?

推荐答案

有一个函数 CGPathContainsPoint() 在你的情况下它可能会有用。

There is a function CGPathContainsPoint() it may be useful in your case.

如果你从superview获得手势点也要小心,你的测试坐标可能不正确。你有一个方法来转换或从特定视图的坐标系转换:

Also be careful if you get gesture point from superview, the coordinate may not be correct with your test. You have a method to convertPoint from or to a particular view's coordinate system:

- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view

这篇关于检测bezier路径内的水龙头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆