简体   繁体   中英

Cancel UIButton touch while touching it

I have a situation where a user would be touching a UIButton and while they might already be touching it, I need to be able to cancel it or cause it to fail.

I have tried to use button.enabled and userInteractionEnabled, but neither one works on the fly. Both have to be set before the touch begins.

Can I cause the button to fail after and while it is being touched?

You need to register two actions for your button, one for touch down and one for touch up. In touch down, you can decide if you want to cancel; and in touch up, write the actual logic for button press.

[button addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown]; 

- (IBAction)touchDown:(id)sender
{
    UIButton *b = (UIButton*)sender;

    // Call this if you wish to cancel the event tracking
    [b cancelTrackingWithEvent:nil];
}

For reference, see the documentation of -cancelTrackingWithEvent: in UIControl .

One thing you can do to solve your issue is to customize the pressed appearance of the button to your needs. Use: – setBackgroundImage:@"NormalStateImage.png" forState: UIControlStateNormal – setBackgroundImage:@"HighlightedStateImage.png" forState: UIControlStateHighlighted

set the property adjustsImageWhenHighlighted to NO and the adjustsImageWhenDisabled property to NO

This can be done for any UIButton . If you want to cancel the button press at the time the user touches down on it, these steps should work:

  1. Set up the handler for the events where the cancellation needs to be checked. In this case, when the button is down.

     [yourButton addTarget: self action: @selector(buttonIsDownHandler:) forControlEvents: UIControlEventTouchDown]; 
  2. In the handler, decide whether to cancel the press

     -(IBAction) buttonIsDownHandler: (id) sender { if (needToCancelThisPress) { [yourButton cancelTrackingWithEvent: nil]; } } 
  3. The button's handler for UIControlEventTouchCancel will be called after the cancellation.

The button touch cancellation can be done at any time. For example, if you want to track when the user presses and holds for too long, you could start a timer in the UIControlEventTouchDown handler, and then cancel the touch when the timer expires.

Just have BOOL variable which points either button action is allowed or not and check where necessary. Enabled and userinteractionenabled is just to disable touches in general

Not sure about your problem but you can have differant actions based on control event like below

[button addTarget:self
                     action:@selector(onTouchUpInside:)
           forControlEvents:(UIControlEventTouchUpInside)]; 

[button addTarget:self
                     action:@selector(onTouchDown:)
           forControlEvents:(UIControlEventTouchDown)]; 

[button addTarget:self
                     action:@selector(onTouchUpOutside:)
           forControlEvents:(UIControlEventTouchUpOutside)]; 

Please excuse me if it is not helpful or what you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM