0

我有一個UIScrollView內的子視圖。在SubView中,我有一個帶有多個按鈕的圖像。我有兩個手指滑動手勢編碼,以便用戶可以向上,向下,向左或向右滑動以在導航控制器上彈出另一個視圖。當視圖加載時,我可以輕鬆刷卡並按預期執行。但是,當我放大或全部放大時,忽略兩個手指輕掃手勢。如果我很幸運,有時它會在放大/縮小時被識別,但我不能允許我的用戶使用該行爲。任何人都可以幫忙嗎?謝謝。SwipeGesture放大時不工作

+0

你能分享你已經試過的代碼嗎? –

回答

0

我能夠使用手勢識別器委託方法requireGestureRecognizerToFail計算出來。我需要兩個手指在各個方向上滑動。我首先創建了一個平移手勢識別器,並設置適當的最小和最大觸摸量。您必須將平移手勢的代表設置爲self,並且確保您將requireGestureRecognizerToFail設置爲每個滑動手勢。不要忘記在shouldRecognizeSimultaneouslyWithGestureRecognizer中傳遞滑動手勢集合。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    scrollView.contentSize = CGSizeMake(image.frame.size.width, image.frame.size.height); 
    scrollView.maximumZoomScale = 4; 
    scrollView.minimumZoomScale = 1; 
    scrollView.delegate = self; 

    UIPanGestureRecognizer *TwoFingerPanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(HandlePanGesture:)]; 
    TwoFingerPanGesture.minimumNumberOfTouches=2; 
    TwoFingerPanGesture.maximumNumberOfTouches=2; 
    [TwoFingerPanGesture setDelegate:self]; 
    [scrollView addGestureRecognizer:TwoFingerPanGesture]; 

    UISwipeGestureRecognizer *TwoFingerSwipeDownGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(HandleSwipeGesture:)]; 
    TwoFingerSwipeDownGesture.direction = UISwipeGestureRecognizerDirectionDown; 
    TwoFingerSwipeDownGesture.numberOfTouchesRequired = 2 ; 
    [TwoFingerPanGesture requireGestureRecognizerToFail:TwoFingerSwipeDownGesture]; 
    [scrollView addGestureRecognizer:TwoFingerSwipeDownGesture]; 

    UISwipeGestureRecognizer *TwoFingerSwipeUpGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(HandleSwipeGesture:)]; 
    TwoFingerSwipeUpGesture.direction = UISwipeGestureRecognizerDirectionUp; 
    TwoFingerSwipeUpGesture.numberOfTouchesRequired = 2; 
    [TwoFingerPanGesture requireGestureRecognizerToFail:TwoFingerSwipeUpGesture]; 
    [scrollView addGestureRecognizer:TwoFingerSwipeUpGesture]; 

    UISwipeGestureRecognizer *TwoFingerSwipeLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(HandleSwipeGesture:)]; 
    TwoFingerSwipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft; 
    TwoFingerSwipeLeftGesture.numberOfTouchesRequired = 2; 
    [TwoFingerPanGesture requireGestureRecognizerToFail:TwoFingerSwipeLeftGesture]; 
    [scrollView addGestureRecognizer:TwoFingerSwipeLeftGesture]; 

    UISwipeGestureRecognizer *TwoFingerSwipeRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(HandleSwipeGesture:)]; 
    TwoFingerSwipeRightGesture.direction = UISwipeGestureRecognizerDirectionRight; 
    TwoFingerSwipeRightGesture.numberOfTouchesRequired = 2; 
    [TwoFingerPanGesture requireGestureRecognizerToFail:TwoFingerSwipeRightGesture]; 
    [scrollView addGestureRecognizer:TwoFingerSwipeRightGesture]; 
} 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
{ 
    // Either just return YES to allow your cell's gesture recognizer 
    // to work simultaneously with all other recognizers: 
    //return YES; 

    // Or you can decide whether your cell's pan gesture recognizer should 
    // recognize simultaneously with otherGestureRecognizer. For example, 
    // you could get a reference to your collection view's panGestureRecognizer 
    // and only return YES if otherGestureRecognizer is equal to that recognizer: 
    return otherGestureRecognizer == scrollView.gestureRecognizers; 
}