pixelbitlabs
pixelbitlabs

Reputation: 1934

Why is my UIGestureRecognizer code not working?

Why is the following code not working?

- (void)viewDidLoad
{    

UISwipeGestureRecognizer *oneFingerSwipeLeft = 
[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeLeft:)];
[oneFingerSwipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:oneFingerSwipeLeft];

UISwipeGestureRecognizer *oneFingerSwipeRight = 
[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeRight:)];
[oneFingerSwipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[[self view] addGestureRecognizer:oneFingerSwipeRight];

}

- (void)oneFingerSwipeLeft:(UISwipeGestureRecognizer *)recognizer 
{ 
CGPoint point = [recognizer locationInView:[self view]];
NSLog(@"Swipe up - start location: %f,%f", point.x, point.y);
}

- (void)oneFingerSwipeRight:(UISwipeGestureRecognizer *)recognizer 
{ 
CGPoint point = [recognizer locationInView:[self view]];
NSLog(@"Swipe down - start location: %f,%f", point.x, point.y);
}

Bear in mind that I have lots of layers (e.g. images, buttons and labels) in front of the bare view. Would this make a difference? How can I make the gestures be recognised on top of all these layers?

Thanks!

Upvotes: 0

Views: 4327

Answers (2)

iOSDevSF
iOSDevSF

Reputation: 1179

Double check and make sure the UIView you are adding the UIGestureRecognizer to has UserInteractionEnabled set to YES. i.e.

[self.imageView setUserInteractionEnabled:YES];

Upvotes: 8

Ankit Srivastava
Ankit Srivastava

Reputation: 12405

you have to pass all the touches from the top of your subviews to your last view.. so add a transparent view and then pass all those touches to the main view as explained in the thread

Passing a touch event to all subviews of a View Controller

Upvotes: 3

Related Questions