10

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

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

我如何检测到震动已经停止?

4

1 回答 1

23

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

您可以通过向or方法添加一个NSLog来测试它,然后在模拟器中,按motionBeganmotionEndedCONTROL + 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
    }
}
于 2011-01-26T19:16:45.083 回答