如何检测 iPhone 中的震动

发布于 2024-11-07 15:04:54 字数 396 浏览 1 评论 0原文

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

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

- (BOOL)canBecomeFirstResponder
{ 
return YES; 
}

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

谢谢问候

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

Thanks and Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

新一帅帅 2024-11-14 15:04:54

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

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

,我是完成

这是我想出的解决方案

谢谢和问候

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

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

ヤ经典坏疍 2024-11-14 15:04:54

您可以执行以下操作...

首先...

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

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

    application.applicationSupportsShakeToEdit = YES;

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

其次...

在视图中添加/覆盖 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];
 }

第三...

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

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

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

You could do something like this...

Firstly...

Set the applicationSupportsShakeToEdit property in the App's Delegate:

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

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

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

机场等船 2024-11-14 15:04:54

要全局处理应用程序所有屏幕上的抖动,您可以子类化 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");
        }
    }
}

接下来,在 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
        {
        }
    }

    // ...
}

瞧,摇动是在整个应用程序中处理的。

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:

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:

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.

傲娇萝莉攻 2024-11-14 15:04:54

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

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

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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文