通过 CoreMotion 检测摇动手势
Posted
技术标签:
【中文标题】通过 CoreMotion 检测摇动手势【英文标题】:Detecting Shake Gesture by CoreMotion 【发布时间】:2016-06-23 06:32:11 【问题描述】:我想使用适用于 ios 7 的 coreMotion 框架检测设备抖动。谁能帮我解决这个问题?
我在 viewDidAppear 中编写了下面描述的代码
CMMotionManager *motionManager = [[CMMotionManager alloc] init];
motionManager.deviceMotionUpdateInterval = 1.0/60.0;
__block double myAcceleration;
[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:^(CMDeviceMotion *motion, NSError *error)
myAcceleration = motion.userAcceleration.y;
CATransform3D transform;
transform = CATransform3DMakeRotation(
motion.attitude.pitch, 1, 0, 0);
transform = CATransform3DRotate(transform,
motion.attitude.roll, 0, 1, 0);
transform = CATransform3DRotate(transform,
motion.attitude.yaw, 0, 0, 1);
];
但未检测到抖动。
【问题讨论】:
嘿,如果有人知道在 ios 7 及更高版本中使用 core motion 进行摇动,请分享您的答案 【参考方案1】: #define accelerationThreshold 0.30
- (void)motionMethod:(CMDeviceMotion *)deviceMotion
CMMotionManager *motionManager = [[CMMotionManager alloc] init];
CMAcceleration userAcceleration = deviceMotion.userAcceleration;
if (fabs(userAcceleration.x) > accelerationThreshold || fabs(userAcceleration.y) > accelerationThreshold || fabs(userAcceleration.z) > accelerationThreshold)
float sensitivity = 1;
float x1 = 0, x2 = 0, y1 = 0, y2 = 0, z1 = 0, z2 = 0;
double totalAccelerationInXY = sqrt(userAcceleration.x * userAcceleration.x +
userAcceleration.y * userAcceleration.y);
if (0.85 < totalAccelerationInXY < 3.45)
x1 = userAcceleration.x;
y1 = userAcceleration.y;
z1 = userAcceleration.z;
float change = fabs(x1-x2+y1-y2+z1-z2);
if (sensitivity < change)
// print change in position in coordinates.
NSLog (@"total=%f x=%f y=%f z=%f timeStamp:%f, UpTime:%f", totalAccelerationInXY, userAcceleration.x, userAcceleration.y, userAcceleration.z, deviceMotion.timestamp, [[NSProcessInfo processInfo] systemUptime]);
x2 = x1;
y2 = y1;
z2 = z1;
【讨论】:
帮我检测 ios 设备的抖动。【参考方案2】:我知道它很旧,但我正在为可能感兴趣的其他人发帖。
此抖动检测代码也可以在没有 ViewController 或应用在后台时使用(只需使用 coremotion)
import CoreMotion
private var centralManager : CBCentralManager!
@IBOutlet weak var shakeLabel: UILabel!
let manager = CMMotionManager()
override func viewDidLoad()
super.viewDidLoad()
var xInPositiveDirection = 0.0
var xInNegativeDirection = 0.0
var shakeCount = 0
var tempVariable = 0
manager.deviceMotionUpdateInterval = 0.02
manager.startDeviceMotionUpdates(to: OperationQueue.main, withHandler:
(data, error) in
if data!.userAcceleration.x > 1.0 || data!.userAcceleration.x < -1.0
if data!.userAcceleration.x > 1.0
xInPositiveDirection = data!.userAcceleration.x
if data!.userAcceleration.x < -1.0
xInNegativeDirection = data!.userAcceleration.x
if xInPositiveDirection != 0.0 && xInNegativeDirection != 0.0
shakeCount = shakeCount + 1
xInPositiveDirection = 0.0
xInNegativeDirection = 0.0
if shakeCount > 5
tempVariable = tempVariable + 1
self.shakeLabel.text = "Shaken! \(tempVariable)"
shakeCount = 0
)
【讨论】:
以上是关于通过 CoreMotion 检测摇动手势的主要内容,如果未能解决你的问题,请参考以下文章
在 SwiftUI 中通过摇动手势(使用 UIKit)设置 EnvironmentObject 不会更新视图