gpt4 book ai didi

iOS 运动检测 : Motion Detection Sensitivity Levels

转载 作者:技术小花猫 更新时间:2023-10-29 10:31:21 29 4
gpt4 key购买 nike

我有一个简单的问题。我正在尝试检测用户何时摇动 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com