【问题标题】:UIButton blocking touchesBegan and touchesMovedUIButton 阻止 touchesBegan 和 touchesMoved
【发布时间】:2016-04-07 18:14:15
【问题描述】:

我正在为 tvOS 编写一个应用程序 - 在我将 UIButton 放在屏幕上之前,它都可以正常工作。添加按钮时的问题是 touchesBegan 和 touchesMoved 停止工作。如果我移除按钮,则 touchesBegan 和 touchesMoved 将再次开始正常工作。为了实验,我已经尝试取消选中“启用用户交互” - 但这没有任何区别。我也尝试过继承 UIButton 并添加以下代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self.nextResponder touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    [self.nextResponder touchesMoved:touches withEvent:event];
}

遗憾的是,这似乎也不起作用。有没有人对我下一步可能尝试什么有任何建议?

【问题讨论】:

  • 除了按钮之外,您在该视图上还有什么?你想达到什么目标?

标签: uibutton tvos touchesbegan touchesmoved


【解决方案1】:

根据this answer,按钮变成了一个焦点视图,它得到了所有的触摸。您必须使您的视图(在其中实现 touchesBegantouchesMoved)具有焦点。

【讨论】:

    【解决方案2】:

    如果使用焦点引擎(例如,当您在屏幕上有UITabbarController 或任何UIButton 时)确实不再调用诸如touchesBegan: withEvent: 之类的触摸事件。如果您真的需要它们,您必须首先通过覆盖其只读属性canBecomeFocused 来使您的视图具有焦点:

    - (BOOL)canBecomeFocused {
        return YES;
    }
    

    现在焦点可以移动到您的视图,只要它处于焦点,就会触发触摸事件。但是,当焦点移动到屏幕上的其他可聚焦项目时,焦点可能会立即再次丢失。

    为了防止这种情况,在你的视图上实现:

    - (BOOL)shouldUpdateFocusInContext:(UIFocusUpdateContext *)context {
       return NO;
    }
    

    现在您的视图不能再失去焦点。这也意味着用户无法再访问屏幕上的其他可聚焦项目,因此您可能需要实现允许用户再次离开的逻辑,例如按下按钮。

    【讨论】:

      猜你喜欢
      • 2011-03-21
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 2010-11-10
      相关资源
      最近更新 更多