目标C:检测到震动 [英] Objective C: Detecting a shake

查看:94
本文介绍了目标C:检测到震动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the shake api like this:

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

如何检测到抖动已停止?

How do I detect that the shaking has stopped?

推荐答案

您处于正确的轨道,但是,还需要添加更多东西来检测晃动:

You are on the right track, however, there are still more things you need to add to detect shaking:

您可以通过在 motionBegan motionEnded 方法,然后在模拟器中按 CONTROL + COMMAND + Z

You can test this by adding an NSLog to the motionBegan or motionEnded methods, and in the Simulator, press 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
    }
}

这篇关于目标C:检测到震动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆