iOS 运动检测 : Motion Detection Sensitivity Levels

标签 ios detection motion

我有一个简单的问题。我正在尝试检测用户何时摇动 iPhone。我有标准代码来检测运动,这没问题。但是,在我的实际手机上进行测试时,我意识到您必须非常用力地摇晃设备才能触发运动检测。我想知道是否有办法实现一定程度的敏感性检查。例如,一种检测用户是否轻轻摇晃设备或介于轻摇和剧烈摇晃之间的方法。这将针对 iOS 7,因此将不胜感激旧 iOS 版本未弃用的任何提示或建议。我已经完成了谷歌搜索,但还没有找到解决这个问题的任何好的方法(如果有的话。)

谢谢!

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if(motion == UIEventSubtypeMotionShake)
    {
       //Detected motion, do something about it 
       //at this point.
    }
}

-(BOOL)canBecomeFirstResponder
{
    return YES;
}

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

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

最佳答案

这是我找到的解决方案。这很好用,但你必须使用 deviceMotionUpdateInterval 时间值以及 accelerationThreshold 这可能很难获得实际“轻摇”与“拿起手机并将其靠近你的脸等”的良好平衡...”可能有更好的方法,但这里是一个开始。在我的 View didLoad 中,我做了这样的事情:

#import <CoreMotion/CoreMotion.h> //do not forget to link the CoreMotion framework to your project
#define accelerationThreshold  0.30 // or whatever is appropriate - play around with different values

-(void)viewDidLoad
{
      CMMotionManager *motionManager; 

      motionManager = [[CMMotionManager alloc] init];
      motionManager.deviceMotionUpdateInterval = 1;

      [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]  withHandler:^(CMDeviceMotion *motion, NSError *error)
             {
                [self motionMethod:motion];
             }];
}

-(void)motionMethod:(CMDeviceMotion *)deviceMotion
{
    CMAcceleration userAcceleration = deviceMotion.userAcceleration;
    if (fabs(userAcceleration.x) > accelerationThreshold
        || fabs(userAcceleration.y) > accelerationThreshold
        || fabs(userAcceleration.z) > accelerationThreshold)
        {
           //Motion detected, handle it with method calls or additional
           //logic here.
           [self foo];
        }
}

关于iOS 运动检测 : Motion Detection Sensitivity Levels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19131957/

相关文章:

ios - 将值转换为从 Error 继承的枚举

ios - 将 ScrollView 放大到特定坐标(IPAD)

iphone - 如何使用 iPhone 的摄像头跟踪运动?

android - 如何使typeconverter与包含列表的自定义类的列表一起使用?

opencv - 检测自定义形状 OpenCV

python - opencv/ python : motion detect weird thresholding

android - 当 ANDROID 中没有更多 Action 移动事件时如何获取 Action?

ios - 从 NSDictionaryResultType 获取关系

ios - 在 isKindOfClass : 中未检测到从 UIButton 继承的自定义类

r - 如何在 R 中通过网络检测远程文件夹中新添加的文件?