目标C:检测摇动

时间:2011-01-26 17:09:46

标签: objective-c accelerometer shake

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

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

如何检测到摇晃已停止?

1 个答案:

答案 0 :(得分:22)

你走在正确的轨道上,但是,还需要添加更多东西来检测震动:

您可以通过向NSLogmotionBegan方法添加motionEnded进行测试,然后在模拟器中按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
    }
}