1

我有 2 个类(一个用于 HealthKit,另一个用于 CoreMotion),它们为一些已发布的变量分配值,然后我通过我的主 swiftui 文件访问这些变量。我的内容视图正确更新。到目前为止,一切都很好。现在我想将这些变量的输出记录到控制台。我可以通过直接从类中记录来直接执行此操作,但我遇到的问题是我需要将两个类的输出放在一行中。我为此编写了一个新函数,但值没有得到更新。谁能帮助我?是不是因为函数只被调用一次,所以所有参数总是函数被调用时的参数?

我的主要 contentView.swift 文件:

import SwiftUI

struct ContentView: View {

    @State var isActive: Bool = false
    @ObservedObject var motion = MotionManager()
    @ObservedObject var health = HealthKitManager()

    private var logger = ConsoleLogger()

    var body: some View {
        ScrollView {
            VStack(alignment: .leading) {
                Indicator(title: "X:", value: motion.rotationX)
                Indicator(title: "Y:", value: motion.rotationY)
                Indicator(title: "Z:", value: motion.rotationZ)
                Divider()
                Indicator(title: "Pitch:", value: motion.pitch)
                Indicator(title: "Roll:", value: motion.roll)
                Indicator(title: "Yaw:", value: motion.yaw)
                Divider()
                Indicator(title: "HR:", value: health.heartRateValue)
                }
            .padding(.horizontal, 10)
            Button(action: {
                self.isActive.toggle()
                self.isActive ? self.start() : self.stop()
            }) {
                Text(isActive ? "Stop" : "Start")
            }
            .background(isActive ? Color.green : Color.blue)
            .cornerRadius(10)
        }.onAppear {
            self.health.autorizeHealthKit()
        }
    }

    private func start() {
        self.motion.startMotionUpdates()
        self.health.fetchHeartRateData(quantityTypeIdentifier: .heartRate)
        self.logger.startLogging(rotationX: self.motion.rotationX, rotationY: self.motion.rotationY, rotationZ: self.motion.rotationZ, pitch: self.motion.pitch, roll: self.motion.roll, yaw: self.motion.yaw, heartRate: self.health.heartRateValue)
    }

    private func stop() {
        self.motion.stopMotionUpdates()
        self.health.stopFetchingHeartRateData()
        self.logger.stopLogging()
    }

}

struct Indicator: View {
    var title: String
    var value: Double

    var body: some View {
        HStack {
            Text(title)
                .font(.footnote)
                .foregroundColor(.blue)
            Text("\(value)")
                .font(.footnote)
        }
    }
}

这是我的 consoleLogger.swift 文件

import Foundation
import os.log

class ConsoleLogger {

    private var timer: Timer?

    func startLogging(rotationX: Double, rotationY: Double, rotationZ: Double, pitch: Double, roll: Double, yaw: Double, heartRate: Double) {

        timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { _ in
            let timestamp = Date().timeIntervalSince1970
            os_log("Motion: %@, %@, %@, %@, %@, %@, %@, %@",
                   String(timestamp),
                   String(rotationX),
                   String(rotationY),
                   String(rotationZ),
                   String(pitch),
                   String(roll),
                   String(yaw),
                   String(heartRate)
            )
        })
    }

    func stopLogging() {
        timer?.invalidate()
    }
}

所以我的计时器正在工作,我每秒都在控制台中看到输出,但是所有变量(在类中创建的时间戳除外)的初始值为 0.0。知道如何解决吗?

4

1 回答 1

1

问题不只是您只startLogging使用初始值集调用一次吗?然后,您只需一遍又一遍地打印同一组值。您无处可咨询实际数据(当前持有您的motionhealth对象)来查看它现在是什么。

于 2020-03-15T13:18:12.373 回答