2013-05-17 15 views
9

Desidero eseguire un determinato metodo e ripetere se stesso fino a quando il dito di qualcuno viene premuto su un pulsante. Voglio che quel metodo smetta di ripetersi da solo quando il dito non è più sul pulsanteUIButton premere e tenere premuto - ripetere l'azione fino al rilascio

C'è un modo per verificare se il touchDown si sta ancora verificando durante l'implementazione del metodo? Aiuto!

+2

possibile duplicato di [modo per fare un UIButton continuo fuoco durante una situazione di stampa-and-hold?] (http://stackoverflow.com/questions/9 03114/way-to-make-a-uibutton-continuous-fire-during-a-press-and-hold-situation) – rmaddy

+0

ha funzionato in questo modo! grazie. Non l'ho capito completamente quando l'ho letto prima di postare questa domanda - la seconda volta è un incantesimo. – dokun1

risposta

12

È possibile utilizzare l'evento di controllo UIControlEventTouchDown per avviare il metodo in esecuzione e UIControlEventTouchUpInside o simile per rilevare quando il pulsante non viene più "premuto".

impostare le azioni al pulsante, ad esempio:

[myButton addTarget:self action:@selector(startButtonTouch:) forControlEvents:UIControlEventTouchDown]; 
[myButton addTarget:self action:@selector(endButtonTouch:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside]; 

(. Si noti la tocco sopra causerà fino dentro e fuori il pulsante per richiamare il metodo endButtonTouch:)

quindi aggiungere il startButtonTouch: e endButtonTouch metodi, ad esempio,:

- (void)startButtonTouch:(id)sender { 
    // start the process running... 
} 

- (void)endButtonTouch:(id)sender { 
// stop the running process... 
} 
+0

Questo approccio non funziona se l'utente tocca il pulsante e sposta il dito fuori dai limiti del pulsante. – danypata

+1

Usa sia 'UIControlEventTouchUpInside' e' UIControlEventTouchUpOutside'. Aggiunto alla risposta. Se vuoi terminarlo quando l'utente trascina fuori dal pulsante, aggiungi anche "UIControlEventTouchDragExit'. – bobnoble

0

attingendo la risposta del bobnoble ecco una view helper

#import <UIKit/UIKit.h> 

@interface YOIDCAutorepeatingButton : UIButton 

// you COULD pinch pennies switching to nonatomic, but consider 
// how much time it would take to debug if some day some moron decides without checking this spec 
// to alter this prop off another thread 
@property (atomic) NSTimeInterval delayUntilAutorepeatBegins; 
@property NSTimeInterval delayBetweenPresses; 

@property (weak) id<NSObject> recipient; 
@property SEL touchActionOnRecipient; 

@end 

-------------> .m

#import "YOIDCAutorepeatingButton.h" 

@interface YOIDCAutorepeatingButton() 
{ 
NSTimeInterval _pressStartedAt; 
} 

@end 

@implementation YOIDCAutorepeatingButton 


- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
self = [super initWithCoder:aDecoder]; 
if (self) { 
    [self addTarget:self action:@selector(startButtonTouch:) forControlEvents:UIControlEventTouchDown]; 
    [self addTarget:self action:@selector(endButtonTouch:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside]; 

    self.delayUntilAutorepeatBegins = .250; 
    self.delayBetweenPresses = .080; 
} 
return self; 
} 

-(void)killLastCharacter:(id)sender 
{ 
[self.recipient performSelector:self.touchActionOnRecipient withObject:sender]; 
} 

- (void)performAutorepeat:(id)sender 
{ 
if(!self.delayBetweenPresses) { 
    // bail 
    return; 
} 
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.delayBetweenPresses * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
    if(_pressStartedAt) { 
     [self killLastCharacter:sender]; 
     [self performAutorepeat:sender]; 
    } 
}); 
} 

- (void)startButtonTouch:(id)sender { 

[self killLastCharacter:sender]; 
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate]; 
_pressStartedAt = now; 
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.delayUntilAutorepeatBegins * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
    if(_pressStartedAt) { 
     [self killLastCharacter:sender]; 
     [self performAutorepeat:sender]; 
    } 
}); 
} 

- (void)endButtonTouch:(id)sender { 
_pressStartedAt = 0; 
} 

@end 

esempio dell'uso -------- -------

- (IBAction)killLastDigit:(id)sender { 

.....

- (void)viewDidLoad 
{ 
assert(self.backSpace); 
[YOIDCAutorepeatingButton class]; // if xib is in a bundle other than main gottal load the class 
// otherwise you'd get -[UIButton setRecipient:]: unrecognized selector sent to instance 
// on setRecipient: 
self.backSpace.recipient = self; 
self.backSpace.touchActionOnRecipient = @selector(killLastDigit:); 
Problemi correlati