2012-12-06 23 views
14

पर दो बार टैप करें मेरे पास एक बटन है और मैं उस पर नल का परीक्षण कर रहा हूं, एक टैप के साथ यह एक पृष्ठभूमि रंग बदलता है, जिसमें दो नल रंग होते हैं और तीन टैप एक और रंग फिर से होते हैं। कोड है:आईओएस - यूबूटन

- (IBAction) button { 
UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnce:)]; 
UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwice:)]; 
UITapGestureRecognizer *tapTrice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTrice:)]; 

tapOnce.numberOfTapsRequired = 1; 
tapTwice.numberOfTapsRequired = 2; 
tapTrice.numberOfTapsRequired = 3; 
//stops tapOnce from overriding tapTwice 
[tapOnce requireGestureRecognizerToFail:tapTwice]; 
[tapTwice requireGestureRecognizerToFail:tapTrice]; 

//then need to add the gesture recogniser to a view - this will be the view that recognises the gesture 
[self.view addGestureRecognizer:tapOnce]; 
[self.view addGestureRecognizer:tapTwice]; 
[self.view addGestureRecognizer:tapTrice]; 
} 

- (void)tapOnce:(UIGestureRecognizer *)gesture 
{ self.view.backgroundColor = [UIColor redColor]; } 

- (void)tapTwice:(UIGestureRecognizer *)gesture 
{self.view.backgroundColor = [UIColor blackColor];} 

- (void)tapTrice:(UIGestureRecognizer *)gesture 
{self.view.backgroundColor = [UIColor yellowColor]; } 

समस्या यह है कि पहले नल काम करता है नहीं है, अन्य हाँ। यदि मैं बटन के बिना इस कोड का उपयोग करता हूं तो यह पूरी तरह से काम करता है। धन्यवाद।

+0

क्या आप बटन टैप पर यह इशारा जोड़ रहे हैं? आप इसेडिडलोड में क्यों नहीं जोड़ते? – iDev

+0

क्योंकि मुझे केवल इस दृश्य का एक छोटा सा हिस्सा उपयोग करना चाहिए। – Kerberos

+0

लेकिन आपका कोड पूरे 'self.view' पर इशारा सेट कर रहा है। आपको इसके जवाब में दिखाए गए अनुसार इसे बदलना चाहिए। – iDev

उत्तर

17

यदि आप बटन को टैप करने के लिए रंग बदलना चाहते हैं, तो आपको बटन पर इन संकेतों को viewDidLoad विधि या बटन के बजाय बटन पर जोड़ना चाहिए। उपर्युक्त कोड बार-बार बटन के टैप पर self.view पर जेस्चर जोड़ देगा और button पर नहीं होगा।

- (void)viewDidLoad { 
     [super viewDidLoad]; 
     UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnce:)]; 
     UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwice:)]; 
     UITapGestureRecognizer *tapTrice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTrice:)]; 

     tapOnce.numberOfTapsRequired = 1; 
     tapTwice.numberOfTapsRequired = 2; 
     tapTrice.numberOfTapsRequired = 3; 
     //stops tapOnce from overriding tapTwice 
     [tapOnce requireGestureRecognizerToFail:tapTwice]; 
     [tapTwice requireGestureRecognizerToFail:tapTrice]; 

     //then need to add the gesture recogniser to a view - this will be the view that recognises the gesture 
     [self.button addGestureRecognizer:tapOnce]; //remove the other button action which calls method `button` 
     [self.button addGestureRecognizer:tapTwice]; 
     [self.button addGestureRecognizer:tapTrice]; 
}