gpt4 book ai didi

ios - UIButton 句柄触摸然后通过?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:39:17 24 4
gpt4 key购买 nike

是否可以处理 UIButton 发生的触摸,然后将其传递给下一个响应者?

最佳答案

编辑

一个有效的方法也是覆盖 UIRespondertouchesEnded:withEvent: 方法

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)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)pointInside:(CGPoint)point withEvent:(UIEvent *)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/

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