Cancel Press If Button Not Held Long Enough Objective-C

I have a UIButton in Objective-C and I want to cancel the press if the button is let go before 10 seconds. How would I go about this?

Thanks In Advance

Answers


You can use something like this:

[yourButton addGestureRecognizer:[[UILongPressGestureRecognizer alloc] 
                                   initWithTarget:self action:@selector(handle:)]];

Create an NSTimer property called timer:

@property (nonatomic, strong) NSTimer *timer;

And a counter:

@property (nonatomic, strong) int counter;

- (void)incrementCounter {
self.counter++;

}

- (void)handle:(UILongPressGestureRecognizer)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan) {
     self.counter = 0;
     self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(incrementCounter) userInfo:nil repeats:yes];
    }
    if (gesture.state == UIGestureRecognizerStateEnded) {
    [self.timer invalidate];
    }
 }

So when the gesture begins start a timer that fires the incrementation method every second until the gesture ends. In this case you'll want to set the minimumPressDuration to 0 otherwise the gesture won't start straight away. In this way you can get your timer and put a condition on button pressed with self.counter = 10 seconds.

Please let me know if it helps.


To stick with a UIButton's intended design, I would suggest utilizing a UIButton's UIControlEventTouchUpInside and UIControlEventTouchDown control events.

(hint: -addTarget:action:forControlEvents: method)

Logically:
  1. Start a timer on UIControlEventTouchDown
  2. Stop the timer on UIControlEventTouchUpInside
  3. In the UIControlEventTouchUpInside action method
    • check the timer duration (if 10seconds then perform action else don't)

Example:

Prerequisites:
  1. Declare following objects (in the .h file of the class)
    • UIButton *btnTest;
    • NSTimer *tmrTest;
    • int i_tmrCount;

Code:
- (void)viewDidLoad
{
    //...

    i_tmrCount = 0;

    btnTest = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnTest setFrame:CGRectMake(20, 100, 100, 30)];
    [btnTest setTitle:@"Test" forState:UIControlStateNormal];

    //method connected to the button pressed down event alone
    [btnTest addTarget:self action:@selector(btnTestActStart:) 
                  forControlEvents:UIControlEventTouchDown];

    //method connected to the button pressed down and released event
    [btnTest addTarget:self action:@selector(btnTestAct:) 
                  forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnTest];
}

- (void)btnTestActStart:(UIButton *)sender
{
    tmrTest = [NSTimer scheduledTimerWithTimeInterval:1
                                               target:self
                                             selector:@selector(timerDo)
                                             userInfo:nil
                                              repeats:YES];
}

-(void)timerDo
{
    i_tmrCount++;
}

- (void)btnTestAct:(UIButton *)sender
{
    //stop the timer
    [tmrTest invalidate];

    if (i_tmrCount >= 10) {
        //do something
        NSLog(@"action");
    } else {
        //do nothing
        NSLog(@"reset");
    }

    //reset i_tmrCount to initial state (to handle next occurrence)
    i_tmrCount = 0;
}

PS: If it were a UILabel then go with UILongPressGestureRecognizer but for a UIButton, it won't be ideal because you'll have to drop the implementation of the method connected to the button's UIControlEventTouchUpInside and instead make the method connected to the UILongPressGestureRecognizer as the main button method (it feels kinda dirty)


Need Your Help

Return the third to last elements of given list in Prolog

prolog

I am new in using Prolog, I want to write a program that compute the third to last element of a given list.

CardView button and text not aligning

android material-design

I'm having a problem creating a CardView with a button that aligns to the start of the text: