您的位置:首页 > 移动开发 > IOS开发

按钮超出父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可将消息往下一个响应者传递。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息