手势识别器不适用于子视图

时间:2013-03-25 04:53:34

标签: iphone ios objective-c ipad cocoa-touch

首先,我将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 个答案:

答案 0 :(得分:25)

将此代码添加到imageView:

imageView.userInteractionEnabled = YES;

答案 1 :(得分:2)

试试这个 [imageView setUserInteractionEnabled:YES];