UIButton with multiple actions: How to prevent other actions from firing

后端 未结 3 1080
一向
一向 2021-01-14 08:15

I\'m trying to find a way to consume a button press, not from the response chain per se, but from other action methods bound to that button. I\'ve looked all over for this

相关标签:
3条回答
  • 2021-01-14 08:36

    Based on the @danielquokka idea about override the method sendActionsForControlEvents:, I subclass UIButton and add following codes. It works fine. After firing event, it will block UI events for 0.5 seconds.

    - (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
    {
        [super sendAction:action to:target forEvent:event];
        self.userInteractionEnabled = NO;
    
        ///! After handle UIEvent, block this button UI events for a while.
        [self performSelector:@selector(delayEnable) withObject:nil afterDelay:0.5];
    }
    
    - (void)delayEnable
    {
        self.userInteractionEnabled = YES;
    }
    

    UPDATE

    Another way to ignore UI events is by [[UIApplication sharedApplication] beginIgnoringInteractionEvents] and [[UIApplication sharedApplication] endIgnoringInteractionEvents].

    beginIgnoringInteractionEvents and endIgnoringInteractionEvents will block all touch events for the Application until endIgnoringInteractionEvents called.

    0 讨论(0)
  • 2021-01-14 08:36

    if you know the current state of the app like tutorial etc then probably better to use only one handler1. And add to the body of the method handler1: if (mode == tutorial) {tutorial} else {default}.

    0 讨论(0)
  • 2021-01-14 08:45

    You may be able to add a category to UIButton (or subclass it), overriding the method sendActionsForControlEvents: to only call sendAction:to:forEvent: for one target (refer to the documentation on UIControl for descriptions of those methods).

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