daveMac
daveMac

Reputation: 3037

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?

Upvotes: 10

Views: 7133

Answers (5)

Sohaib
Sohaib

Reputation: 11297

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.

Upvotes: 11

Mischinab
Mischinab

Reputation: 2911

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.

Upvotes: 2

luksfarris
luksfarris

Reputation: 1551

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

Upvotes: 2

msk
msk

Reputation: 8905

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.

Upvotes: 0

Marius Kurgonas
Marius Kurgonas

Reputation: 83

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

Upvotes: 0

Related Questions