2011-01-26 13 views
10

estoy usando la API de batido de la siguiente manera:C Objetivo: Detección de una sacudida

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

¿Cómo puedo detectar que pase el temblor?

Respuesta

22

Está en el camino correcto, sin embargo, todavía hay más cosas que hay que añadir para detectar sacudidas:

Esto se comprueba mediante la adición de un NSLog a los métodos motionBegan o motionEnded, y en el simulador, presione 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

¡Perfecto! Muchas gracias ... – itsame69

+0

canBecomeFirstResponder tiene sentido. –