Swift 3设备运动重力

时间:2017-11-23 18:56:48

标签: ios iphone swift gravity

我有一个iPhone应用程序,其中包含一个基于设备动作调用motionmanager的gravityY参数的级别。我已经将电平固定到电话的音高,因为我希望向用户显示电话是否相对于平面(平面到地面)通过其x轴...从一侧到另一侧或旋转升高或下降不相关。要做到这一点,我已经编写了应用程序,以便沿着关卡(一个条形)滑动指示器(超出水平时红色)....它的最大行程是关卡的每个末端。

关卡效果很好......并显示正确的值,直到用户锁定手机并将其放入后袋。在这个阶段,水平指示器转移到水平的一端(手机的末端在口袋中升高),当手机被拉出并解锁时,应用程序不会立即恢复水平 - 它仍然存在超出水平,即使我做一个手动功能调用恢复水平。大约5分钟后,水平似乎恢复了自己。

以下是代码:

func getElevation () {

    //now get the device orientation - want the gravity value
    if self.motionManager.isDeviceMotionAvailable {

        self.motionManager.deviceMotionUpdateInterval = 0.05

        self.motionManager.startDeviceMotionUpdates(
            to: OperationQueue.current!, withHandler: {
                deviceMotion, error -> Void in

                var gravityValueY:Double = 0

                if(error == nil) {
                    let gravityData = self.motionManager.deviceMotion
                    let gravityValueYRad = (gravityData!.gravity.y)
                    gravityValueY = round(180/(.pi) * (gravityValueYRad))

                    self.Angle.text = "\(String(round(gravityValueY)))"
                }
                else {
                    //handle the error
                    self.Angle.text = "0"
                    gravityValueY = 0
                }
                var elevationY = gravityValueY
                //limit movement of bubble
                if elevationY > 45 {
                    elevationY = 45
                }
                else if elevationY < -45 {
                    elevationY = -45
                }

                let outofLevel: UIImage? = #imageLiteral(resourceName: "levelBubble-1")
                let alignLevel: UIImage? = #imageLiteral(resourceName: "levelBubbleGR-1")

                let highElevation:Double = 1.75
                let lowElevation:Double = -1.75

                if highElevation < elevationY {
                    self.bubble.image = outofLevel
                }
                else if elevationY < lowElevation {
                    self.bubble.image = outofLevel
                }
                else {
                    self.bubble.image = alignLevel
                }
                // Move the bubble on the level
                if let bubble = self.bubble {
                    UIView.animate(withDuration: 1.5, animations: { () -> Void in
                        bubble.transform = CGAffineTransform(translationX: 0, y: CGFloat(elevationY))
                    })
                }
        })
    }
}

我希望水平几乎立即恢复(2-3秒内)。我无法强制校准或更新。这是我的第一篇文章....帮助赞赏。

编辑 - 我尝试使用以下代码设置一个单独的应用程序而不使用任何动画:

//

import UIKit
import CoreMotion

class ViewController: UIViewController {

let motionManager = CMMotionManager()

@IBOutlet weak var angle: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func startLevel(_ sender: Any) {
    startLevel()
}

func startLevel() {
//now get the device orientation - want the gravity value
if self.motionManager.isDeviceMotionAvailable {

self.motionManager.deviceMotionUpdateInterval = 0.1
self.motionManager.startDeviceMotionUpdates(
to: OperationQueue.current!, withHandler: {
deviceMotion, error -> Void in

var gravityValueY:Double = 0

if(error == nil) {
    let gravityData = self.motionManager.deviceMotion
    let gravityValueYRad = (gravityData!.gravity.y)
    gravityValueY = round(180/(.pi) * (gravityValueYRad))
    }
else {
    //handle the error
     gravityValueY = 0
    }
self.angle.text = "(\(gravityValueY))"
})}
}
}

仍然表现得完全一样......

1 个答案:

答案 0 :(得分:1)

好的....所以我通过反复试验来解决这个问题。首先,我按如下方式构建了一个stopGravity函数:

    func stopGravity () {
if self.motionManager.isDeviceMotionAvailable {
    self.motionManager.stopDeviceMotionUpdates()
    }
}

我发现如果我调用该函数,则总是设置正确的级别,例如移动到新视图,然后在返回原始视图时重新启动更新。当锁定设备或单击主页按钮时,我需要调用相同的功能,然后在重新加载或返回视图时重新启动重力功能。

为此,我在viewDidLoad()...

中插入了以下内容
        NotificationCenter.default.addObserver(self, selector: #selector(stopGravity), name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(stopGravity), name: NSNotification.Name.UIApplicationWillTerminate, object: nil)

这会通知AppDelegate并运行该功能。这立即解决了这个问题。