Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gesture recognizer (swipe) on UIImageView

I am trying to NSLog when I swipe over an UIImageView with this code, but it does not work for some reason. Any idea ?

@implementation ViewController  - (void)viewDidLoad {     [super viewDidLoad];      UIImage *image = [UIImage imageNamed:@"image2.png"];     UIImageView *imageView = [[UIImageView alloc] initWithImage:image];      imageView.frame = [[UIScreen mainScreen] bounds];      [self.view addSubview:imageView];      UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];     [recognizer setNumberOfTouchesRequired:1];     [imageView addGestureRecognizer:recognizer];  }  - (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer {     NSLog(@"right swipe"); } 
like image 237
Sucrenoir Avatar asked Jul 29 '13 15:07

Sucrenoir


People also ask

How do I add swipe gestures in Swift?

We can use the createSwipeGestureRecognizer(for:) method in the view controller's viewDidLoad() method to create a swipe gesture recognizer for each direction. We pass the result of createSwipeGestureRecognizer(for:) to the addGestureRecognizer(_:) method.

How do I add tap gestures in Objective C?

Adding a Tap Gesture Recognizer in Interface Builder You don't need to switch between the code editor and Interface Builder. Open Main. storyboard and drag a tap gesture recognizer from the Object Library and drop it onto the view we added earlier. The tap gesture recognizer appears in the Document Outline on the left.

How do I use UITapGestureRecognizer?

The iOS UITapGestureRecognizer class has a built-in way to detect a double tap on any view. All you need to do is create the recognizer, set its numberOfTapsRequired property to 2, then add it to the view you want to monitor.


1 Answers

Enable UIImage view user interaction which is disabled by default.

[imageView setUserInteractionEnabled:YES]; 

Adding a Swipe Gesture Events

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];  // Setting the swipe direction. [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];  // Adding the swipe gesture on image view [imageView addGestureRecognizer:swipeLeft]; [imageView addGestureRecognizer:swipeRight]; 

Handling Swipe Gesture Events

- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {      if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {         NSLog(@"Left Swipe");     }      if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {         NSLog(@"Right Swipe");        }   } 
like image 155
icodebuster Avatar answered Sep 21 '22 15:09

icodebuster