Frage

I tried using the function

- (BOOL) canBecomeFirstResponder{
    return YES;
 }
- (void) viewWillAppear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeDetected:) name:@"NOTIFICATION_SHAKE" object:nil];

    [self.view becomeFirstResponder];
    [super viewWillAppear:animated];
}

- (void) viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self.view resignFirstResponder];
    [super viewDidDisappear:animated];
}


- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.subtype == UIEventSubtypeMotionShake) {
   //action if detect shake
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATION_SHAKE" object:self];

}

}

But when performing continuous shaking, the function does not automatically detect shaking event. The question here is how to define continuous shaking event?

War es hilfreich?

Lösung

Normally when you shake the device the system will send your view controller the events: motionBegan -> motionEnded.

When you shake continuously, the system send the message motionBegan -> motionCancelled (because it not a single shake anymore) multiple times.

You can try to play with this sequence of event, but I think you have to use the accelerometer, see this answer (diceshaker).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top