UIImageView等待动画完成

时间:2017-03-14 15:47:01

标签: ios swift3 uiimageview

我试图在第一次完成后使用isAnimating执行2个不同的动画。

但是,我只看到第一个动画......

if anims[0] == 1{
    startAnimation(image : #imageLiteral(resourceName: "first"))
}
if anims[1] == 2{
    while myView.isAnimating {
        RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.05))
    }
}
startAnimation(image : #imageLiteral(resourceName: "second") , time : Int)

spriteSheet在裁剪后返回UIImage数组..

func startAnimation(image : UIImage , time : Int){
   myView.animationImages = image.spriteSheet(cols: 19, rows: 1)
    myView.animationDuration = 1
    myView.animationRepeatCount = time
    myView.startAnimating()
}

1 个答案:

答案 0 :(得分:1)

您可以随时链接动画,例如

    UIView.animate(withDuration: 1, animations: {
        //do your animation here
    }) { (state) in
        UIView.animate(withDuration: 1, animations: {
            //do your second animation
        })
    }

如果您正在使用CABasicAnimations,那么您可以使用beginTime和duration来将它们链接起来:)

    var totalDuration = 0
    let baseicAnim1 = CABasicAnimation()
    baseicAnim1.beginTime = CACurrentMediaTime()
    totalDuration += 10
    baseicAnim1.duration = CFTimeInterval(totalDuration)

    let basicAnim2 = CABasicAnimation()
    basicAnim2.beginTime = CACurrentMediaTime() + CFTimeInterval(totalDuration)
    totalDuration += 10
    basicAnim2.duration = CFTimeInterval(totalDuration)

修改

使用while循环来检查动画是否已完成执行,这绝不是建议的方法

编辑:

试试这个,

func startAnimation(image : UIImage , time : Int,completionBlock : (()->())?){
    let animationDuration = 1
    myView.animationImages = image.spriteSheet(cols: 19, rows: 1)
    myView.animationDuration = animationDuration
    myView.animationRepeatCount = time
    myView.startAnimating()

    DispatchQueue.main.asyncAfter(deadline: .now() + animationDuration, execute: {
        if let block = completionBlock {
            block()
        }
    })
}

现在你的startAnimation函数将完成块作为参数,并在animationDuration之后执行完成块。所以你可以在动画结束时知道:)

链接简单地调用

self.startAnimation(image: #imageLiteral(resourceName: "first"), time: 1) { 
            self.startAnimation(image: #imageLiteral(resourceName: "second"), time: 1, completionBlock: nil)
        }

希望有所帮助