YosiFZ
YosiFZ

Reputation: 7900

UISwipeGestureRecognizer UIGestureRecognizerStateChanged not called

I'm define two UISwipeGestureRecognizer in my application:

UISwipeGestureRecognizer *swipeLeftVolume = [[UISwipeGestureRecognizer alloc]  initWithTarget:self action:@selector(handleSwipeGestureVolume:)];
swipeLeftVolume.direction = UISwipeGestureRecognizerDirectionLeft;
[self.playerView addGestureRecognizer:swipeLeftVolume];

UISwipeGestureRecognizer *swipeRightVolume = [[UISwipeGestureRecognizer alloc]  initWithTarget:self action:@selector(handleSwipeGestureVolume:)];
swipeRightVolume.direction = UISwipeGestureRecognizerDirectionRight;
[self.playerView addGestureRecognizer:swipeRightVolume];

In the target method i have 3 states:

UIGestureRecognizerStateBegan
UIGestureRecognizerStateChanged
UIGestureRecognizerStateEnded

and i noticed that only the UIGestureRecognizerStateEnded is called.

Any idea what can be the problem? i want to recognize a swipe on specific UIView :

- (void)handleSwipeGestureVolume:(UISwipeGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateBegan) {
        NSLog(@"Start");
    } else if (sender.state == UIGestureRecognizerStateChanged) {
        NSLog(@"Changed");
    } else if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"Finish");
    }
}

Upvotes: 0

Views: 371

Answers (3)

trungduc
trungduc

Reputation: 12154

Seem like you are confused between UISwipeGestureRecognizer and UIPanGestureRecognizer.

UISwipeGestureRecognizer will generate UIGestureRecognizerStateEnded state only while UIPanGestureRecognizer has both 3 states you want.

If you need to receive both UIGestureRecognizerStateBegan, UIGestureRecognizerStateChanged, UIGestureRecognizerStateEnded, use UIPanGestureRecognizer instead.

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]  initWithTarget:self action:@selector(handleSwipeGestureVolume:)];
[self.playerView addGestureRecognizer:panGesture];

Upvotes: 2

Mahendra
Mahendra

Reputation: 8924

For any gesture recogniser, user interaction must be enabled to work. Without it none of the gestures will be triggered including touchBegan:, touchMove:, etc.

so you need to make [self.playerView setUserInteractionEnabled:TRUE];

And one more thing I would like to let you know that if you have implemented touchBegan:, touchMove:, etc. then the method will be triggered first for gesture rather than UIGestureRecognizer shilds.

Upvotes: 0

Kamal Thakur
Kamal Thakur

Reputation: 408

Make sure you enable UserInteraction for self.playerView.

Upvotes: 1

Related Questions