usar trucos sabias que funciones cosas como iphone iphone-sdk-3.0 delegates shake

sabias - trucos iphone xr



cómo detectar y programar batidos para el iphone (2)

Estoy tratando de implementar el "tutorial" de sacudidas en esta página, pero creo que me falta algo. Copié su función de acelerómetro en myAppViewController.m file y puse algunos nslogs allí para ver si incluso entra en la función cuando uso la función de simulación de "shake". No aparece nada en la consola de depuración.

http://mithin.in/2009/07/28/detecting-a-shake-using-iphone-sdk

¿Alguien puede explicar lo que me podría estar perdiendo? ¿O apuntarme a un tutorial?

Encontré esto, que parece prometedor, pero no sé cómo "ponerlo en una UIView" ¿Cómo puedo detectar cuando alguien sacude un iPhone?

EDITAR - ahora aquí está mi código de trabajo debido a la sugerencia de la respuesta aceptada.

Aquí está mi código para detectar el gesto de agitar en 3.0 iphone sdk.

-(BOOL)canBecomeFirstResponder { return YES; } -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self becomeFirstResponder]; } - (void)viewWillDisappear:(BOOL)animated { [self resignFirstResponder]; [super viewWillDisappear:animated]; } - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { NSLog(@"{motion ended event "); if (motion == UIEventSubtypeMotionShake) { NSLog(@"{shaken state "); } else { NSLog(@"{not shaken state "); } }


Aquí está mi respuesta que funciona:

//MainViewController.m

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shake) name:@"shake" object:nil]; if(event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake) NSLog(@"motion Began");

}

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shake) name:@"shake" object:nil]; if(event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake) NSLog(@"motion Ended");

}

-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shake) name:@"shake" object:nil]; if(event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake) NSLog(@"motion Cancelled");

}

- (void) viewDidLoad {[super viewDidLoad];

[self becomeFirstResponder];

}

- (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; [self resignFirstResponder];

}

Probé solo con simulador y me devuelve:

2010-06-22 12: 40: 48.799 Cócteles [14589: 207] movimiento Comenzó

2010-06-22 12: 40: 48.800 cócteles [14589: 207] movimiento finalizado

Espero esta ayuda, porque pierdo 2 horas de hacer este trabajo.


No debes estar escuchando UIAccelerometer directamente con tu propio filtro para manejar eventos shake. Esa es una operación de alta potencia y solo debe ser utilizada por aplicaciones que necesitan una alta velocidad de muestreo del acelerómetro. Usa los nuevos eventos de movimiento que se han agregado a UIEvent :

http://developer.apple.com/IPhone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/EventHandling/EventHandling.html#//apple_ref/doc/uid/TP40007072-CH9-SW24

Al igual que los toques, un evento de movimiento se entregará al primer respondedor, y luego viajará por la cadena de respuesta si el primer respondedor no responde. El UIEvent tendrá el tipo UIEventTypeMotion y el subtipo UIEventSubtypeMotionShake .