手势识别器在子视图上无法工作

13

起初,我将UISwipeGestureRecognizer附加到一个图片视图上,但是动作方法无法触发。然后我将UISwipeGestureRecognizer附加到视图控制器的视图上,它可以正常工作。我不知道原因。

这里是不起作用的代码:
 - (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];
}

这里是运行良好的代码

- (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];
}
2个回答

25

将此代码添加到imageView中:

imageView.userInteractionEnabled = YES;

2

尝试这个: [imageView setUserInteractionEnabled:YES];


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接