【发布时间】:2020-03-25 08:25:24
【问题描述】:
Swift 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()
}
})
}
谢谢...
【问题讨论】:
-
可以使用RunLoop,见stackoverflow.com/questions/60818892/…
标签: swift swiftui grand-central-dispatch nsoperationqueue nsoperation