gpt4 book ai didi

ios - UITapGestureRecognizer 吞下所有手势

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:09:36 30 4
gpt4 key购买 nike

我正在尝试将 UITapGestureRecognizer 添加到我的应用程序中,以便我可以检测所有两根手指的点击并采取行动,但传递任何其他用户输入。听起来微不足道,但我无法让它发挥作用。到目前为止,这是我的代码,在我的主视图 Controller 中:

- (void) viewDidLoad {
// other init stuff
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[tapRecognizer setNumberOfTouchesRequired:2];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setCancelsTouchesInView:NO];

UIView *tapHolderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[tapHolderView setMultipleTouchEnabled:YES];
[tapHolderView addGestureRecognizer:tapRecognizer];

[self.view addSubview:tapHolderView];

[tapRecognizer release];
}

- (void) handleTap:(UITapGestureRecognizer *) sender {
if (sender.state == UIGestureRecognizerStateEnded) {
// do something
}
}

发生的事情是这样的:用两根手指点击会触发 handleTap 方法。任何其他用户输入,无论是滑动、捏合、单指点击等,都不会被任何其他 View (UIToolbar、UIWebView 等)接收。

如果您对此有任何见解,我将不胜感激,如果我对问题的描述不够具体,请告诉我。

最佳答案

尝试将 View Controller 设置为 UIGestureRecognizerDelegate 的委托(delegate),并实现 gestureRecognizer:shouldRecognizeSimulaneouslyWithGestureRecgonizer 以始终返回“YES”。示例:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}

关于ios - UITapGestureRecognizer 吞下所有手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10927542/

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