首先,我将UISwipeGestureRecognizer附加到图像 View ,无法触发该操作方法。然后,我将UISwipeGestureRecognizer附加到 View Controller 的 View ,效果很好。我不知道为什么。
这是无效的代码

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

最佳答案

将此代码添加到imageView:

imageView.userInteractionEnabled = YES;

08-26 03:58