【问题标题】:IOS UIButton not responding to touch, takes multiple presses, UIScreenEdge Gesture interference?IOS UIButton 不响应触摸,需要多次按下,UIScreenEdge 手势干扰?
【发布时间】:2014-01-06 16:32:03
【问题描述】:

我展示了一系列模态视图,每个视图都有一个顶部标题工具栏和右上角的 UIBarButtonItem,并带有一个“X”图像,允许用户单击它来关闭视图。

我遇到的问题是这些按钮经常对触摸没有响应,并且似乎需要轻按 5-10 次才能执行操作。我什至增加了按钮的图像大小以使区域更大,但没有帮助。然而,在模拟器中,只需单击鼠标指针,它们似乎可以立即正常工作。

我注意到的一件事是,主视图上似乎有一个我尚未添加的 UIScreenEdgePanGestureRecognizer。我认为它是用于可以从顶部拉下的 Iphone 通知中心手柄。底部也有一个。这些会干扰我的按钮吗?这些手柄总是响应轻微的向下拖动。

我做了一个测试,我把按钮放在屏幕中间,每次都能立即工作。虽然顶部和底部是工具栏和按钮的明显位置,但边缘手势识别器会导致这种冲突并且不会让每个人都生气,这似乎很奇怪。

// The button, added as part of a standard toolbar on very top of screen
UIButton *closeButtonImage = [UIButton buttonWithType:UIButtonTypeCustom];
[closeButtonImage setImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal];
[closeButtonImage setFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width - 35, 9, 26, 26)];
[closeButtonImage addTarget:self action:@selector(closeClick:) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithCustomView:closeButtonImage];

【问题讨论】:

    标签: ios iphone objective-c uibutton


    【解决方案1】:

    而不是这个:

    [closeButtonImage setFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width - 35, 9, 26, 26)];
    

    试试这个:

    [closeButtonImage setFrame:CGRectMake(0, 0, 26, 26)];
    

    原因:您将一个按钮放入 UIBarButtonItem。这有点奇怪,因为条形按钮项本身一种按钮,但它并没有什么问题。但是,放置其内容的位置取决于栏按钮项。如果您将按钮放在包含栏按钮项的边界之外,它将变得不可点击。

    说真的,我真正的建议是不要做你正在做的事情。只需为 UIBarButtonItem 本身提供 close.png 图像作为其图像,然后在栏按钮项本身上设置操作。它已经是一个按钮了。

    【讨论】:

    • 伟大的收获!这似乎是问题所在,而不是手势。他们现在反应很好。这显然很有意义,因为框架矩形的原点是相对于它的父级 UIBarButtonItem。当我试图将 UIButton 添加到工具栏时,一定是被遗忘了,当然这永远不会起作用。
    【解决方案2】:

    您可以尝试为“forControlEvents:”使用其他类型,例如UIControlEventTouchUpInside

    【讨论】:

      猜你喜欢
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多