Objective C:检测摇晃动作

10

我这样使用shake API:

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

如何检测晃动已停止?

1个回答

23

你已经走在了正确的轨道上,然而,仍需添加更多内容来检测摇晃:

你可以通过向 motionBeganmotionEnded 方法添加一个 NSLog,然后在模拟器中按下 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
    }
}

canBecomeFirstResponder 有意义。 - Almas Adilbek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接