【问题标题】:SwiftUI Timer is not startingSwiftUI 计时器未启动
【发布时间】:2022-01-27 11:54:16
【问题描述】:

我已经尝试了几种不同的方法在 Xcode SwiftUI 中的三个不同应用程序上运行计时器(onRecieve 和调度),但还没有看到计时器在模拟器中工作。我按照以下视频中的说明进行演示:

https://www.youtube.com/watch?v=y68YmyTB7JU

我不确定 Xcode 是否需要修改设置以在我的笔记本电脑上的模拟器中启用计时器,或者我是否缺少更新版本的 Xcode/SwiftUI 所需的内容。不过,截至本文发布时,该视频只有大约 6 个月大。

提前谢谢你!

内容视图:

import SwiftUI

struct ContentView: View {
    @ObservedObject var stopWatchManager = StopWatchManager()
    
    var body: some View {
        VStack {
            Text(String(format: "%.1f", stopWatchManager.secondsElapsed))
                .font(.system(size: 50))
                .foregroundColor(.yellow)
                .padding(.top, 200)
                .padding(.bottom, 100)
                .padding(.trailing, 100)
                .padding(.leading, 100)
            
            if stopWatchManager.mode == .stopped{
                Button(action: {self.stopWatchManager.start()}){
                    TimerButton(label: "Start", buttonColor: .green, textColor: .black)
                }
            }
            
            if stopWatchManager.mode == .running{
                Button(action: {self.stopWatchManager.pause()}){
                    TimerButton(label: "Pause", buttonColor: .green, textColor: .black)
                }
            }
            
            if stopWatchManager.mode == .paused{
                Button(action: {self.stopWatchManager.start()}){
                    TimerButton(label: "Start", buttonColor: .green, textColor: .black)
                }
                Button(action: {self.stopWatchManager.stop()}){
                    TimerButton(label: "Stop", buttonColor: .red, textColor: .black)
                }
                .padding(.top, 30)
            }
            
            Spacer()
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct TimerButton: View {
    
    let label: String
    let buttonColor: Color
    let textColor: Color
    
    var body: some View {
        Text(label)
            .foregroundColor(textColor)
            .padding(.vertical, 20)
            .padding(.horizontal, 90)
            .background(buttonColor)
            .cornerRadius(10)
    }
}

秒表管理器:

import Foundation
import SwiftUI

class StopWatchManager: ObservableObject{
    
    enum stopWatchMode {
        case running
        case stopped
        case paused
    }
    
    @Published var mode: stopWatchMode = .stopped
    
    @Published var secondsElapsed = 0
    var timer = Timer()
    
    func start() {
        mode = .running
        timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) {
            timer in self.secondsElapsed += 1
        }
    }

    func pause() {
        timer.invalidate()
        mode = .paused
    }
        
    func stop() {
        timer.invalidate()
        secondsElapsed = 0
        mode = .stopped
    }
}

【问题讨论】:

  • 您应该在此处包含您的代码(minimal reproducible example)- 要求人们关注视频链接并重现那里不太可能发生的代码。另外,如果视频链接过时,问题就变得毫无用处。

标签: swift xcode swiftui timer


【解决方案1】:

这是一个难以识别的鬼鬼祟祟的错误。

在您的代码中,您将elapsedSeconds 定义为:

@Published var secondsElapsed = 0

Swift 进行类型推断以推断secondsElapsedInt。当您使用 String(format: "%.1f"...) 时,它需要一个 Double(浮点数)并且不会正确更新为 Int

在视频代码中,你会看到secondsElapsed被定义为:

@Published var secondsElapsed = 0.0

Swift 推断这是一个Double——如果你做出改变,一切都会按预期工作。

PS:您可以考虑将 Timer 与 Combine(例如 Timer.publish)结合使用,以获得更以 SwiftUI 为中心的处理方式。见the documentation

【讨论】:

    猜你喜欢
    • 2018-09-26
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    • 2013-09-04
    • 2020-12-30
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    相关资源
    最近更新 更多