我正在尝试将HealthKit
中的步骤中的数据转换为圆形动画,但收到此错误:
二进制运算符'<='不能应用于UILabel和Double类型的操作数
我尝试过类型转换,但是说我无法转换。
这是发生错误的部分
if self.totalSteps <= 100.0 {
cP.setProgressWithAnimation(duration: 1.0, value: 0.1)
if self.totalSteps >= 100.0 {
cP.setProgressWithAnimation(duration: 1.0, value: 0.2)
}
}
这就是我计算步数的方式
func getTodaysSteps(completion: @escaping (Double) -> Double) {
let stepsQuantityType = HKQuantityType.quantityType(forIdentifier: .stepCount)!
let now = Date()
let startOfDay = Calendar.current.startOfDay(for: now)
let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: now, options: .strictStartDate)
let query = HKStatisticsQuery(quantityType: stepsQuantityType, quantitySamplePredicate: predicate, options: .cumulativeSum) { (_, result, error) in
var resultCount = 0.0
guard let result = result else {
print("Failed to fetch steps rate")
completion(resultCount)
return
}
if let sum = result.sumQuantity() {
resultCount = (sum.doubleValue(for: HKUnit.count()))
}
DispatchQueue.main.async {
completion(resultCount)
}
}
healthStore.execute(query)
}
@IBOutlet weak var theGif: UIImageView!
@IBOutlet weak var totalSteps: UILabel!
@IBAction func getTotalSteps(_ sender: Any) {
getTodaysSteps { (result) in
print("\(result)")
DispatchQueue.main.async {
self.totalSteps.text = "\(result)"
这就是我制作动画的方式
func setProgressWithAnimation(duration: TimeInterval, value: Float) {
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.duration = duration
animation.fromValue = 0
animation.toValue = value
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)
progressLayer.strokeEnd = CGFloat(value)
progressLayer.add(animation, forKey: "animateprogress")
我希望圆形进度条反映出步骤数。