0

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?

Kara
  • 5,996
  • 16
  • 49
  • 56
Giang
  • 1,914
  • 2
  • 22
  • 24

1 Answers1

1

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).

Community
  • 1
  • 1
valfer
  • 3,475
  • 2
  • 17
  • 23