gpt4 book ai didi

objective-c - 查找使用 UITapGestureRecognizer 时点击了哪个 subview

转载 作者:IT老高 更新时间:2023-10-28 11:26:34 27 4
gpt4 key购买 nike

在使用 UIGestureRecognizers 时,我如何知道在哪个 subview 上发生了事件?

根据文档:

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.

这将是父 View 。

最佳答案

这将在事件的位置找到最里面的后代 View 。 (请注意,如果该 subview 有任何交互式内部私有(private)孙子,则此代码也会找到这些。)

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?`

关于objective-c - 查找使用 UITapGestureRecognizer 时点击了哪个 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2788870/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com