当使用UIGestureRecognizer时,如何知道事件发生在哪个子视图中?
根据文档:
A gesture recognizer operates on
touches hit-tested to a specific view
and all of that view’s subviews.
据我所见,’view’属性是
The view the gesture recognizer is
attached to.
这将是父视图。
解决方法
这将在事件的位置找到最内层的后代视图。 (请注意,如果该子视图有任何交互式内部私有孙代码,这段代码也会找到那些。)
UIView* view = gestureRecognizer.view; CGPoint loc = [gestureRecognizer locationInView:view]; UIView* subview = [view hitTest:loc withEvent:nil];
在Swift 2:
let view = gestureRecognizer.view let loc = gestureRecognizer.locationInView(view) let subview = view?.hitTest(loc,withEvent: nil) // note: it is a `UIView?`
在Swift 3:
let view = gestureRecognizer.view let loc = gestureRecognizer.location(in: view) let subview = view?.hitTest(loc,with: nil) // note: it is a `UIView?`
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。