查找使用UITapGestureRecognizer时点击的子视图 [英] Find which child view was tapped when using UITapGestureRecognizer

查看:104
本文介绍了查找使用UITapGestureRecognizer时点击的子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何知道在使用UIGestureRecognizers时发生了哪些子视图?

How do I know on which of the the child views an event occurred when using UIGestureRecognizers?

根据文档:


手势识别器对
进行操作,触摸特定视图
以及该视图的所有子视图。

A gesture recognizer operates on touches hit-tested to a specific view and all of that view’s subviews.

据我所知,'view'属性是

As far as I can see, the 'view' property is


查看手势识别器附加的是

The view the gesture recognizer is attached to.

这将是父视图。

推荐答案

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

This will find the innermost descendant view at the event's location. (Note that if that child view has any interactive internal private grandchildren this code will find those too.)

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

这篇关于查找使用UITapGestureRecognizer时点击的子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆