V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
appstore54321
V2EX  ›  iDev

Swift UI 用 Timer 每秒更新 Text 时文字会出现残影

  •  
  •   appstore54321 · 2 小时 17 分钟前 · 79 次点击

    想在 app 里加一个页面显示当前的系统时间,精确到秒,网上查到的资料都是推荐用 Timer 每秒触发一次更新。

    struct AtomicClockView: View {
        @State private var currentDate = Date.now
        let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
        
        var body: some View {
            Text(currentDate.formatted(date: .omitted, time: .standard))
                .font(.system(size: 50, weight: .ultraLight, design: .monospaced))
                .onReceive(timer) { input in
                    currentDate = input
                }
        }
    }
    
    

    然后放在 TabView 里了

    var body: some View {
            NavigationStack {
                TabView {
                    AtomicClockView()
                        .tabItem {
                            Label("Atomic Clock", systemImage: "clock")
                        }
                }
            }
        }
    

    在 Xcode 里预览和模拟器里看起来都一切正常,但是在手机( iPhone 14 Pro, iOS 18 )上打开之后,可以明显感到秒的文字在更新时会有上一秒的残影叠加在上面,而不是瞬间消失再渲染下一秒。而且好像是头几秒没有问题,第 3 秒左右开始出现残影。

    搜索了一下好像没有人提过类似的问题,所以不知道是 SwiftUI 的问题还是代码的问题,每秒一次的频率应该不会是性能问题吧(尝试把 Timer 改成 0.1 也无法解决)。

    感谢帮助。

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2431 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 20ms · UTC 10:31 · PVG 18:31 · LAX 03:31 · JFK 06:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.