ios - 无法识别超出父 View 范围的 UILabel 上的点击

标签 ios objective-c uiview uigesturerecognizer

我有一个情况:

            -------------
            |           |
-------------------------
| UILabel               |
-------------------------
            | UIView    |
            |           |
            -------------

我需要检查用户是否点击了 UILabel。 UILabel 是 UIView 的 subview 。

result.userInteractionEnabled = YES;
textLabel.userInteractionEnabled = YES;
[result addSubview: textLabel];

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(reportTap:)];
tapRecognizer.delaysTouchesBegan = YES;
[textLabel addGestureRecognizer: tapRecognizer];

所以点击只能在 UIView 上识别,我试图覆盖 - (UIView *) hitTest: (CGPoint)point withEvent: (UIEvent *)event 但它从未被调用,试图添加tapRecognizer 到 UILabel ==> 这也不能解决问题。有什么帮助吗?

最佳答案

希望这对你有帮助。

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {

BOOL isInside = [super pointInside:point withEvent:event];

// identify the label view subclass
UILabel * textLabel = (UILabel *)[result viewWithTag:1];
CGPoint inLabelSpace = [self convertPoint:point toView:textLabel];

BOOL isInsideLabel = [textLabel pointInside:inLabelSpace withEvent:nil];

if (YES == isInsideLabel) {

    return isInsideLabel;

} // if (YES == isInsideLabel)


return isInside;

}

关于ios - 无法识别超出父 View 范围的 UILabel 上的点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20676330/

相关文章:

iOS block 和对自身的强/弱引用

c++ - 程序的输出?

objective-c - [table reloaddata] 应该只从主线程调用吗?

ios - 带有数据源的自定义 View 。应该什么时候第一次查询数据源?

ios - 使用 View Controller 自定义 UIView 旋转

ios - UIActivityViewController 共享应用列表为空

ios - 本地通知的自定义声音不起作用

ios - 自定义 iOS 键盘访问没有完全访问权限的容器

ios - UIDatePIcker TouchUp 事件以编程方式快速进行

ios - 当使用 UIView.animate 对 View 进行动画处理时,其他 View 也会不必要地进行动画处理