如何在iPad上使用CMPedometer

问题描述 投票:1回答:1

我一直在探索计步器,并玩一个简单的应用程序来计算您的步数。这在iPhone上完美运行,但是在Ipad上运行时无法计算任何步骤。

我已导入CoreMotion并添加了用法说明。

为什么没有在iPad上运行该应用程序的任何原因?

@IBAction func startStopButton(_ sender: UIButton) {
        if sender.titleLabel?.text == "Start"{
            //Start the pedometer
            stepsLabel.text = "Steps: 0"
            distanceLabel.text = "Distance: N/A"

            pedometer = CMPedometer()
            startTimer()
            pedometer.startUpdates(from: Date(), withHandler: { (pedometerData, error) in
                if let pedData = pedometerData{
                    self.numberOfSteps = Int(truncating: pedData.numberOfSteps)
                    //self.stepsLabel.text = "Steps:\(pedData.numberOfSteps)"
                    if let distance = pedData.distance{
                        self.distance = Double(truncating: distance)
                    }
                    if let averageActivePace = pedData.averageActivePace {
                        self.averagePace = Double(truncating: averageActivePace)
                    }
                    if let currentPace = pedData.currentPace {
                        self.pace = Double(truncating: currentPace)
                    }
                } else {
                    self.numberOfSteps = nil
                }
            })
            //Toggle the UI to on state
            statusTitle.text = "Pedometer On"
            sender.setTitle("Stop", for: .normal)
            sender.backgroundColor = stopColor
        } else {
            //Stop the pedometer
            pedometer.stopUpdates()
            stopTimer()
            //Toggle the UI to off state
            statusTitle.text = "Pedometer Off: "
            sender.backgroundColor = startColor
            sender.setTitle("Start", for: .normal)


        }
    }
    //MARK: - timer functions
    func startTimer(){
        if timer.isValid { timer.invalidate() }
        timer = Timer.scheduledTimer(timeInterval: timerInterval,target: self,selector: #selector(timerAction(timer:)) ,userInfo: nil,repeats: true)
    }

    func stopTimer(){
        timer.invalidate()
        displayPedometerData()

        if let distance = self.distance {

            let milesString = (String(format: "%.2f", self.miles(meters: self.distance)))

        let alert = UIAlertController(title: "Save your distance of \(milesString) Miles", message: "You can add the counted distance to all runners now or add them individually by swiping them right on the home screen and adding lap. This will add the distance to the runner you select.", preferredStyle: UIAlertController.Style.alert)
                 // add an action (button)
                alert.addAction(UIAlertAction(title: "Add to all", style: UIAlertAction.Style.default, handler: { action in


                    for index in self.distancesArray.indices {
                        self.distancePerLapMiles = self.miles(meters: self.distance)
                        self.distancesArray[index] += self.distancePerLapMiles
                        print("index is \(index)")
                    }

                    self.tableView.reloadData()
                    self.saveData()

                    self.allViewsOff()

                     }))
                alert.addAction(UIAlertAction(title: "Add Individually", style: UIAlertAction.Style.default, handler: { action in

                    self.distancePerLapMiles = self.miles(meters: self.distance)
                    self.allViewsOff()

                                 }))

            alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil))

                self.present(alert, animated: true, completion: nil)

        }
    }

    @objc func timerAction(timer:Timer){
        displayPedometerData()
    }
    // display the updated data
    func displayPedometerData(){
        timeElapsed += 1.0
        statusTitle.text = "Steps are being Counted"
        //Number of steps
        if let numberOfSteps = self.numberOfSteps{
            stepsLabel.text = String(format:"Steps: %i",numberOfSteps)
        }
        //distance
        if let distance = self.distance{
            distanceLabel.text = String(format:"Distance: \n \n %02.02f meters,\n \n %02.02f miles",distance,miles(meters: distance))
        } else {
            distanceLabel.text = "Distance: N/A"
        }
    }

    //MARK: - Display and time format functions

    // convert seconds to hh:mm:ss as a string
    func timeIntervalFormat(interval:TimeInterval)-> String{
        var seconds = Int(interval + 0.5) //round up seconds
        let hours = seconds / 3600
        let minutes = (seconds / 60) % 60
        seconds = seconds % 60
        return String(format:"%02i:%02i:%02i",hours,minutes,seconds)
    }
    // convert a pace in meters per second to a string with
    // the metric m/s and the Imperial minutes per mile

    func miles(meters:Double)-> Double{
        let mile = 0.000621371192
        return meters * mile
    }

ios swift core-motion
1个回答
0
投票

并非所有设备都具有支持步数计数所需的硬件。

您应检查isStepCountingAvailable的值。如果它返回isStepCountingAvailable,则步数在设备上不可用。

© www.soinside.com 2019 - 2024. All rights reserved.