一千萬個為什麽

搜索

Iphone SDK中的同步手勢識別器

我需要使用 UISwipeGestureRecognizer (例如, UISwipeGestureRecognizerDirectionRightUISwipeGestureRecognizerDirectionLeft )捕獲兩個不同的翻轉手勢。當我使用 addGestureRecognizer 方法添加兩個不同的識別器時,只有最後添加的識別器有效。我已經讀過我需要實現 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizerDelegate 協議的方法,但沒有任何作用。

任何人都可以幫助捕獲兩個或多個相同的手勢的簡單示例? 謝謝!

最佳答案

這真的很容易:

首先我們應該創建實現 UIGestureRecognizerDelegate 協議的類:

@interface MyGestureDelegate : NSObject 

@implementation MyGestureDelegate

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

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

並像這樣使用它:


    UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc]
                                              initWithTarget:self action:@selector(handleSwipeGestureLeft:)];
    [self.view addGestureRecognizer:swipeGestureLeft];
    swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [swipeGestureLeft release];

    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]
                                          initWithTarget:self action:@selector(handleSwipeGesture:)];
    swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipeGesture];

    MyGestureDelegate *deleg = [[MyGestureDelegate alloc] init];

    [swipeGesture setDelegate:deleg];
    [swipeGesture release];

轉載註明原文: Iphone SDK中的同步手勢識別器