0

我有几个 IBAction 附加到 UIButtons。IBActions 工作正常,直到我添加以下代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"View Did Load");
    [self addGestureRecognizersToView:drawImage];
}

在我添加那段代码后,IBActions 不会触发。当我触摸 UIButtons 时,它们会突出显示,但没有任何 IBAction 代码被命中。

这是我的 addGestureRecognizers 代码:

- (void)addGestureRecognizersToView:(UIImageView *)theView {
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [panGesture setMaximumNumberOfTouches:2];
    [panGesture setMinimumNumberOfTouches:1];
    //panGesture.delegate = drawImage;
    [theView addGestureRecognizer:panGesture];
    [panGesture release];

    UITapGestureRecognizer *doubleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    [doubleFingerTap setNumberOfTapsRequired:2];
    [self.view addGestureRecognizer:doubleFingerTap];
    [doubleFingerTap release];

    UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [singleFingerTap setNumberOfTapsRequired:1];
    [self.view addGestureRecognizer:singleFingerTap];
    [singleFingerTap release];
}

如果我注释掉 singleFingerTap 代码,它就可以工作。我猜我不应该使用 alloc,因为我之前已经在 doubleFingerTap 中分配过一次?

关于我可能在这里遗漏的任何想法?

4

2 回答 2

1

您的单指点击阻碍了按钮的正常行为。您必须确保触摸畅通无阻。

[singleFingerTap setCancelsTouchesInView:NO];
于 2011-05-23T05:43:03.633 回答
0

听起来 UITapGestureRecognizer 正在拦截原本由 UIButtons 处理的点击。

您可以使用gestureRecognizer:shouldReceiveTouch:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    return (touch.view != self.button1 && 
            touch.view != self.button2);
}

或者,您可以破解响应者链

于 2011-05-23T04:43:23.077 回答