gpt4 book ai didi

iphone - 如何检测圆形 View 内的触摸

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:10:39 25 4
gpt4 key购买 nike

enter image description here

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

设置半径和检测触摸将没有任何用处,因为当多个 View 通过不同的 Controller 彼此重叠时,将难以管理。

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

最佳答案

创建 UIView 的自定义子类,例如 CircularView 并覆盖 pointInside:withEvent: 方法以忽略位于圆之外的点。这个子类的对象将是自包含的,您可以按照您想要的任何方式安排它。

要确定圆形区域是否包含点,您可以使用 Core Graphics 函数 CGPathContainsPointUIBezierPath 中的 containsPoint: 方法。这将要求您记住代表圆的CGPathRefUIBezierPath 对象。在此示例中,我假设您已使用 UIBezierPath 创建了一个圆形路径,并将其作为属性存储在 CircularView 类中。

@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

就是这样。

关于iphone - 如何检测圆形 View 内的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14078392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com