按钮上的手势识别器

时间:2011-05-26 08:05:27

标签: iphone objective-c xcode uigesturerecognizer gesture-recognition

我想为按钮实现手势识别器(滑动动作)。问题是,按钮是以编程方式创建的,并且根据一些条件存在或不存在。所以,我不知道是否有按钮,或者有多少按钮。

我知道我需要这样的东西:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (touch.view == aButtonView) {
        //get the button's tag
    }
}

当然,当按下任何按钮视图时,if语句应该返回Yes ...

任何人都知道aButtonView这个词应该是什么?或者,如果它甚至可能?提前谢谢。

1 个答案:

答案 0 :(得分:8)

您应该考虑使用UISwipeGestureRecognizer个实例。将手势识别器附加到按钮对象 -

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                                            action:@selector(handleSwipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionUp;
[button addGestureRecognizer:swipe];
[swipe release];

并在handleSwipe:

- (void) handleSwipe:(UISwipeGestureRecognizer *)swipe {
    NSInteger tag = swipe.view.tag;
}

<小时/> 它应该是if ( [gestureRecognizer.view isKindOfClass:[UIButton class]] ) {