Search This Blog

Thursday, 14 February 2013

Long Gesture-iOS

Hello friends ,
      So here is something from old school......hehehe.......we still have lot of useful things from old school.What if you are working on xcode project using MainStoryBoard??? and you want to capture the long press on button??? ...dont know ?......awww dont be sad ...here is the code..


CODE:

first add a gesture recognizer,as i am doing below,suppose you have an IBOutlet 'btnEmergencyBeacon' for UIButton.Then in your view did load method ,do this :

  UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self.btnEmergencyBeacon addGestureRecognizer:longPress];



Now simply override this function , in your same .m file :

- (void)longPress:(UILongPressGestureRecognizer*)gesture {
    if ( gesture.state == UIGestureRecognizerStateEnded ) {
       //Do something on long press....for example ill code for UIAlertView.

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Demo" message:@"Long Press Countered" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
}


thats all,you can add this gesture recognizer to some view or anything else....


Happy programming ;)

No comments:

Post a Comment