amurcia
amurcia

Reputation: 791

UISwipeGestureRecognizer in UIImageView

I want to put two swipe gesture recognizers in a UIImageView but gestures aren't recognized. Only recognizes the tap gesture.

This is my code:

 - (void)viewDidLoad {

  //Acciones
     img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[urls
                                                                                       objectAtIndex:index]]]];

    UIImage *img2;

    if (img.size.height > img.size.width) {
         img2 = [[UIImage alloc] initWithCGImage: img.CGImage scale:1.0 orientation: UIImageOrientationUp];
     }else{
         img2 = [[UIImage alloc] initWithCGImage: img.CGImage scale:1.0 orientation: UIImageOrientationRight];
    }

     imageSelected.image = img2;
     [imageSelected setUserInteractionEnabled:YES];
     imageSelected.contentMode = UIViewContentModeScaleAspectFit;
     imageSelected.backgroundColor= [UIColor whiteColor];

     UITapGestureRecognizer *singleTap =  [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(exitAction:)];
     [singleTap setNumberOfTapsRequired:1];
     [imageSelected addGestureRecognizer:singleTap]; 

     UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipe:)];
     swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
     [imageSelected addGestureRecognizer:swipeLeft];

     UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self  action:@selector(leftSwipe:)];
     swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
     [imageSelected addGestureRecognizer:swipeRight];

 }

Swipe function:

 -(IBAction)leftSwipe:(id)sender{ NSLog(@"Left Swipe");    }

Thank you for advance.

Upvotes: 1

Views: 502

Answers (1)

amurcia
amurcia

Reputation: 791

The solution for me was:

Set a delegate for your gestures and return YES from gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

Upvotes: 1

Related Questions