xcode 如何检测 iPhone 中的抖动动作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6042420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:03:15  来源:igfitidea点击:

How to detect shake motion in iPhone

objective-cxcodeiphone-sdk-3.0shake

提问by Radix

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

在 iPhone 设备抖动时,我想要调用一些函数,我不知道如何识别抖动,所以我尝试了一些链接并尝试了此代码

- (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

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

Thanks and Regards

感谢致敬

回答by Radix

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

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

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

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

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

- (BOOL)canBecomeFirstResponder
{ 
return YES; 
}

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];
}

and i was done

我完成了

This was the solution that i came up with

这是我想出的解决方案

Thanks and Regards

感谢致敬

回答by Thomas Stone

You could do something like this...

你可以做这样的事情......

Firstly...

首先...

Set the applicationSupportsShakeToEdit property in the App's Delegate:

在应用程序的委托中设置 applicationSupportsShakeToEdit 属性:

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    application.applicationSupportsShakeToEdit = YES;

    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}

Secondly...

其次...

Add/Override canBecomeFirstResponder, viewDidAppear: and viewWillDisappear: methods in your View Controller:

在您的视图控制器中添加/覆盖 canBecomeFirstResponder、viewDidAppear: 和 viewWillDisappear: 方法:

-(BOOL)canBecomeFirstResponder {
    return YES;
}

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

- (void)viewWillDisappear:(BOOL)animated {
    [self resignFirstResponder];
    [super viewWillDisappear:animated];
 }

Thirdly...

第三...

Add the motionEnded method to your View Controller:

将 motionEnded 方法添加到您的视图控制器:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
   if (motion == UIEventSubtypeMotionShake)
   {
    // your code
   }
 }

That should work if the first answer did not and this is only quickly typed not tested:)

如果第一个答案没有,那应该可以工作,而且这只是快速输入未测试:)

回答by Bj?rn Egil

To globally handle shake across all screens of your app, you can subclass UIWindow and implement motionEnded, since the event will bubble up the responder chain until it reaches the window-object. Example using Xamarin:

要在应用程序的所有屏幕上全局处理抖动,您可以子类化 UIWindow 并实现motionEnded,因为该事件将在响应者链中冒泡,直到它到达窗口对象。使用 Xamarin 的示例:

public class MyWindow : UIWindow
{
    public MyWindow(CoreGraphics.CGRect frame)
        : base (frame)
    { }

    public override void MotionEnded (UIEventSubtype motion, UIEvent evt)
    {
        if (motion == UIEventSubtype.MotionShake) {
            Console.WriteLine ("Shake received");
        }
    }
}

Next, in AppDelegate, implement the window method to return an instance of your UIWindow subclass. Example using Xamarin:

接下来,在 AppDelegate 中,实现 window 方法以返回 UIWindow 子类的实例。使用 Xamarin 的示例:

public class AppDelegate : UIApplicationDelegate
{
    UIWindow _window;

    public override UIWindow Window 
    { 
        get
        {
            if (_window == null) {
                _window = new MyWindow (UIScreen.MainScreen.Bounds);
            }

            return _window;
        }
        set
        {
        }
    }

    // ...
}

Voilà, shake is handled across the whole app.

瞧,整个应用程序都在处理抖动。

回答by Shengsheng

Things become easier with SSEventListener. Your view controller doesn't need to override motionEnded:withEvent:any more! With SSEventListener, you can listener for shake event as simple as this:

使用SSEventListener事情变得更容易。您的视图控制器不再需要覆盖motionEnded:withEvent:!使用SSEventListener,您可以像这样简单地监听震动事件:

[viewController ss_setShakeEventListener:^{ ... }];

[viewController ss_setShakeEventListener:^{ ... }];