Han Pengbo
Han Pengbo

Reputation: 1436

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];
}

Upvotes: 13

Views: 5602

Answers (2)

B.S.
B.S.

Reputation: 21726

Add this code to imageView:

imageView.userInteractionEnabled = YES;

Upvotes: 25

Nitin
Nitin

Reputation: 241

Try this one [imageView setUserInteractionEnabled:YES];

Upvotes: 2

Related Questions