目标C:检测摇动

Chr*_*ris 10 objective-c accelerometer shake

我正在使用像这样的摇动api:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{
    if (event.subtype == UIEventSubtypeMotionShake)
    {
        [img stopAnimating];    
    }
}
Run Code Online (Sandbox Code Playgroud)

如何检测到摇晃已停止?

Wri*_*sCS 22

您处于正确的轨道上,但是,还需要添加更多内容来检测抖动:

您可以通过添加一个测试这NSLogmotionBeganmotionEnded方法,并在模拟器中,按CONTROL + COMMAND + Z

#pragma mark - Shake Functions

-(BOOL)canBecomeFirstResponder {
    return YES;
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:NO];
    [self becomeFirstResponder];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:NO];
}

-(void)viewDidDisappear:(BOOL)animated {
    [self resignFirstResponder];
    [super viewDidDisappear:NO];
}

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake )
    {
        // shaking has began.   
    }   
}


-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake )
    {
        // shaking has ended
    }
}
Run Code Online (Sandbox Code Playgroud)