2010-07-11 20 views
15

मैंने अपने व्यू कंट्रोलर के viewDidLoad पर एक UITapGestureRecognizer स्थापित किया है लेकिन किसी भी तरह से यह एक टैप के लिए चयनकर्ता विधि को दो बार निकाल देता है।UIGestureRecognizer दो बार फायरिंग?

UITapGestureRecognizer *g = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openInMapsApp:)] autorelease]; 
[self.mapView addGestureRecognizer:g]; 

मेरे विधि:

-(void)openInMapsApp:(UIGestureRecognizer*)g { 
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" 
                message:@"This will open this location in the Maps application. Continue?" 
                delegate:self 
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"OK",nil]; 
[alertView show]; 
[alertView release]; 
} 
+0

क्या यह संभव है कि यह केवल कुछ समय होगा? –

उत्तर

2

मैं दृश्य सुनिश्चित करने के लिए जाँच करता है कि करने के लिए एक टाइमर जोड़ा स्पर्श कम से कम आधा सेकंड पहले था और दूसरा स्पर्श की अनदेखी करता है, तो यह बहुत जल्दी है।

हालांकि यह केवल एक कामकाज है। मैं अभी भी असली समस्या को ठीक करना चाहता हूं।

4

मैं इसकी पुष्टि कर सकता हूं। मैंने इस मुद्दे का प्रदर्शन करने वाली एक नमूना परियोजना के साथ ऐप्पल को एक बग रिपोर्ट प्रस्तुत की।

मैंने पाया है कि अस्थायी कामकाज अलर्ट दिखाने से पहले UITapGestureRecognizer को तुरंत अक्षम करना है। फिर, UIAlertView प्रतिनिधि विधि में आप कार्यान्वित करते हैं, इसे फिर से सक्षम करें। इसके लिए आपको किसी भी तरह जीआर का ट्रैक रखने की आवश्यकता है, लेकिन यह समय के लिए सबसे सुंदर समाधान की तरह लगता है।

ऊपर नमूना कोड का उपयोग करना:

-(void)openInMapsApp:(UIGestureRecognizer*)g { 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" 
               message:@"This will open this location in the Maps application. Continue?" 
               delegate:self 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"OK",nil]; 
    g.enabled = NO; 
    self.activeGestureRecognizer = g; 
    [alertView show]; 
    [alertView release]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    self.activeGestureRecognizer.enabled = YES; 
    self.activeGestureRecognizer = nil; 
} 

- (void)alertViewCancel:(UIAlertView *)alertView { 
    self.activeGestureRecognizer.enabled = YES; 
    self.activeGestureRecognizer = nil; 
} 
+0

यह समाधान मेरे लिए भी काम करता है। क्या यह वास्तव में एक बग है? – mvexel

+0

मुझे एक ही समस्या एक एक्शन शीट थी। इस कामकाज ने इसे ठीक किया। धन्यवाद! –

+2

यह बग आईओएस एसडीके 4.2 के रूप में हल किया जाना चाहिए। मैं ऐप्पल से संकल्प की पुष्टि करने में सक्षम था। – gorbster

4

मैं एक ही समस्या थी, और मैंने देखा है दूसरी घटना (जबकि पहले UIGestureRecognizerStateEnded था) राज्य UIGestureRecognizerStateCancelled थी, इसलिए एक और वैकल्पिक हल में घटना की अनदेखी करने के लिए है उस मामले में।

-1

मैं इस किया था और काम किया:

पहली बार अपने से निपटने वस्तु पर एक सदस्य है कि पिछले कड़ी चोट संग्रहीत करता प्रारंभ NSTimeInterval

-(id)initWithResourcePath:(NSString*)path { 

    if ([super init]) { 
     //some code 


     lastSwipe = 0; 
    } 
    return self; 

} 

फिर अपने विधि

-(void)viewDetectedRightSwipe:(UISwipeGestureRecognizer*)recognizer { 


//touch being registered  
NSTimeInterval thisTouch = [NSDate timeIntervalSinceReferenceDate]; 

//if the last swipe is the first swipe, then there's nothing to do but the handling of a correct gesture 
if(lastSwipe !=0) { 
    // find out how much time has passed between gestures 
    NSTimeInterval timeSinceLastTouch = thisTouch -lastSwipe ; 

    NSLog(@"thisTouch = %f",thisTouch); 
    NSLog(@"timeSinceLastTouch = %f",timeSinceLastTouch); 
    if (timeSinceLastTouch<0.2f) { 
     //return if this is an invalid touch 
     return; 
    } else { 
     // register the timestamp if it is valid 
     lastSwipe = thisTouch; 
     NSLog(@"left"); 
    } 

} 
// invalid gestures won't be registed because they won't get to this portion of code. 
lastSwipe = thisTouch; 
NSLog(@"left"); 
// rest of the handling code 
} 
19

इशारा करने के लिए इस कोड को जोड़ना पहचानकर्ता विभिन्न इशारा राज्य के साथ कार्रवाई भेजते हैं। तो यह एक बग नहीं है। समाधान नहीं है:

UITapGestureRecognizer *g = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openInMapsApp:)] autorelease]; 
[self.mapView addGestureRecognizer:g]; 

-(void)openInMapsApp:(UIGestureRecognizer*)g { 
if(g.state != UIGestureRecognizerStateRecognized) 
    return; 
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" 
                message:@"This will open this location in the Maps application. Continue?" 
                delegate:self 
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"OK",nil]; 
[alertView show]; 
[alertView release]; 
} 
5

मैं हो रही थी एक डबल UIAlertView आते हैं। जैसा कि उपरोक्त निकोलस रोस्तोव द्वारा दिखाया गया है, यह मेरे लिए काम करता है। UIGestureRecognizerStateEnded और UIGestureRecognizerStateRecognized दोनों राज्यों ने एक नई चेतावनी बनाई जब [alertView show] ब्लॉक में उपयोग किया गया था। // [चेतावनी दृश्य शो] के साथ टिप्पणी की, दोनों अभी भी कंसोल पर दिखाई दिए, लेकिन केवल एक ही कार्रवाई हुई।

-(void) handleTapGesture:(UIGestureRecognizer*)sender{ 
    if(sender.state != UIGestureRecognizerStateRecognized) 
    return; 
1
if(g.state != UIGestureRecognizerStateBegan) 
//instead of if(g.state != UIGestureRecognizerStateRecognized) 
return; 

यह इशारा घटना के तुरंत बाद alertview आह्वान करेंगे। उदाहरण के लिए: यदि आप लांगटैप ईवेंट का उपयोग करते हैं तो एक बार जब आप अपनी अंगुली बंद कर लेंगे तो अलर्टव्यू प्रदर्शित किया जाएगा। लेकिन अगर इशारा स्वचालित रूप से पहचाना जाता है तो UIGestureRecognizerStateBegan स्थिति स्वचालित रूप से लागू की जाएगी।

3

मैंने अभी भी एक ही समस्या का समाधान किया है। यह पता चला कि मेरे पास दो टैप इशारा पहचानकर्ताओं को उसी कॉल के साथ दृश्य में जोड़ा गया था। जिस तरह से मुझे पता चला कि चयनकर्ता कहलाता है जब इशारा पहचानकर्ता NSLogging है।

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

NSLog(@"recognizer: %@",[gestureRecognizer description]); 
+0

महान टिप। tnxx – user1105951

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