0

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!

pixelbitlabs
  • 1,934
  • 6
  • 36
  • 65

2 Answers2

8

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

[self.imageView setUserInteractionEnabled:YES];

iOSDevSF
  • 1,169
  • 1
  • 13
  • 24
3

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

Community
  • 1
  • 1
Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
  • could you provide some sample code because I don't understand how to do it...? I know you gave that link but it's not very clear exactly how to do it :) – pixelbitlabs Dec 03 '11 at 11:48
  • Try something like this... for (UIView *view in subviews) [view touchesBegan:touches withEvent:event]; The code above, in your touchesBegan method for example would pass the touches to all of the subviews of view. – Ankit Srivastava Dec 03 '11 at 11:55
  • this is what I've done - but it also has some errors: http://file.reddexuk.com/1g3p0N383h2C3f3v3e0c – pixelbitlabs Dec 03 '11 at 12:01
  • you will have to implement all the touches delegate functions and perform the same in every method and also that link of yours is not working. – Ankit Srivastava Dec 03 '11 at 12:05
  • you need to learn a lot before you can start implementing such things.(looking at the error image) I just can't explain you everything here. Sorry that is the best I could do. – Ankit Srivastava Dec 03 '11 at 12:11