gpt4 book ai didi

iOS gestureRecognizer 未处理 subview

转载 作者:行者123 更新时间:2023-11-29 12:45:43 25 4
gpt4 key购买 nike

我有以下问题:在我的 iphone 应用程序中,我有一个 UIView 包含 0 到多个 subview 。这些 subview 可能会溢出父 View 并因此被隐藏,它们都是 UIButtons。我向 UIView 添加了一个 UISwipeGestureRecognizer 来移动按钮,效果很好。但是,它仅在手势在后台完成时有效,UIButton 会干扰手势识别器。

我怎样才能传递手势?顺便提一句。我仍然需要点击按钮。

谢谢!

编辑:

我也尝试将手势识别器添加到 UIButtons,但它没有被触发...虽然执行滑动手势可以防止 UIButton 进入突出显示状态,但它不会触发手势。我按照 UIButton and Swipe Gesture 中的建议添加了 setDelaysTouchesBegan:YES .我现在就是这样做的:

UIButton *breadcrumb = [UIButton buttonWithType:UIButtonTypeCustom];
[breadcrumb setImage:[UIImage imageNamed:@"Next"] forState:UIControlStateNormal];
[breadcrumb setTitle:title forState:UIControlStateNormal];
[breadcrumb.titleLabel setFont:[UIFont systemFontOfSize:12.0f]];
[breadcrumb sizeToFit];
[breadcrumb setTag:level];


UISwipeGestureRecognizer *gr = [[UISwipeGestureRecognizer alloc] init];
[gr setDelegate:self];
[gr setDirection:UISwipeGestureRecognizerDirectionRight];
[gr setDelaysTouchesBegan:YES];
[self.tableView addGestureRecognizer:gr];
[breadcrumb addGestureRecognizer:gr];

编辑 2:

我现在已经将 UIButton 子类化并像这样初始化它:[BreadcrumbButton buttonWithType:UIButtonTypeCustom]。在初始化程序中,我将按钮本身添加为所有触摸事件的监听器 [self addTarget:self action:@selector(eventReceiver:) forControlEvents:UIControlEventAllTouchEvents]; 以检查发生了什么。

- (void)eventReceiver:(UIButton *)btn {
NSLog(@"Reveived event: %@ ---------------", btn);

for(UIGestureRecognizer *gr in ev.gestureRecognizers) {
NSLog(@"Gesture: %@", gr);
}
}

我看到的是 a) 该按钮只添加了一个手势识别器,并且 b) 此 UISwipeGestureRecognizer 在滑动期间跳转到状态 Possible 但不会转发到它的状态委托(delegate)方法。

最佳答案

您必须子类化 UIButton 并覆盖点击委托(delegate)回调,并将此调用转发给任何正在处理 UISwipeGestureRecognizer 的对象。除非你在 UIButton 上添加手势识别器,否则它总是会在它后面的 View 之前调用它的触摸处理程序。您还可以明确告诉按钮不处理它的触摸事件,从而将触摸事件传递到链下(通过 userInteractionEnabled),但正如您已经声明的那样,您不希望这样做。解决此问题的最佳方法是创建 UIButton 的子类并在那里处理触摸事件和/或使用委托(delegate)转发事件。按下按钮是一个触摸事件,因此您可能只想向按钮添加一个点击手势识别器并从中调用 IBAction,然后让滑动手势识别器转发委托(delegate)调用。

关于iOS gestureRecognizer 未处理 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23736525/

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