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

后端 未结 3 1084
一向
一向 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.

提交回复
热议问题