Reputation: 28248
Is it possible to fire a UIPinchGestureRecognizer action just once?
I want to enable my users to pinch (actually spread) to fire of an action but the method gets called constantly which I understand is the functionality of a UIPinchGestureRecognizer.
UPDATED CODE USING ANSWER BELOW (just detects zoom):
-(void) handlePinchGesture: (UIPinchGestureRecognizer *) sender {
if (sender.state == UIGestureRecognizerStateBegan) {
self.startingScale = sender.scale;
}
if (sender.state == UIGestureRecognizerStateEnded) {
if (sender.scale > self.startingScale) {
[self zoomIn];
}
}
}
Upvotes: 1
Views: 480
Reputation: 130193
Gesture recognizers are state machines, and if you don't specify which state you want to listen to simply calling a selector from a gesture will fire for any/every state across the board.
To limit detection to when the gesture starts you can use:
- (void)myGestureRecognized:(UIGestureRecognizer*)sender
{
if (sender.state == UIGestureRecognizerStateBegan) {
//do something
}
}
Other recognition states include:
typedef enum {
UIGestureRecognizerStatePossible,
UIGestureRecognizerStateBegan,
UIGestureRecognizerStateChanged,
UIGestureRecognizerStateEnded,
UIGestureRecognizerStateCancelled,
UIGestureRecognizerStateFailed,
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
} UIGestureRecognizerState;
Upvotes: 6