2011-01-26 2 views
10

J'utilise le api shake comme ceci:Objectif C: détecter une secousse

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{ 
    if (event.subtype == UIEventSubtypeMotionShake) 
    { 
     [img stopAnimating];  
    } 
} 

Comment puis-je détecter que l'ébranlement a arrêté?

Répondre

22

Vous êtes sur la bonne voie, cependant, il y a encore plus de choses que vous devez ajouter à détecter secouer:

Vous pouvez tester cela en ajoutant un NSLog aux motionBegan ou motionEnded méthodes, et dans le simulateur, appuyez sur CONTROL + COMMAND + Z

#pragma mark - Shake Functions 

-(BOOL)canBecomeFirstResponder { 
    return YES; 
} 

-(void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:NO]; 
    [self becomeFirstResponder]; 
} 

-(void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:NO]; 
} 

-(void)viewDidDisappear:(BOOL)animated { 
    [self resignFirstResponder]; 
    [super viewDidDisappear:NO]; 
} 

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{ 
    if (motion == UIEventSubtypeMotionShake) 
    { 
     // shaking has began. 
    } 
} 


-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{ 
    if (motion == UIEventSubtypeMotionShake) 
    { 
     // shaking has ended 
    } 
} 
+1

Parfait! Merci beaucoup ... – itsame69

+0

canBecomeFirstResponder a du sens. –