18

मुझे अपने स्वाइप इशारे की दिशा का पता लगाने की आवश्यकता है और मुझे इसके साथ समस्या है। इशारा काम कर रहा है, लेकिन मुझे नहीं पता कि दिशा का पता कैसे लगाया जाए। ...कैसे स्वाइप जेश्चर दिशा का पता लगाने?

swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(detectSwipe:)]; 
[swipeGesture setNumberOfTouchesRequired:1]; 
[swipeGesture setDirection:UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp]; 
[appView addGestureRecognizer:swipeGesture]; 

-(void)detectSwipe:(UISwipeGestureRecognizer *)recognizer { 
switch (recognizer.direction) { 
    case UISwipeGestureRecognizerDirectionUp: 
     NSLog(@"smth1"); 
     break; 


    case UISwipeGestureRecognizerDirectionDown: 
     NSLog(@"smth2"); 
    default: 
     break; 
} 
} 

यह काम नहीं कर रहा:/

+0

कृपया परिभाषित क्या गलत मान लॉग है? क्या यह कुछ भी लॉग नहीं करता है? पता लगाया जा रहा है स्वाद नहीं कहा जा रहा है? – sosborn

+0

'डिफ़ॉल्ट' केस को तब कहा जाता है जब मैं ऊपर या नीचे स्वाइप करता हूं। –

+0

चूंकि यह सिर्फ एक enum है - क्या आपने पहचानकर्ता के मूल्य को डालने और लॉग करने का प्रयास किया है: http://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UISwipeGestureRecognizer_Class/Reference/Reference.html – bryanmac

उत्तर

20

direction संपत्ति केवल परिभाषित करता है अनुमति दिशाओं कि स्वाइप के रूप में पहचाने जाते हैं, एक विशेष कड़ी चोट की नहीं वास्तविक दिशा।

इसके बजाय दो अलग इशारा पहचानकर्ताओं का उपयोग करना सबसे आसान होगा। जब इशारा शुरू होता है और जब यह locationInView: विधि के साथ समाप्त होता है तो आप स्पर्श के स्थान का भी निरीक्षण कर सकते हैं।

+0

'CGPoint start = [swipeGesture locationInView: appView]; 'मैं इसका उपयोग करता हूं और यह फ़ंक्शन मुझे स्वाइप का प्रारंभ बिंदु देता है, लेकिन स्वाइप समाप्त होने पर मुझे कैसा पता चलता है? एकमात्र तरीका 'स्पर्शों और' स्पर्श 'का उपयोग करना है? –

+1

मुझे यकीन नहीं है कि यह वास्तव में संभव है (आप शायद दो पहचानकर्ताओं के साथ बेहतर हो)। अपनी कार्रवाई में इशारा पहचानकर्ता के 'राज्य' की जांच करें। अधिकांश इशारा पहचानकर्ताओं के लिए, यह 'UIGestureRecognizerStateBegan' से' UIGestureRecognizerStateEnded' 'में संक्रमण होता है, लेकिन यह हो सकता है कि स्वाइप जेश्चर पहचानकर्ता प्रकार ऐसा नहीं करता है, मैंने कोशिश नहीं की है। – omz

+1

हल हो गया। मैं 'touchesBegan' और' touchesEnded' का उपयोग करता हूं। –

11

विस्तार OMZ के समाधान:

self.myView दृश्य मैं पर इशारा पहचानकर्ता रखना चाहते है। नीचे दिया गया कोड परीक्षण नहीं किया गया है, मुझे लगता है कि पहचानकर्ताओं को property एस के रूप में रखना बेहतर होगा और उन्हें viewDidLoad() या xib फ़ाइल के अंदर जोड़ें। self एक UIViewController है।

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedLeft:)]; 
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft ]; 
[self.view addGestureRecognizer:swipeLeft]; 

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedRight:)]; 
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight ]; 
[self.view addGestureRecognizer:swipeRight]; 

अपने UIViewController करने के लिए उन दो विधियों जोड़ें और जोड़ने के लिए आवश्यक कार्रवाई:

- (IBAction)swipedRight:(UISwipeGestureRecognizer *)recognizer 
{ 
    NSLog(@"swiped right"); 
} 

- (IBAction)swipedLeft:(UISwipeGestureRecognizer *)recognizer 
{ 
    NSLog(@"swiped left"); 
} 
47

यहाँ मेरी परियोजनाओं में से एक से एक उदाहरण है: "। यह काम नहीं कर रहा"

// ... 

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

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

    UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)]; 
    swipeUp.direction = UISwipeGestureRecognizerDirectionUp; 
    [self.view addGestureRecognizer:swipeUp]; 

    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)]; 
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown; 
    [self.view addGestureRecognizer:swipeDown]; 

    // ... 

- (void)didSwipe:(UISwipeGestureRecognizer*)swipe{ 

    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) { 
     NSLog(@"Swipe Left"); 
    } else if (swipe.direction == UISwipeGestureRecognizerDirectionRight) { 
     NSLog(@"Swipe Right"); 
    } else if (swipe.direction == UISwipeGestureRecognizerDirectionUp) { 
     NSLog(@"Swipe Up"); 
    } else if (swipe.direction == UISwipeGestureRecognizerDirectionDown) { 
     NSLog(@"Swipe Down"); 
    } 
} 
+0

यह मेरे लिए सही समाधान –

+0

पर काम कर रहा है मुझे लगता है कि आपको 4 इशारा पहचानकर्ता जोड़ने की आवश्यकता नहीं है, बस एक जोड़ें और डीडस्वाइप विधि में प्रेषक की स्वाइप दिशा की जांच करें, जो इसे करना चाहिए –

संबंधित मुद्दे