2011-09-20 8 views
11

मेरे दृश्य में UIPanGestureRecognizer जोड़ा गया है।
समस्या यह है कि जब तक स्पर्श पैन इशारा के रूप में पहचाना जाता है, इसे कुछ बिंदुओं को स्थानांतरित किया जाना चाहिए, और मैं UIGestureRecognizerStateBegan राज्य पर मूल स्पर्श स्थान निकालने नहीं कर सकता। इस स्थिति में translationInView: (0,0) है, और किसी भी बाद की चाल की गणना इस बिंदु से की जाती है, न कि मूल से।UIPanGestureRecognizer से मूल स्पर्श स्थान प्राप्त करें

क्या इशारा पहचानकर्ता से मूल स्थान निकालने का कोई तरीका है, या क्या मुझे touchesBegan: विधि को ओवरराइड करने की आवश्यकता है?

+0

इस कड़ी में मदद मिल सकती - http://www.icodeblog.com/2010/10/14/working-with-uigesturerecognizers/ – Coffee

उत्तर

7

आप इशारा recogniser के लिए delegate सेट करने में सक्षम और

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 

लागू प्रारंभिक स्पर्श पाने के लिए किया जाना चाहिए।

+2

यह काम करता है, भी। हालांकि, अंत में मैंने 'स्पर्श बेगन' का इस्तेमाल किया। ऐसा लगता है, पैन इशारा पहचानकर्ता वास्तव में एक सुराग नहीं है जहां स्पर्श घटना मूल रूप से शुरू हुई थी। – antalkerekes

1

यदि आप translationInView: के बजाय locationInView: का उपयोग करते हैं तो आपको सापेक्ष निर्देशांक के बजाय पूर्ण निर्देशांक प्राप्त होंगे। आप पैन शुरू करने के लिए इनपुट पाने के लिए ... इस आपके विचार हल करने के लिए एक UIButton हो सकता है की क्या ज़रूरत है और आप एक - (IBAction)buttonWasTouched:(UIButton *)button forEvent:(UIEvent *)event तो जैसे "touch down" से जुड़े ट्रिगर कर सकते हैं हालांकि:

-(IBAction)shakeIntensityPanGesturebuttonWasTouched:(UIButton *)button forEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; //touch location local cordinates 
} 
3

आप को लागू करने से इस समस्या का समाधान कर सकते हैं एक कस्टम PanGestureRecognizer जो मूल स्पर्श बिंदु बचाता है और इसे कॉलर के लिए उपलब्ध कराता है।

मैं इस मार्ग पर गया क्योंकि मैं पैन जेस्चर ट्रिगर करने के लिए स्थानांतरित दूरी को नियंत्रित करना चाहता था।

यह आपकी स्थिति के लिए अधिक हो सकता है, लेकिन पूरी तरह से काम करता है और यह लगता है उससे कम मुश्किल है।

संपर्क सूत्र निर्देशांक पाने के लिए आपको सिर्फ फोन:

[sender touchPointInView:yourView] 
बजाय

:

#import "PanGestureRecognizer.h" 
#import <UIKit/UIGestureRecognizerSubclass.h> 

@implementation PanGestureRecognizer 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesBegan:touches withEvent:event]; 
    UITouch *touch = [touches anyObject]; 

    // touchPoint is defined as: @property (assign,nonatomic) CGPoint touchPoint; 

    self.touchPoint = [touch locationInView:nil]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesMoved:touches withEvent:event]; 
    UITouch *touch = [touches anyObject]; 
    CGPoint p = [touch locationInView:nil]; 

    // customize the pan distance threshold 

    CGFloat dx = fabs(p.x-self.touchPoint.x); 
    CGFloat dy = fabs(p.y-self.touchPoint.y); 

    if (dx > 2 || dy > 2) { 
     if (self.state == UIGestureRecognizerStatePossible) { 
      [self setState:UIGestureRecognizerStateBegan]; 
     } 
     else { 
      [self setState:UIGestureRecognizerStateChanged]; 
     } 
    } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesEnded:touches withEvent:event]; 

    if (self.state == UIGestureRecognizerStateChanged) { 
     [self setState:UIGestureRecognizerStateEnded]; 
    } 
    else { 
     [self setState:UIGestureRecognizerStateCancelled]; 
    } 
} 

- (void) reset 
{ 
} 

// this returns the original touch point 

- (CGPoint) touchPointInView:(UIView *)view 
{ 
    CGPoint p = [view convertPoint:self.touchPoint fromView:nil]; 

    return p; 
} 

@end 
+0

मुझे समझ में नहीं आता कि इसका उपयोग कैसे किया जाना चाहिए। –

0

आप UIGestureRecognizerState

उपयोग कर सकते हैं:

[sender locationInView:yourView] 

यहाँ PanGestureRecognizer.m के लिए कोड है

- (void) onPanGesture:(UIPanGestureRecognizer *)sender { 
    if(sender.state == UIGestureRecognizerStateBegan) { 
     origin = [sender locationInView]; 
    } else { 
     current = [sender locationInView]; 
     // Initial touch stored in origin 
    } 
} 

स्विफ्ट (ish):

var state = recognizer.state 
if state == UIGestureRecognizerState.Began { 
    println(recognizer.locationInView(viewForGesture)) 
    //this will be the point of first touch 
} 
+0

कृपया अपने उत्तर के लिए कुछ प्रकार की स्पष्टीकरण प्रदान करें .. – Lal

+0

इशारा पहचानकर्ताओं के पास अलग-अलग राज्य हैं (.Began, .nded, .Cancelled, आदि ...)। अगर आपको पहचानकर्ता राज्य होने पर (देखें या स्पर्श करें) का स्थान मिलता है .बैगन आपके पास पहला स्थान होगा। – Aiden

+0

इस कोड को उस फ़ंक्शन के अंदर रखें जो इशारा द्वारा बुलाया जाता है।उपरोक्त println लाइन केवल एक समन्वय जोड़ी प्रति स्पर्श/पैन – Aiden