gpt4 book ai didi

iOS:拦截从顶 View 到底 View 的点击手势事件

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

在我的 View Controller 中,我将 UITapGestureRecognizer 添加到 self.view。我在 self.view 之上添加了一个小 View 。当我点击小 View 时,我不想触发 self.view 中的 UITapGestureRecognizer 事件。这是我的代码,它不起作用。

    - (void)viewDidLoad {
[super viewDidLoad];

UITapGestureRecognizer *_tapOnVideoRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleControlsVisible)];

[self.view addGestureRecognizer:_tapOnVideoRecognizer];

UIView *smallView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
smallView.backgroundColor=[UIColor redColor];
smallView.exclusiveTouch=YES;
smallView.userInteractionEnabled=YES;

[self.view addSubview:smallView];
}

- (void)toggleControlsVisible
{
NSLog(@"tapped");
}

当我点击小 View 时,它仍然会触发 self.view 中的点击事件。 Xcode 记录“点击”。如何拦截smallView到self.view的手势事件?

最佳答案

像这样实现 UIGestureRecognizer 委托(delegate)方法 shouldReceiveTouch。如果触摸位置在 topView 内部,则不接收触摸。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
CGPoint location = [touch locationInView:self.view];

if (CGRectContainsPoint(self.topView.frame, location)) {
return NO;
}
return YES;
}

关于iOS:拦截从顶 View 到底 View 的点击手势事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27663767/

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