23

कैसे आप सेटअप इशारा recognizers, ताकि आप एक UISwipeGestureRecognizer और एक UIPanGestureRecognizer एक ही समय में काम हो सकता था होता है करने के लिए? ऐसा है कि यदि आप जल्दी से स्पर्श करते हैं और तेज़ी से आगे बढ़ते हैं (त्वरित स्वाइप) यह एक स्वाइप के रूप में इशारा करता है लेकिन यदि आप स्पर्श करते हैं तो स्पर्श करें (& चाल के बीच छोटी देरी) यह इसे पैन के रूप में पहचानता है?कैसे एक ही दृश्य पर एक UISwipeGestureRecognizer और UIPanGestureRecognizer काम

मैं requireGestureRecognizerToFail के विभिन्न क्रमपरिवर्तन की कोशिश की है और कहा कि वास्तव में मदद नहीं की, यह इतना है कि अगर SwipeGesture तो छोड़ दिया गया था मेरी पैन इशारा ऊपर, नीचे और सही काम करेगा बनाया, लेकिन किसी भी आंदोलन से पता चला था छोड़ दिया स्वाइप इशारा।

उत्तर

51

आप एक वस्तु है कि भावना (संभावना self) बनाता है करने के लिए दो UIGestureRecognizer के प्रतिनिधियों में से एक स्थापित करने के लिए तो सुनो, और इस method के लिए YES वापसी चाहते करने जा रहे हैं:

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

इस विधि है जिसे gestureRecognizer या otherGestureRecognizer द्वारा किसी इशारे की मान्यता के समय कहा जाता है, अन्य इशारा पहचानकर्ता को इसके इशारे को पहचानने से रोक देगा। ध्यान दें कि YES लौटने पर एक साथ मान्यता की अनुमति है; दूसरी ओर, NO लौटने पर, एक साथ मान्यता को रोकने की गारंटी नहीं है क्योंकि अन्य इशारा पहचानकर्ता का प्रतिनिधि YES लौटा सकता है।

+0

एक आकर्षण की तरह काम किया। बहुत सारे –

+0

धन्यवाद, बहुत मदद की! – Mateus

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

सुंदर! मेरे लिए अच्छा काम किया! –

2

डिफ़ॉल्ट रूप से, जब उपयोगकर्ता स्वाइप करने के लिए प्रयास करता है, इशारा एक पैन के रूप में व्याख्या की है। इसका कारण यह है कि एक स्वाइपिंग इशारा एक आवश्यक पैन (एक निरंतर इशारा) के रूप में व्याख्या करने के लिए आवश्यक शर्तों को पूरा करता है, इससे पहले कि वह एक स्वाइप (एक पृथक इशारा) के रूप में व्याख्या की जाने वाली आवश्यक शर्तों को पूरा करता है। इशारा पहचानकर्ता है कि आप में देरी करने के

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

यहाँ पैन और कड़ी चोट दिशाओं (पता लगाने के उपयोग के लिए 2cupsOfTech के एक पूर्ण समाधान है चाहता हूँ पर विधि:

आप requireGestureRecognizerToFail फोन करके दो इशारा recognizers के बीच एक रिश्ता इंगित करने के लिए की जरूरत है 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 
     } 
    } 

}