gpt4 book ai didi

ios - 如何在 swift 3 中多次延迟动画对象?

转载 作者:行者123 更新时间:2023-11-30 12:16:32 24 4
gpt4 key购买 nike

我想连续多次对多个按钮进行动画处理,但是当需要一个按钮进行两次或更多次动画处理时,代码根本不起作用。

//function that animates a button
func buttonAnimationChain(buttonColor:UIButton, iDelayTime: Int){
UIView.animate(withDuration: 0.5, delay: Double(iDelayTime), options: [],
animations: {
buttonColor.alpha = 0.0;
},
completion: {finished in
buttonColor.alpha = 1.0;
})

}
//function that displays the sequence
func showSequence(iGeneratedArraySequence: [Int]){

var iDelayTime:Int = 0;

for _ in 1 ... iGeneratedArraySequence.count{
if(iGeneratedArraySequence[iDelayTime] == 1){
buttonAnimationChain(buttonColor: buttonBlue, iDelayTime: iDelayTime);
}
if(iGeneratedArraySequence[iDelayTime] == 2){
buttonAnimationChain(buttonColor: buttonYellow, iDelayTime: iDelayTime);
}
if (iGeneratedArraySequence[iDelayTime] == 3){
buttonAnimationChain(buttonColor: buttonPurple, iDelayTime: iDelayTime);
}
if(iGeneratedArraySequence[iDelayTime] == 4){
buttonAnimationChain(buttonColor: buttonGreen, iDelayTime: iDelayTime);

}//end of if statement
iDelayTime += 1;
}//end of for loop
}//end of function

当生成的数组只有不重复的数字时,动画效果完美,但一旦按钮需要动画两次,则什么也不会显示。我认为发生这种情况是因为即使另一个功能将其变为事件状态,按钮仍处于非事件状态,并且我想不出解决此问题的解决方案。我尝试过使用 sleep() 函数,但这只会产生不稳定的结果。

最佳答案

问题是您一个接一个地调用动画,而不等待第一个动画结束,并且 UIView 动画是异步的。

也许您应该使用递归函数,使用第一个按钮调用它,当它结束时使用第二个按钮再次调用它,依此类推。

关于ios - 如何在 swift 3 中多次延迟动画对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45366039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com