Long press and single button press

How to get a button Pressed once or Long press on an event with a button press

+4
source share
2 answers

Check this code

//Add Long Press Gesture Reconizer UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)]; longPress.minimumPressDuration = 3; //seconds longPress.delegate = self; [yourButton addGestureRecognizer:longPress]; //Add button touch UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)]; tapGesture.numberOfTapsRequired = 1; tapGesture.numberOfTouchesRequired = 1; [yourButton addGestureRecognizer:tapGesture]; //For touch you can also set selector for button event with Controlevent touchupinside -(void) handleLongPress : (id)sender { //Long Press done by the user } -(void) tapDetected : (id) sender { //Button Tapped by user } 
+3
source

You can use NSTimer to measure the duration between the touch down in and touch up in events on a button.

Then you define the threshold for โ€œlong pressโ€ and treat the touch event as โ€œlong pressโ€ if the duration threshold is passed.

+1
source

All Articles