首页 > 解决方案 > 在 SwiftUI 下延迟运行代码的 GCD 替代方案

问题描述

斯威夫特 5,iOS 13

我正在运行这段代码,它可以工作。

var body: some View {
...
Button(action: {
  self.animateTLeft() 
  quest = quest + "1"
}) { Wedge(startAngle: .init(degrees: 180), endAngle: .init(degrees: 270)) 
         .fill(Color.red) 
         .frame(width: 200, height: 200)
         .offset(x: 95, y: 95)
         .scaleEffect(self.tLeft ? 1.1 : 1.0)
}.onReceive(rPublisher) { _ in
  self.animateTLeft() 
}
...
}

private func animateTLeft() {
withAnimation(.linear(duration: 0.25)){
  self.tLeft.toggle()
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25, execute: {
  withAnimation(.linear(duration: 0.25)){
    self.tLeft.toggle()
  }
})
}

如果可能的话,我想尝试一些使用 GCD 的替代方法。所以我尝试使用没有编译的计时器。我尝试使用 perform,它也没有编译。于是我尝试了编译操作里面!:)。但可悲的是,它只能工作一次。没有GCD的替代品。

private func animateTLeft() {

//perform(#selector(animate), with: nil, afterDelay: 0.25)
//Timer.scheduledTimer(timeInterval: 0.15, target: self, selector: #selector(animateRed), userInfo: nil, repeats: false)

let queue = OperationQueue()
let operation1 = BlockOperation(block: {
  withAnimation(.linear(duration: 1)){
    self.tLeft.toggle()
  }
})
let operation2 = BlockOperation(block: {
  withAnimation(.linear(duration: 1)){
    self.tLeft.toggle()
  }
})
operation2.addDependency(operation1)
queue.addOperations([operation1,operation2], waitUntilFinished: true)

}

我为什么要这样做,因为我有四个要制作动画的切片,但我想要更少的代码。我有红色、绿色、黄色和蓝色切片。我编写了一个通用例程来为它们设置动画,提供颜色作为参数。这是我的代码。

private func animateSlice(slice: inout Bool) {
withAnimation(.linear(duration: 0.25)){
  slice.toggle()
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25, execute: {
  withAnimation(.linear(duration: 0.25)){
    slice.toggle()
  }
})
}

但这不会与 inout 参数一起编译,并给出红色错误消息“Escaping Closure captures 'inout' parameter 'slice'”,这不好。我可以将其复制,而不是 inout 参数。编译,但当然不起作用,因为它没有改变愚蠢的值。

也试过这个,但它不会编译。也许有人可以让它工作。

private func animateSliceX(slice: String) {
var ptr = UnsafeMutablePointer<Bool>.allocate(capacity: 1)
switch slice {
case "red": ptr = &tLeft
case "green": ptr = &tRight
case "yellow": ptr = &bLeft
default: ptr = &bRight
}
withAnimation(.linear(duration: 0.25)){
  ptr.toggle()
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25, execute: {
  withAnimation(.linear(duration: 0.25)){
    ptr.toggle()
  }
})
}

谢谢...

标签: swiftswiftuigrand-central-dispatchnsoperationqueuensoperation

解决方案


如果我要使用 GCD,我可能会使用 Combine 方法,例如:

struct ContentView: View {
    var body: some View {
        Button(action: {
            DispatchQueue.main.schedule(after: .init(.now() + 1)) {
                print("bar")
            }
        }) {
            Text("foo")
        }
    }
}

如果您不想使用 GCD,可以使用Timer

struct ContentView: View {
    var body: some View {
        Button(action: {
            Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { _ in
                print("bar")
            }
        }) {
            Text("foo")
        }
    }
}

或者正如 user3441734 所说,您也可以使用scheduleon RunLoop

struct ContentView: View {
    var body: some View {
        Button(action: {
            RunLoop.main.schedule(after: .init(Date() + 1)) {
                print("bar")
            }
        }) {
            Text("foo")
        }
    }
}

推荐阅读