Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gesture recognizer doesn't work on subview

At first,I attach UISwipeGestureRecognizer to a image view,the action method can't be triggered.Then I attach UISwipeGestureRecognizer to view controller's view,it works well.I don't know why. here is the code that doesn't work

 - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UISwipeGestureRecognizer *swipeRight=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction)];
    swipeRight.direction=UISwipeGestureRecognizerDirectionRight;
    //imageView is an outlet of image view
    [imageView addGestureRecognizer:swipeRight];
}

here is the code that works well

- (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.

    UISwipeGestureRecognizer *swipeRight=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction)];
    swipeRight.direction=UISwipeGestureRecognizerDirectionRight;
    //imageView is an outlet of image view
    [self.view addGestureRecognizer:swipeRight];
}
like image 343
Han Pengbo Avatar asked Mar 25 '13 04:03

Han Pengbo


2 Answers

Add this code to imageView:

imageView.userInteractionEnabled = YES;
like image 104
B.S. Avatar answered Nov 14 '22 20:11

B.S.


Try this one [imageView setUserInteractionEnabled:YES];

like image 22
Nitin Avatar answered Nov 14 '22 19:11

Nitin