使用 UIGestureRecognizer 时如何知道哪个子视图发生了事件?
根据文档:
手势识别器运行于
触摸特定视图的命中测试
以及该视图的所有子视图。
据我所知,“view”属性是
手势识别器的视图是
附于.
这将是父视图。
这将找到事件位置的最里面的后代视图。 (请注意,如果该子视图有任何交互式内部私有孙子视图,则此代码也会找到它们。)
UIView* view = gestureRecognizer.view;
CGPoint loc = [gestureRecognizer locationInView:view];
UIView* subview = [view hitTest:loc withEvent:nil];
在斯威夫特 2 中:
let view = gestureRecognizer.view
let loc = gestureRecognizer.locationInView(view)
let subview = view?.hitTest(loc, withEvent: nil) // note: it is a `UIView?`
在斯威夫特 3 中:
let view = gestureRecognizer.view
let loc = gestureRecognizer.location(in: view)
let subview = view?.hitTest(loc, with: nil) // note: it is a `UIView?`
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)