永远不会调用 UIGestureRecognizer 委托方法

UIGestureRecognizer Delegate method is never called

尝试在滚动视图的子视图中制作一些可拖动的图像。但是没有任何反应。有人知道为什么永远不会命中 handlePan 中的断点吗? :'( 如果您能提供帮助,谢谢!

-(void) viewWillAppear:(BOOL)animated
{
    twView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"twitter"]];
    [self.springView addSubview:twView];
    [twView setFrame:CGRectMake(100, 100, 60, 60)];

    fbView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"facebook"]];
    [self.springView addSubview:fbView];
    [fbView setFrame:CGRectMake(200, 200, 60, 60)];

    g1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [twView addGestureRecognizer:g1];
    g1.delegate = self;

    g2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [fbView addGestureRecognizer:g2];
    g2.delegate = self;

}

-(void)handlePan:(UIPanGestureRecognizer *)gesture
{
    ... do amazing things
}

根据您在此处获得的信息,这可能是两件事之一。您的图片视图没有 userInteractionEnabled 设置为是,或者您的图片视图之上有另一个视图阻止了手势。

UIScrollView 子类的内置平移手势识别器可能正在拦截手势。

确保您的视图控制器实现了 UIGestureRecognizerDelegate 协议并实现了

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES; 
}

另一件对我有帮助的事情是手势识别器集的实例

UITapGestureRecognizer *loginTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loginButtonAction)];
loginTap.delegate = self;
[self.loginButton setUserInteractionEnabled:YES];
[self.loginButton addGestureRecognizer:self.loginTap];

并且将委托分配给自己后,它将在委托方法中接收调用