按钮超出父View后 响应点击事件
2018-01-22 17:14
471 查看
///按钮 超出 父view视图时 也响应点击事件 override func hitTest(_ point:CGPoint, with event: UIEvent?) -> UIView? { var view = super.hitTest(point, with: event) if view == nil { let currentPoint = self.currentLocationBtn.convert(point, from:self) if self.currentLocationBtn.bounds.contains(currentPoint) { view = self.currentLocationBtn return view } let zoomOutPoint = self.zoomOutBtn.convert(point, from:self) if self.zoomOutBtn.bounds.contains(zoomOutPoint) { view = self.zoomOutBtn return view } let zoomInPoint = self.zoomInBtn.convert(point, from:self) if self.zoomOutBtn.bounds.contains(zoomInPoint) { view = self.zoomInBtn return view } } return view }
iOS判断哪个界面能接受消息是从View层级结构的父View向子View传递,即树状结构的根节点向叶子节点递归传递, hitTest和pointInside成对,且hitTest会调用pointInside,iOS的消息处理是,当消息被人处理后默认不再向父层传递。AppDelegate <- UIApplication <- UIWindow <- ViewController <- UIView <- UIButton当用户点击屏幕时,会产生一个触摸事件,系统会将该事件加入到一个由UIApplication管理的事件队列中, UIApplication会从事件队列中取出最前面的事件进行分发以便处理,通常,先发送事件给应用程序的主窗口(UIWindow), 主窗口会调用hitTest:withEvent:方法在视图(UIView)层次结构中找到一个最合适的UIView来处理触摸事件 (hitTest:withEvent:其实是UIView的一个方法,UIWindow继承自UIView,因此主窗口UIWindow也是属于视图的一种), hitTest:withEvent:方法大致处理流程是这样的:首先调用当前视图的pointInside:withEvent:方法判断触摸点是否在当前视图内: 若pointInside:withEvent:方法返回NO,说明触摸点不在当前视图内,则当前视图的hitTest:withEvent:返回nil 若pointInside:withEvent:方法返回YES,说明触摸点在当前视图内,则遍历当前视图的所有子视图(subviews),调用子视图的hitTest:withEvent:方法重复前面的步骤,子视图的遍历顺序是从top到bottom,即从subviews数组的末尾向前遍历,直到有子视图的hitTest:withEvent:方法返回非空对象或者全部子视图遍历完毕:若第一次有子视图的hitTest:withEvent:方法返回非空对象,则当前视图的hitTest:withEvent:方法就返回此对象,处理结束 若所有子视图的hitTest:withEvent:方法都返回nil,则当前视图的hitTest:withEvent:方法返回当前视图自身(self)最终,这个触摸事件交给主窗口的hitTest:withEvent:方法返回的视图对象去处理。拿到这个UIView后,就调用该UIView的touches系列方法。1.2、消息处理过程,在找到的那个视图里处理,处理完后根据需要,利用响应链nextResponder可将消息往下一个响应者传递。
iOS判断哪个界面能接受消息是从View层级结构的父View向子View传递,即树状结构的根节点向叶子节点递归传递, hitTest和pointInside成对,且hitTest会调用pointInside,iOS的消息处理是,当消息被人处理后默认不再向父层传递。AppDelegate <- UIApplication <- UIWindow <- ViewController <- UIView <- UIButton当用户点击屏幕时,会产生一个触摸事件,系统会将该事件加入到一个由UIApplication管理的事件队列中, UIApplication会从事件队列中取出最前面的事件进行分发以便处理,通常,先发送事件给应用程序的主窗口(UIWindow), 主窗口会调用hitTest:withEvent:方法在视图(UIView)层次结构中找到一个最合适的UIView来处理触摸事件 (hitTest:withEvent:其实是UIView的一个方法,UIWindow继承自UIView,因此主窗口UIWindow也是属于视图的一种), hitTest:withEvent:方法大致处理流程是这样的:首先调用当前视图的pointInside:withEvent:方法判断触摸点是否在当前视图内: 若pointInside:withEvent:方法返回NO,说明触摸点不在当前视图内,则当前视图的hitTest:withEvent:返回nil 若pointInside:withEvent:方法返回YES,说明触摸点在当前视图内,则遍历当前视图的所有子视图(subviews),调用子视图的hitTest:withEvent:方法重复前面的步骤,子视图的遍历顺序是从top到bottom,即从subviews数组的末尾向前遍历,直到有子视图的hitTest:withEvent:方法返回非空对象或者全部子视图遍历完毕:若第一次有子视图的hitTest:withEvent:方法返回非空对象,则当前视图的hitTest:withEvent:方法就返回此对象,处理结束 若所有子视图的hitTest:withEvent:方法都返回nil,则当前视图的hitTest:withEvent:方法返回当前视图自身(self)最终,这个触摸事件交给主窗口的hitTest:withEvent:方法返回的视图对象去处理。拿到这个UIView后,就调用该UIView的touches系列方法。1.2、消息处理过程,在找到的那个视图里处理,处理完后根据需要,利用响应链nextResponder可将消息往下一个响应者传递。
相关文章推荐
- UIButton超出父view的显示范围后,不响应点击事件
- UIButton超出父view的显示范围后,不响应点击事件
- 解决通过UIAlertView按钮点击事件pop回上级页面键盘会响应问题
- 如何为一个按钮(View)设置任意多次点击事件以及可以同时响应多个多次点击事件
- 在Activity中响应ListView内部按钮的点击事件
- UIImageView响应点击事件
- 百度地图api点击BMKAnnotationView,不响应didSelectAnnotationView事件。
- UIActionSheet 按钮不响应点击事件的解决方法
- UIScrollView在有点击手势的情况下,会导致UITableView的cell点击事件无法响应
- android RecyclerView响应点击事件
- UIImageView响应点击事件
- view上添加点手势 button无法响应点击事件
- Android 设置了ClickableSpan导致的上层View点击事件无法响应解决方案
- JavaScript之响应按钮点击事件
- 在Activity中响应ListView内部按钮的点击事件
- webView文件上传取消弹窗后再次点击不响应点击事件问题
- 关于子视图超出父视图后无法响应点击事件的处理办法
- UIImageView响应点击事件
- TextView按钮点击第二次才有响应的问题解决
- UIImageView响应点击事件