문제

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?

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top