swift - 在 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()
}
})
}
谢谢...
解决方案
如果我要使用 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 所说,您也可以使用schedule
on RunLoop
:
struct ContentView: View {
var body: some View {
Button(action: {
RunLoop.main.schedule(after: .init(Date() + 1)) {
print("bar")
}
}) {
Text("foo")
}
}
}
推荐阅读
- nativescript - 在我的手机中找不到 nativescript 演示应用程序文件夹路径
- python - 为发生创建一个二进制列
- c# - 无法看到已发布的 ASP.NET 站点的代码更改
- android - android finish() 没有完成我的活动
- csv - BizTalk 平面文件/CSV 架构
- android - 2个活动实现的抽象类工作不正常
- python - Django 将文件从一个视图操作传递到另一个视图操作
- javascript - 为什么这个函数没有像我期望的那样打印出 DIV?
- excel - 可扩展 Excel 表格到 Word 文档
- php - PhpSpreadSheet 是否具有自动从 PHP 数组生成 .xlsx 文件的功能?