为什么 UIButton 会覆盖 UITapGestureRecognizer?

发布于 2024-09-25 19:12:10 字数 1159 浏览 0 评论 0原文

我有一个带有 2 个子视图的父 UIView:一个 UIButton 和一个实现了 UITapGestureRecognizer 的 UIScrollView。 UITapGestureRecognizer 用于放大和缩小滚动视图,按钮用于显示/隐藏一些文本。

我的问题是,一旦我使用了 UIButton,UITapGestureRecognizer 就不再起作用。

此外,UITapGestureRecognizer 是在scrollView 类中实现的,这是正确计算zoomScale 所必需的。

我发现了一些类似的问题,但他们有相反的问题。 有什么想法吗?

谢谢!

编辑:我刚刚意识到使用按钮后,如果我在滚动视图中滚动或捏合/缩放然后点击,它会再次起作用。只是使用按钮后不会立即起作用。

另外,这是我的按钮如何显示文本视图的代码:

- (void)textImageButtonAction:(id)sender{
    if(self.textView.frame.origin.y < 0){
        [UIView beginAnimations:@"HideTabbar" context:nil];
        [UIView setAnimationDuration:.3];
            textView.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, footerStringSize.height +10);
        [UIView commitAnimations];
    }
    else if(self.textView.frame.origin.y == 0) {//If the textView is visible
        [UIView beginAnimations:@"HideTabbar" context:nil];
        [UIView setAnimationDuration:.3];
            textView.frame = CGRectMake(0, -footerStringSize.height -10, self.view.frame.size.width, footerStringSize.height +10);
        [UIView commitAnimations];
    }
 }

I have a parent UIView with 2 subviews: a UIButton and a UIScrollView with a UITapGestureRecognizer implemented. The UITapGestureRecognizer is used to zoom in and out the scrollview and the button is used to show/hide some text.

My problem is that once I've used the UIButton, the UITapGestureRecognizer is no longer functioning.

Also, the UITapGestureRecognizer is implemented from within the class for the scrollView, which is necessary to calculate the zoomScale correctly.

I've found a few similar questions, except they were having the opposite problem.
Any ideas?

Thanks!

Edit: I just realized that after using the button, if I scroll around or pinch/zoom in the scrollView and then tap, it works again. It just doesn't work immediately after using the button.

Also, here's my code for how the button shows the text view:

- (void)textImageButtonAction:(id)sender{
    if(self.textView.frame.origin.y < 0){
        [UIView beginAnimations:@"HideTabbar" context:nil];
        [UIView setAnimationDuration:.3];
            textView.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, footerStringSize.height +10);
        [UIView commitAnimations];
    }
    else if(self.textView.frame.origin.y == 0) {//If the textView is visible
        [UIView beginAnimations:@"HideTabbar" context:nil];
        [UIView setAnimationDuration:.3];
            textView.frame = CGRectMake(0, -footerStringSize.height -10, self.view.frame.size.width, footerStringSize.height +10);
        [UIView commitAnimations];
    }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疯狂的代价 2024-10-02 19:12:10

您是否应该在按钮完成其职责后退出第一响应者,以便按钮所在的视图不再受控制。

怎么样添加:
将SubviewtoFront:scrollview

带到 UIButton 代码的末尾。这里的想法是让你的滚动视图成为最重要的视图;我认为当您触摸滚动视图并重新激活手势识别器时会发生这种情况

Should you resign first responder after the button has done its duty so that the view that the button is in is no longer in control.

How about adding:
bringSubviewtoFront:scrollview

to the end of your UIButton code. The idea here is to make your scrollview the foremost view; which is what I think is happening when you touch the scrollview and thus reactivating the gesture recognizer

爱殇璃 2024-10-02 19:12:10

您是否尝试过调用[scrollView变成FirstResponder]?这应该能让它发挥作用。

Have you tried to call [scrollView becomeFirstResponder]? That should make it work.

别闹i 2024-10-02 19:12:10

我最终为这个问题寻求了苹果开发者技术支持,他们检查了我的代码。问题的原因是不同类别的手势识别器存在冲突。这是一个第一响应者问题,但由于某种原因,resignFirstResponder和becomeFirstResponder没有任何效果。

我必须重新构建代码以及手势识别器的组织方式才能使其正常工作。

感谢大家的帮助。

I finally sought Apple Developer Tech Support for this issue and they looked through my code. The cause of the problem turned out to be conflicting gesture recognizers in different classes. It was a first responder issue, but for some reason, resignFirstResponder and becomeFirstResponder did not have any effect.

I had to restructure the code and how the gesture recognizers were organized to get it to work properly.

Thanks everyone for the help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文