如何确定 iPhone 晃动的方向 [英] How to determine the direction of a iPhone shake

查看:35
本文介绍了如何确定 iPhone 晃动的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用加速度计在 UIScrollVIew 中滚动多个子视图.我希望视图(纵向)在用户向右滑动 iPhone 时向右滚动,并在设备向左滑动时向左滚动.

I am using the accelerometer to scroll multiple subViews in a UIScrollVIew. I want the view (portrait orientation) to scroll to the right when the user flicks the iPhone to the right, and scroll to the left when the device is flicked to the left.

我以为我可以通过记下正或负的 x 加速度值来做到这一点,但我发现这些值通常是正值和负值的混合.我已将地板设置为 1.5g 以消除非抖动运动,并且正在查看 0.5 秒持续时间内的 x 值.

I thought I could do that just by noting positive or negative x acceleration values, but I see that the values are usually a mixture of positive and negative values. I have set the floor at 1.5g to eliminate non-shake movement, and am looking at the x values over the duration of .5 seconds.

我确信有一种三角方法可以确定轻弹的整体方向,并且您必须在轻弹运动的持续时间内测量值.我也确定有人已经想出了这个.

I'm sure there is a trigonometrical method for determining the overall direction of a flick, and that you have to measure values over the duration of the flick motion. I'm also sure that someone has already figured this one out.

有什么想法吗?

谢谢

推荐答案

我开发了一个解决方案,它给我比提议的解决方案更好的反馈(仅适用于左右摇晃).我在这里做的方式非常敏感(识别小震动),但可以通过更改 tresholdFirstMove 和 tresholdBackMove 来调整灵敏度(增加以降低灵敏度)

I developed a solution which gives me better feedback than the proposed solution (only for left and right shake). The way I did it here is quite sensitive (recognizes a small shake), but sensitivity can be adapted by changing tresholdFirstMove and tresholdBackMove (increase for lower sensitivity)

在 Swift 中:(在您的 viewController 中.并添加import CoreMotion")

In Swift : (in your viewController. And add "import CoreMotion")

var startedLeftTilt = false
var startedRightTilt = false
var dateLastShake = NSDate(timeIntervalSinceNow: -2)
var dateStartedTilt = NSDate(timeIntervalSinceNow: -2)
var motionManager = CMMotionManager()
let tresholdFirstMove = 3.0
let tresholdBackMove = 0.5

override func viewDidLoad() {
    // your code

    motionManager.gyroUpdateInterval = 0.01
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    motionManager.startGyroUpdatesToQueue(NSOperationQueue.currentQueue(), withHandler: { (gyroData, error) -> Void in
        self.handleGyroData(gyroData.rotationRate)
    })

}

private func handleGyroData(rotation: CMRotationRate) {

    if fabs(rotation.z) > tresholdFirstMove && fabs(dateLastShake.timeIntervalSinceNow) > 0.3
    {
        if !startedRightTilt && !startedLeftTilt
        {
            dateStartedTilt = NSDate()
            if (rotation.z > 0)
            {
                startedLeftTilt = true
                startedRightTilt = false
            }
            else
            {
                startedRightTilt = true
                startedLeftTilt = false
            }
        }
    }

    if fabs(dateStartedTilt.timeIntervalSinceNow) >= 0.3
    {
        startedRightTilt = false
        startedLeftTilt = false
    }
    else
    {
        if (fabs(rotation.z) > tresholdBackMove)
        {
            if startedLeftTilt && rotation.z < 0
            {
                dateLastShake = NSDate()
                startedRightTilt = false
                startedLeftTilt = false

                println("\
 Shaked left
/")
            }
            else if startedRightTilt && rotation.z > 0
            {
                dateLastShake = NSDate()
                startedRightTilt = false
                startedLeftTilt = false

                println("\
 Shaked right
/")
            }
        }
    }

}

这篇关于如何确定 iPhone 晃动的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆