我\xe2\x80\x99m 在 SwiftUI 中构建了一个 Apple Watch 应用,该应用读取用户\xe2\x80\x99 的心率并将其显示在心形符号旁边。
\n我有一个动画可以让心形符号反复跳动。因为我知道实际的用户\xe2\x80\x99s心率,所以我\xe2\x80\x99d喜欢让它以与用户\xe2\x80\x99s心率相同的速率跳动,每次速率更新动画变化。
\n我可以通过将心率除以 60 来确定节拍之间应间隔多少秒。例如,如果用户\xe2\x80\x99s 的心率为 80 BPM,则动画应每 0.75 秒 (60/80) 发生一次。
\n这是我现在拥有的示例代码,其中currentBPM是一个常量,但通常会更新。
struct SimpleBeatingView: View {\n \n // Once I get it working, this will come from a @Published Int that gets updated any time a new reading is avaliable.\n let currentBPM: Int = 80\n \n @State private var isBeating = false\n \n private let maxScale: CGFloat = 0.8\n \n var beatingAnimation: Animation {\n \n // …Run Code Online (Sandbox Code Playgroud)