Swift,从一个阵列连续播放多个动画?
对不起,我是新来的斯威夫特。我不能让每个动画连续播放,也不能一次播放全部动画。我尝试过使用睡眠,但那似乎不允许播放动画。这就是我如何找到要播放的动画Swift,从一个阵列连续播放多个动画?,swift,animation,Swift,Animation,对不起,我是新来的斯威夫特。我不能让每个动画连续播放,也不能一次播放全部动画。我尝试过使用睡眠,但那似乎不允许播放动画。这就是我如何找到要播放的动画 for number in sequence { switch number { case 1: print("blue") animateB() case 2: print("green") animateG() case 3: pri
for number in sequence {
switch number {
case 1:
print("blue")
animateB()
case 2:
print("green")
animateG()
case 3:
print("magenta")
animateM()
case 4:
print("orange")
animateO()
case 5:
print("yellow")
animateY()
case 6:
print("red")
animateR()
case 7:
print("purple")
animateP()
case 8:
print("cyan")
animateC()
default:
print("error")
}
}
这是我用来制作动画的函数之一。我意识到这可能也是非常低效的,但不确定如何使功能更好
private func animateB(){
let animation = CABasicAnimation(keyPath: "transform.scale")
animation.toValue = 1.3
animation.duration = 0.5
animation.autoreverses = true
self.pulsatingB.add(animation, forKey: "pulsing")
}
任何帮助都很好,谢谢 您可以使用CATTransaction来链接:
对于动画序列,基于块的关键帧动画通常也可以完成此工作,例如:
UIView.animateKeyframes(withDuration: 4.0, delay: 0, options: .repeat, animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.25, animations: {
self.subview.transform = .init(scaleX: 0.5, y: 0.5)
})
UIView.addKeyframe(withRelativeStartTime: 0.25, relativeDuration: 0.25, animations: {
self.subview.transform = .init(scaleX: 1.3, y: 1.3)
})
UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.25, animations: {
self.subview.transform = .init(scaleX: 0.75, y: 0.75)
})
UIView.addKeyframe(withRelativeStartTime: 0.75, relativeDuration: 0.25, animations: {
self.subview.transform = .identity
})
}, completion: nil)
或者,如果您有一系列函数:
let animations = [animateA, animateB, animateC, animateD]
UIView.animateKeyframes(withDuration: 4.0, delay: 0, options: .repeat, animations: {
for (index, animation) in animations.enumerated() {
UIView.addKeyframe(withRelativeStartTime: Double(index) / Double(animations.count), relativeDuration: 1 / Double(animations.count), animations: {
animation()
})
}
}, completion: nil)
在哪里,
func animateA() {
subview.transform = .init(scaleX: 0.5, y: 0.5)
}
func animateB() {
subview.transform = .init(scaleX: 1.3, y: 1.3)
}
...
回答得好。投票表决。然而,for循环和switch语句似乎没有太多意义。为什么不只是animationQueue=[animateB,animateC,animateD,animateE,animateF,animateG]?@duncac同意。我的注意力集中在解决连续动画问题上。没有停下来考虑for循环和switch,那么你应该编辑你的答案。这会使它更干净。谢谢,它起作用了。不过,我仍然在使用for循环和switch语句。
func animateA() {
subview.transform = .init(scaleX: 0.5, y: 0.5)
}
func animateB() {
subview.transform = .init(scaleX: 1.3, y: 1.3)
}
...