今天有个同事遇到一个问题。在Scroll View上改了一层view。view上有个按钮,他想手势穿过view去滑动Scroll View,但是又想view的上的按钮响应事件。
如图:
其实很简单:
重写test的hitTest方法就好了。如果为父视图响应则向下传递,不然子视图按钮继续响应
// // test.swift // test // // Created by quanhao huang on 2020/8/21. // Copyright © 2020 hqh. All rights reserved. // import UIKit class test: UIView { /*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/ override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { let hitView = super.hitTest(point, with: event) if (hitView == self) { return nil }else { return hitView } } }
(这其实就是一个很简单的响应者链问题)
Demo-手势穿过父视图不影响子视图
本文地址:http://www.51sjk.com/Upload/Articles/1/0/252/252035_20210626002206120.jpg