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"); }
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.
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.
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.
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"); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With