如何检测圆形视图内的触摸 [英] how to detect touches inside a circular view

查看:73
本文介绍了如何检测圆形视图内的触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个圆形的UIView。我必须只检测紫色圆圈内的触摸。圆圈外的所有触摸,例如。必须忽略黑色方块和白色背景。



设置半径和检测触摸将没有任何用处,因为当多个视图在不同控制器的基础上彼此叠加时,将难以管理。 / p>

有什么办法,我可以这样做。请你给我一些建议。

解决方案

创建一个 UIView的自定义子类,比如说 CircularView 并覆盖 pointInside:withEvent:方法,忽略位于圆圈外的点。这个子类的一个对象将是自包含的,你可以按照你想要的任何方式进行排列。



要查明圆形区域是否包含一个点,你可以在 UIBezierPath CGPathContainsPoint containsPoint:方法C $ C>。这将要求您记住 CGPathRef 或代表该圆圈的 UIBezierPath 对象。在这个例子中,我假设您使用 UIBezierPath 创建了一个循环路径,并将其作为属性存储在 CircularView b。

  @interface CircularView:UIView 

//在适当的时候初始化
@propery(非原子,强)UIBezierPath * circularPath;

@end

@implementation CircularView

- (BOOL)pointInside:(CGPoint)指向withEvent:(UIEvent *)event {
return [circularPath containsPoint:point];
}

@end

就是这样。


I have a rounded UIView. I must detect the touches only inside the purple circle. All the touches outside the circle,e.g. black square and white background must be neglected.

Setting the radius and detecting the touches will not be any use, since when multiple views are on top of each other with different controllers, it will be difficult to manage.

Is there any way, I can do this. Please could you give me some suggestions do this.

解决方案

Create a custom subclass of UIView, say CircularView and override the pointInside:withEvent: method to ignore points that lie outside the circle. An object of this subclass will be self contained and you can arrange it in whatever manner you desire.

To find out if a circular region contains a point or not, you can make use of the Core Graphics function CGPathContainsPoint or the containsPoint: method in UIBezierPath. That will require you to remember the CGPathRef or the UIBezierPath object that represents the circle. In this example, I am assuming that you've created a circular path using UIBezierPath and it is stored as a property on the CircularView class.

@interface CircularView : UIView

// initialize this when appropriate
@propery (nonatomic, strong) UIBezierPath *circularPath;

@end

@implementation CircularView

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    return [circularPath containsPoint:point];
}

@end

And that's it.

这篇关于如何检测圆形视图内的触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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