I am trying to learn about the Shake Gesture in IOS and am making a small app that will count for how many times the device is being shaken continuously without stopping . I am not sure what I am doing wrong, but when you shake the device quite vigorously and then stop shaking it does not detect that the shaking has stopped. Ill show my code below and hopefully you can help. Thanks

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake && canStartShaking == YES) {
        [self runTimer];
    }
}

-(void)runTimer
{
    [self stopTimer];
    NSLog(@"started");
    timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateShakes:) userInfo:nil repeats:YES];
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    canStartShaking = NO;
    [timer invalidate];
    startButton.hidden = NO;
    isShaking = NO;
    shakes = 0;
    NSLog(@"stopped");
}

- (void) stopTimer
{
    if (timer) {
        [timer invalidate];
        timer = nil;
    }
}

Thanks for you help

Related posts

Recent Viewed