Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Detect User Touch Release

This may have been posted here somewhere but I can't find it. I am writing a simple iOS app with two UIViews. The user will first press and hold a certain area of the screen then release their touch on it then quickly touching a second view below.

The first UIView has a UILongPressGestureRecognizer attached to it and works fine. The second UIView has a UITapGestureRecognizer attached to it and also works fine. I cannot however, get either of these gesture recognizers to return anything that states that the user released their touch.

I have tried this code to no avail:

- (void)holdAction:(UILongPressGestureRecognizer *)holdRecognizer
{
    if (UIGestureRecognizerStateRecognized) {
        holdLabel.text = @"Holding Correctly. Release Touch when ready.";
        holdView.backgroundColor = [UIColor greenColor];
    } else if (UIGestureRecognizerStateCancelled){
        holdLabel.text = @"Ended";
        holdView.backgroundColor = [UIColor redColor];
}

Any suggestions would be great and especially if someone knows how to implement a call that returns the state of a user touching the device. I've looked over the developer docs and have come up empty.

like image 719
Brian Avatar asked Sep 11 '11 18:09

Brian


1 Answers

After tinkering for a couple hours, I found a way that is working, not sure if its the best way to do it. Turns out I need to be writing it like the code below. I wasn't calling the specific UIGestureRecognizer I was declaring in the viewDidLoad() method.

- (void)holdAction:(UILongPressGestureRecognizer *)holdRecognizer
{
    if (holdRecognizer.state == UIGestureRecognizerStateBegan) {
        holdLabel.text = @"Holding Correctly. Release when ready.";
        holdView.backgroundColor = [UIColor greenColor];
    } else if (holdRecognizer.state == UIGestureRecognizerStateEnded)
    {
        holdLabel.text = @"You let go!";
        holdView.backgroundColor = [UIColor redColor];
    }
}
like image 185
Brian Avatar answered Oct 08 '22 04:10

Brian