如何在iPhone中检测摇动

Rad*_*dix 14 xcode objective-c shake iphone-sdk-3.0

在摇动iPhone设备我想要一些功能被调用,我不知道如何识别摇,所以我尝试了一些链接,并尝试了这个代码

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if(event.type == UIEventSubtypeMotionShake)
    {
        NSLog(@"called");
        [self.view setBackgroundColor:[UIColor greenColor]];
    }
}

- (BOOL)canBecomeFirstResponder
{ 
return YES; 
}

但唉没有运气所以你能告诉我如何做同样的事情

感谢致敬

Rad*_*dix 32

根据上面讨论的评论,我正在回答我的问题,以便其他人可以阅读我所拥有的解决方案

在UIResponder类文档中,据说运动事件将仅对第一响应者做出响应,所以我所做的只是添加一个小函数,这对我有用,所以这是我的解决方案

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if(event.type == UIEventSubtypeMotionShake)
    {
        NSLog(@"called");
        [self.view setBackgroundColor:[UIColor greenColor]];
    }
}

- (BOOL)canBecomeFirstResponder
{ 
return YES; 
}

现在我仍然无法检测到任何摇晃动作,所以我所要做的就是让我的视图控制器成为第一个响应者,而这就是我使用的代码

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

我完成了

这是我提出的解决方案

感谢致敬