• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UIButton 处理触摸然后通过?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 16:01:45 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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



Best Answer-推荐答案


编辑

一个有效的方法也是覆盖 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap