本文介绍了如何在iPhone中检测震动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iPhone设备的震动下,我希望调用某些功能,我不知道如何识别震动,因此我尝试了一些链接并尝试了此代码

On the shake of the iPhone device i want some function to be called, i dont know how to recognize shake so i tried some link and tried this code


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

- (BOOL)canBecomeFirstResponder
{
return YES;
}

可惜没有运气,所以请让我知道我该怎么做

But alas no luck so can you please let me know how i can do the same

感谢和问候

推荐答案

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

As per the above comments discussed i am answering my question so that others can read the solution that i have got

在UIResponder类文档中,据说运动事件只会响应第一个响应者,所以我所做的只是添加了一个小函数,这对我来说是窍门,所以这是我的解决方案

In the UIResponder class documentation it is said that the motion events will respond to the first responder only so what i did was add just a small function and that did the trick for me, so here's my solution


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

- (BOOL)canBecomeFirstResponder
{
return YES;
}

现在我仍然无法检测到任何抖动动作,因此我要做的就是使我的viewcontroller成为第一响应者,为此,这是我使用的代码

Now still i was not able to detect any shake motion so all i had to do was to make my viewcontroller the first responder and for that here's the code that i used


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

我完成了

这是我想出的解决方案

感谢和问候

这篇关于如何在iPhone中检测震动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 02:57