23

你會如何設置的手勢識別,讓你可以有一個UISwipeGestureRecognizer,並在同一時間UIPanGestureRecognizer工作?這樣,如果您輕觸並快速移動(快速輕掃),則會將其檢測爲輕掃,但如果您觸摸然後移動(觸摸&移動之間的短延遲),它會將其檢測爲平底鍋?如何對同一視圖UISwipeGestureRecognizer和UIPanGestureRecognizer工作

我已經試過requireGestureRecognizerToFail的各種排列並沒有完全幫助,它使人們因此,如果SwipeGesture留下那麼我的移動手勢將努力向上,向下和向右卻留下任何運動被檢測輕掃手勢。

回答

51

你將要設置兩個UIGestureRecognizer的代表之一的對象,是有道理的(可能是self),然後聽着,併爲此method返回YES

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

這種方法當通過gestureRecognizerotherGestureRecognizer識別手勢將阻止其他手勢識別器識別其手勢時調用。請注意,返回YES保證允許同時識別;另一方面,返回NO不能保證防止同時識別,因爲其他手勢識別器的代表可能會返回YES

+0

像一個魅力工作。感謝很多 – 2012-09-28 12:48:40

+0

謝謝,幫了很多! – Mateus 2013-09-08 01:22:13

5

Using a pan recognizer to detect swipping and panning

- (void)setupRecognizer 
{ 
    UIPanGestureRecognizer* panSwipeRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanSwipe:)]; 
    // Here you can customize for example the minimum and maximum number of fingers required 
    panSwipeRecognizer.minimumNumberOfTouches = 2; 
    [targetView addGestureRecognizer:panSwipeRecognizer]; 
} 

#define SWIPE_UP_THRESHOLD -1000.0f 
#define SWIPE_DOWN_THRESHOLD 1000.0f 
#define SWIPE_LEFT_THRESHOLD -1000.0f 
#define SWIPE_RIGHT_THRESHOLD 1000.0f 

- (void)handlePanSwipe:(UIPanGestureRecognizer*)recognizer 
{ 
    // Get the translation in the view 
    CGPoint t = [recognizer translationInView:recognizer.view]; 
    [recognizer setTranslation:CGPointZero inView:recognizer.view]; 

    // TODO: Here, you should translate your target view using this translation 
    someView.center = CGPointMake(someView.center.x + t.x, someView.center.y + t.y); 

    // But also, detect the swipe gesture 
    if (recognizer.state == UIGestureRecognizerStateEnded) 
    { 
     CGPoint vel = [recognizer velocityInView:recognizer.view]; 

     if (vel.x < SWIPE_LEFT_THRESHOLD) 
     { 
      // TODO: Detected a swipe to the left 
     } 
     else if (vel.x > SWIPE_RIGHT_THRESHOLD) 
     { 
      // TODO: Detected a swipe to the right 
     } 
     else if (vel.y < SWIPE_UP_THRESHOLD) 
     { 
      // TODO: Detected a swipe up 
     } 
     else if (vel.y > SWIPE_DOWN_THRESHOLD) 
     { 
      // TODO: Detected a swipe down 
     } 
     else 
     { 
      // TODO: 
      // Here, the user lifted the finger/fingers but didn't swipe. 
      // If you need you can implement a snapping behaviour, where based on the location of your   targetView, 
      // you focus back on the targetView or on some next view. 
      // It's your call 
     } 
    } 
} 
+0

美麗!爲我工作得很好! – 2016-01-31 23:09:16

2

默認情況下,當用戶試圖刷卡,手勢被解釋爲一個盤中。這是因爲一個滑動手勢符合必要的條件,在它滿足必要的條件以解釋爲滑動(離散手勢)之前,將其解釋爲平移(連續手勢)。

你需要通過調用requireGestureRecognizerToFail表示兩個手勢識別之間的關係:方法上要推遲

[self.panRecognizer requireGestureRecognizerToFail:self.swipeRecognizer]; 
0

下面是用於檢測平移和滑動方向(利用2cupsOfTech的完整的解決方案手勢識別swipeThreshold邏輯):

public enum PanSwipeDirection: Int { 
    case up, down, left, right, upSwipe, downSwipe, leftSwipe, rightSwipe 
    public var isSwipe: Bool { return [.upSwipe, .downSwipe, .leftSwipe, .rightSwipe].contains(self) } 
    public var isVertical: Bool { return [.up, .down, .upSwipe, .downSwipe].contains(self) } 
    public var isHorizontal: Bool { return !isVertical } 
} 

public extension UIPanGestureRecognizer { 

    public var direction: PanSwipeDirection? { 
     let SwipeThreshold: CGFloat = 1000 
     let velocity = self.velocity(in: view) 
     let isVertical = fabs(velocity.y) > fabs(velocity.x) 
     switch (isVertical, velocity.x, velocity.y) { 
     case (true, _, let y) where y < 0: return y < -SwipeThreshold ? .upSwipe : .up 
     case (true, _, let y) where y > 0: return y > SwipeThreshold ? .downSwipe : .down 
     case (false, let x, _) where x > 0: return x > SwipeThreshold ? .rightSwipe : .right 
     case (false, let x, _) where x < 0: return x < -SwipeThreshold ? .leftSwipe : .left 
     default: return nil 
     } 
    } 

} 

用法:

@IBAction func handlePanOrSwipe(recognizer: UIPanGestureRecognizer) { 

    if let direction = recognizer.direction { 
     if direction == .leftSwipe { 
      //swiped left 
     } else if direction == .up { 
      //panned up 
     } else if direction.isVertical && direction.isSwipe { 
      //swiped vertically 
     } 
    } 

} 
相關問題