如何使用加速度计和Swift快速检测苹果手表的位置?

时间:2017-11-17 11:09:33

标签: ios swift3 accelerometer apple-watch devicemotion

我已经为Apple Watch创建了一个应用程序。逻辑是,当用户举手并点击应用程序中的按钮时。那时我将获取加速度计值。每当用户举手并达到捕获位置时,我都必须向iPhone发送消息。

对我来说正确地得到了值但是,它总是会根据加速度计给出值。这意味着用户不会抬起手但加速度计值匹配。因此,值将发送到移动设备。

func startUpadateAccelerometer() {
        self.motionManager.accelerometerUpdateInterval = 1.0 / 10.0
        self.motionManager.startAccelerometerUpdates(to: OperationQueue()) { (accelerometerData, error) -> Void in
            guard accelerometerData != nil else
            {
                print("There was an error: \(String(describing: error))")
                return
            }

            DispatchQueue.main.async {
                if(self.can_reset){
                    let differenceX : Bool = self.validateButtom(currentValue: accelerometerData!.acceleration.x, inititalValue: self.gravityReference.x)
                    let differenceY : Bool = self.validateButtom(currentValue: accelerometerData!.acceleration.y, inititalValue: self.gravityReference.y)

                    if(differenceX && differenceY && self.gravityOffsetDifference(currentValue: accelerometerData!.acceleration.x, referenceValue: self.gravityReference.x ) && self.gravityOffsetDifference(currentValue: accelerometerData!.acceleration.y, referenceValue: self.gravityReference.y)){
                        WKInterfaceDevice().play(.success)
//                        self.addLog(_logStr: EventsTypes.Achievements1.rawValue)

                        self.logString += String(format: "X: %0.3f Y: %0.3f Z: %0.3f  \n", accelerometerData!.acceleration.x,accelerometerData!.acceleration.y,accelerometerData!.acceleration.z)
                        self.m_XYZValueLbl.setText(self.logString)

                        self.is_RechedZeroPos = true
                        self.session?.sendMessage(["msg" : "\(self.logString)"], replyHandler: nil) { (error) in
                            NSLog("%@", "Error sending message: \(error)")
                        }

                    } else {
                        if(self.checkAchievements2_3(deviceMotionData: accelerometerData!.acceleration) == true) {
                            if self.is_RechedZeroPos == true {
                                self.addLog(_logStr: EventsTypes.Achievements2.rawValue)
                                self.is_RechedZeroPos = false
                            } else {
                                self.addLog(_logStr: EventsTypes.Achievements3.rawValue)
                            }
                        }
                    }
                } else {
                    self.gravityReference = accelerometerData!.acceleration
                    //self.logString = String(format: "Reference Acceleration   %0.3f   %0.3f   %0.3f  \n", self.gravityReference.x,self.gravityReference.y,self.gravityReference.z)
                    self.can_reset = true
                }
            }
        }
    }

func validateButtom(currentValue : Double , inititalValue : Double) -> Bool {
        if( currentValue == 0 && inititalValue == 0) {
            return true
        } else if( currentValue < 0 && inititalValue < 0) {
            return true
        } else if( currentValue > 0 && inititalValue > 0) {
            return true
        } else {
            return false
        }
    }



 func gravityOffsetDifference(currentValue : Double , referenceValue: Double) -> Bool {
        var difference : Double!
        if (fabs(currentValue) <= fabs(referenceValue)) {
            difference = fabs(referenceValue) - fabs(currentValue)
        } else {
            difference = fabs(currentValue) - fabs(referenceValue)
        }

        if (difference <= gravityOffset ) {
            return true
        } else {
            return false
        }
    }

请指导我仅在用户占据位置时获取值。

0 个答案:

没有答案