gpt4 book ai didi

ios - 从 UITapGestureRecognizer 中排除 subview

转载 作者:可可西里 更新时间:2023-11-01 03:05:26 24 4
gpt4 key购买 nike

我有一个 subview 和一个父 View 。 super View 附加了一个 UITapGestureRecognizer。

UIView *superview = [[UIView alloc] initWithFrame:CGRectMake:(0, 0, 320, 480);
UIView *subview = [[UIView alloc] initWithFrame:CGRectMake:(100, 100, 100, 100);
UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @selector(handleTap);
superview.userInteractionEnabled = YES;
subview.userInteractionEnabled = NO;
[superview addGestureRecognizer:recognizer];
[self addSubview:superview];
[superview addSubview:subview];

识别器也在 subview 中触发,有没有办法从 subview 中排除识别器?



我知道这个问题以前有人问过,但我没有找到好的答案。

最佳答案

您可以使用手势识别器委托(delegate)来限制它可以识别触摸的区域,类似于此示例:

recognizer.delegate = self;
...

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
CGPoint touchPoint = [touch locationInView:superview];
return !CGRectContainsPoint(subview.frame, touchPoint);
}

请注意,您需要保留对父 View 和 subview 的引用(使它们成为实例变量?)以便能够在委托(delegate)方法中使用它们

关于ios - 从 UITapGestureRecognizer 中排除 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17489780/

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