Gesture recognizer (swipe) on UIImageView

后端 未结 6 2073
栀梦
栀梦 2020-12-14 06:19

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

- (voi         


        
6条回答
  •  不思量自难忘°
    2020-12-14 06:59

    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");   
        } 
    
    }
    

提交回复
热议问题