是否可以处理 UIButton 发生的触摸,然后将其传递给下一个响应者?
Best Answer-推荐答案 strong>
编辑
一个有效的方法也是覆盖 UIResponder 的 touchesEnded:withEvent: 方法
- (void)touchesEndedNSSet *)touches withEventUIEvent *)event {
// handle touch
[super touchesEnded:touches withEvent:event];
}
来自 documentation :
The default implementation of this method does nothing. However immediate UIKit subclasses of UIResponder , particularly UIView , forward the message up the responder chain. To forward the message to the next responder, send the message to super (the superclass implementation); do not send the message directly to the next responder.
原答案
为了确定 View 层次结构中的哪个 UIView 应该接收触摸,使用方法 -[UIView hitTest:withEvent:] 。根据 documentation :
This method traverses the view hierarchy by calling the pointInside:withEvent: method of each subview to determine which subview should receive a touch event. If pointInside:withEvent: returns YES , then the subview’s hierarchy is similarly traversed until the frontmost view containing the specified point is found. If a view does not contain the point, its branch of the view hierarchy is ignored.
因此,一种方法可能是创建一个 UIButton 子类并覆盖方法 -pointInside:withEvent:
- (BOOL)pointInsideCGPoint)point withEventUIEvent *)event {
if (CGRectContainsPoint(self.bounds, point) {
// The touch is within the button's bounds
}
return NO;
}
这将使您有机会在按钮范围内处理触摸,但同时返回 NO 将使 HitTest 失败,从而在 View 层次结构中传递触摸。
关于ios - UIButton 处理触摸然后通过?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/19688328/
|