UIButton blocking touchesBegan and touchesMoved

前端 未结 2 612
一生所求
一生所求 2020-12-12 00:46

I\'m writing an app for tvOS - and it all works until I put a UIButton on the screen. The problem, when buttons are added, is that touchesBegan and touchesMoved stop workin

相关标签:
2条回答
  • 2020-12-12 01:20

    According to this answer, the button becomes a focused view and it's getting all the touches. You have to make your view (in which you implement touchesBegan and touchesMoved) focusable.

    0 讨论(0)
  • 2020-12-12 01:34

    If the focus engine is used (for instance when you have a UITabbarController or any UIButton on the screen) the touch events such as touchesBegan: withEvent: are indeed no longer called. If you really need them you must first make your view focusable by overriding its readonly property canBecomeFocused:

    - (BOOL)canBecomeFocused {
        return YES;
    }
    

    Now the focus can be moved to your view and touch events will be triggered as long as it is in focus. However focus may be immediately lost again as it moves along to other focusable items on the screen.

    To prevent this, implement on your view:

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

    Now your view can no longer lose its focus. This also means the user can no longer access other focusable items on the screen, so you may need implement logic that allows the user to leave again, for instance a button press.

    0 讨论(0)
提交回复
热议问题