【问题标题】:SwiftUI: How to cancel timer in SwiftUI view?SwiftUI:如何在 SwiftUI 视图中取消计时器?
【发布时间】:2019-09-29 11:25:09
【问题描述】:

我在 SwiftUI 视图中使用计时器,如下面的代码所示。它按预期工作,但在某些情况下我想取消/停止该计时器。 timer var 上似乎没有“.cancel”属性或方法。如何取消此计时器?有什么想法/提示吗?


import SwiftUI

struct ContentView: View {

    @State private var selection = 2

    @State private var rotation: Double = GBstrtest

    let timer = Timer.publish (every: 0.8, on: .current, in: .common).autoconnect()

    var body: some View {
        TabView(selection: $selection){
            Text("Settings")
                .font(.title)
                .tabItem {
                    VStack {
                        Image(systemName: "gear")
                        .font(Font.system(.title ))
                        Text("Settings")
                    }
                }
                .tag(0)
            VStack {
                Divider().padding(2)
                ZStack {
                    Image("image1")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                    Image("image2")
                        .resizable()
                        .aspectRatio(contentMode:.fit)
                        .rotationEffect(.degrees(rotation))
                        .animation(.easeInOut(duration: 0.3) )
                        .padding(EdgeInsets(top: 0, leading: 50, bottom: 0, trailing: 50))
              }
              Spacer()
            }
            .tabItem {
                VStack {
                    Image(systemName: "speedometer")
                        .font(Font.system(.title ))
                    Text("Read Meter")
                }
            }
            .tag(1)
        }
        .onReceive(timer) {
            _ in self.rotation = Double.random(in: 0 ... 200)
            // How do I cancel timer HERE!?
        }
    }
}

【问题讨论】:

    标签: timer swiftui


    【解决方案1】:

    在您的条件语句中,使用以下代码:

    self.timer.upstream.connect().cancel()
    

    【讨论】:

    • 如何重新连接? @RazibMollick
    • self.timer.upstream.autoconnect()
    • 是重新连接 timer.upstream.autoconnect() 工作,但您的计时器很可能应该是 @State var,因为它正在重新初始化。如果您像我一样使用“让”作为计时器,则 autoconnect() 将不起作用。希望这会有所帮助。
    【解决方案2】:

    整个周期是这样的:

    struct MySwiftUIView : View {
    ...
    
    @State var timer = Timer.publish (every: 1, on: .current, in: .common).autoconnect()
    @State var timeRemaining = -1
    
    var body: some View {
        ZStack {
           // Underlying shapes etc as needed
    
           Text("\(timeRemaining)").
               .opacity(timeRemaining > 0 ? 1 : 0)
               .onReceive(timer) { _ in
                    if self.timeRemaining < 0 {
                        // We don't need it when we start off
                        self.timer.upstream.connect().cancel()
                        return
                    }
                    if self.timeRemaining > 0 {
                        self.timeRemaining -= 1
                    } else {
                        self.timer.upstream.connect().cancel()
                        // Do the action on end of timer. Text would have been hidden by now
                    }
              }
              .onTapGesture {  // Or any other event handler that should start countdown
                    self.timeRemaining = Int(delayValue)
                    self.timer = Timer.publish (every: 1, on: .current, in: 
                                                       .common).autoconnect()
              }
        }
    

    瞧!一个可重复使用的计时器,想用多少次就用多少次!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多