gpt4 book ai didi

objective-c - 如何检测 subview 中的点击手势

转载 作者:技术小花猫 更新时间:2023-10-29 10:11:49 26 4
gpt4 key购买 nike

小问题:如何检测点击手势识别器是否在它所添加到的 View 的 subview 内?例如。如果我单击一个对象,例如已作为 subview 添加到已添加点击手势识别器的背景的正方形,我如何检测它已被点击?

最佳答案

当您的处理程序方法被调用时,您可以使用 -locationInView: 获取手势识别器的点击点。然后,在 UIView 上使用以下方法:- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 获取对被点击的实际 subview 的引用,记住该点您传入的与 View 在同一坐标空间中。

一些让你开始的代码:

CGPoint point = [tapGestureRecognizer locationInView:parentView];
UIView *tappedView = [parentView hitTest:point withEvent:nil];

要使 HitTest 正常工作, View 需要将 userInteractionEnabled 属性设置为 YES。许多 View ,例如 UILabels 默认将此设置为 NO。所以在上述之前:

self.subviewOfInterest.userInteractionEnabled = YES;

关于objective-c - 如何检测 subview 中的点击手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10459184/

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