【问题标题】:iOS: How to Implement a "Shake Device" event?iOS:如何实现“摇动设备”事件?
【发布时间】:2011-07-24 13:02:35
【问题描述】:

我想为我的应用程序添加一个“摇动设备”事件 - 即,当用户摇动设备时,会发生一些事情。 我尝试实施:

-(void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (event.subtype == UIEventSubtypeMotionShake) {
        //something happens
    }
}

它似乎不起作用.......
有谁知道我应该使用哪种方法?

【问题讨论】:

    标签: ios shake


    【解决方案1】:

    尝试使用下面的代码,它对我来说很好。

    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
    {
        if ( event.subtype == UIEventSubtypeMotionShake )
           {
              //your code
           }
    
        if ( [super respondsToSelector:@selector(motionEnded:withEvent:)] )
                    [super motionEnded:motion withEvent:event];
    }
    - (BOOL)canBecomeFirstResponder
    {
        return YES;
    }
    

    【讨论】:

      【解决方案2】:

      回答可能为时已晚,但您需要在 viewDidLoad 中加入。

      [self becomeFirstResponder];
      

      试试看。

      【讨论】:

      • 你还必须添加... -(BOOL)canBecomeFirstResponder{return YES;}
      【解决方案3】:

      除了 Taylor 的解决方案,还要确保你的 AppDelegate.m 中有这个。

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
          application.applicationSupportsShakeToEdit = YES;
          return YES;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-04
        • 2018-06-28
        • 2023-04-08
        相关资源
        最近更新 更多