【问题标题】:How to animate an object multiple times with delay in swift 3?如何在swift 3中延迟多次为对象设置动画?
【发布时间】:2017-07-28 15:27:12
【问题描述】:

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

//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() 函数,但这只会产生不稳定的结果。

【问题讨论】:

    标签: ios swift uiviewanimation


    【解决方案1】:

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

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

    【讨论】:

    • 我也是这么想的,但是递归函数每次被调用时都会创建一个新的范围,所以如果它的动画时间足够长,就会导致内存泄漏,从而导致你的应用程序崩溃。
    • @ScottyBlades 一定要添加一个终止条件,这样它就不会永远动画,还记得在每个动画完成块上添加[weak self] in,最后别忘了使用weak所有按钮的参考。通过这 3 项检查,您应该完全没有内存泄漏、保留周期或崩溃的问题。
    【解决方案2】:
    func buttonAnimationChain(buttonColor:UIButton, index : Int){
        UIView.animate(withDuration: 0.5, delay: 1.0, options: [],
                       animations: {
                        buttonColor.alpha = 0.0;
            },
                       completion: {finished in
                        buttonColor.alpha = 1.0;
                        if(index <= 4)
                        {
                            self.showSequence(index: index)
                        }
    
        })
    
    }
    
    
    
    func showSequence(index :  Int){
    
    
        if(index == 1){
          buttonAnimationChain(buttonColor: buttonBlue, index: index + 1)
        }
        if(index == 2){
          buttonAnimationChain(buttonColor: buttonBlue, index: index + 1)
        }
        if (index == 3){
          buttonAnimationChain(buttonColor: buttonBlue, index: index + 1)
        }
        if(index == 4){
          buttonAnimationChain(buttonColor: buttonBlue, index: index + 1)
    
        }//end of if statement
    
    }
    

    用索引1调用self.showSequence(index: 1)来启动动画

    【讨论】:

    • 问题有点复杂。我需要按特定顺序对盒子进行动画处理,而不是让盒子来回闪烁。
    • @RyanYang 请立即检查,请避免任何语法错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多