Overlapping Circular Buttons and Hit/Tap Testing

回眸只為那壹抹淺笑 提交于 2019-12-06 11:59:39

There is no "simple way" to limit the tap-detection area. You are perfectly correct: the button's tap-detection area is its rectangle, and that's all it knows about. Therefore, you can't use the button's simple tap-detection to do the work for you. You are going to have to detect the tap yourself, look at where it is, and use that knowledge plus your knowledge of where the drawings of the circles are to decide which section of the drum the user tapped in.

Look at UITapGestureRecognizer for a way to find out that the user tapped and where.

Look at UIBezierPath for a way to do hit detection with respect to a shape (e.g. a circle, answering the question, "is this point in this circle").

Your best bet might be to modify the built-in hit-testing, i.e. a UIButton or UIView subclass in which you override pointInside:withEvent: to return YES only if the point is within the interior circle, e.g.:

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    UIBezierPath* p = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
    return [p containsPoint:point];
}

That is about as "simple" as it's going to get, I'm afraid.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!