Timer.TimerPublisher 滚动时不触发

Timer.TimerPublisher doesn’t fire while scrolling

我尝试在 SwiftUI 上制作一个计时器,效果很好:

import SwiftUI
import Combine    
struct ContentView: View {
let currentTimePublisher  = Timer.TimerPublisher(interval: 1.0, runLoop: .main, mode: .default)
    .autoconnect()

@State private var currentTime = Date()
var body: some View {

    VStack{
        Text("Time is:")
        Text("\(currentTime)")
        }.onReceive(currentTimePublisher) { (date) in
            self.currentTime = date
        }
    }
}

但是,我试图将 VStack 放在任何可滚动的东西中,比如 List 或 ScrollView,当我滚动屏幕时我没有更新。当我不滚动时它工作得很好。

如您所见,我还将 TimerPublisher 放在主线程上,但这并没有改变任何东西。

如何让 SwiftUI 在我 scrolling/interacting 时更新时间?

滚动视图 运行 在跟踪触摸时以不同的模式(不是 .default)循环 运行。使用模式 .common 而不是模式 .default 在“常见”运行 循环模式中安排计时器,其中包括滚动跟踪模式:

let currentTimePublisher  = Timer.TimerPublisher(interval: 1.0, runLoop: .main, mode: .common)
    .autoconnect()