等待斯威夫特动画执行code前完成 [英] Wait for Swift animation to complete before executing code

查看:135
本文介绍了等待斯威夫特动画执行code前完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动画一个UIImageView然后隐藏图像视图动画完成后。然而在完成动画前的ImageView被隐藏。我看了看类似的问题,他们建议实施一个动画听众或完成时,动画code内执行.hidden code但是我不知道如何在shakeView内影响这个()函数如下

我如何展示摇动画和隐藏只有在动画完成图像的看法?

动画使用下列code被称为:

  shakeView(image1的!)
shakeView(IMAGE2)
此搜索!.hidden =真
image2.hidden =真

动画功能本身看起来是这样的:

  FUNC shakeView(四:UIImageView的){
    VAR摇:CABasicAnimation = CABasicAnimation(的keyPath:位置)
    shake.duration = 0.1
    shake.repeatCount = 2
    shake.autoreverses =真    VAR from_point:CGPoint = CGPointMake(iv.center.x - 5,iv.center.y)
    VAR from_value:NSValue = NSValue(CGPoint:from_point)    VAR to_point:CGPoint = CGPointMake(iv.center.x + 5,iv.center.y)
    VAR to_value:NSValue = NSValue(CGPoint:to_point)    shake.fromValue = from_value
    shake.toValue = to_value
    iv.layer.addAnimation(摇,forKey:位置)
}


解决方案

您可以使用 CATransaction 动画完成后调用完成块。

  FUNC shakeView(四:UIImageView的){    CATransaction.begin()
    CATransaction.setCompletionBlock({
        iv.hidden =真
    })
    VAR摇:CABasicAnimation = CABasicAnimation(的keyPath:位置)
    shake.duration = 0.1
    shake.repeatCount = 21
    shake.autoreverses =真    VAR from_point:CGPoint = CGPointMake(iv.center.x - 5,iv.center.y)
    VAR from_value:NSValue = NSValue(CGPoint:from_point)    VAR to_point:CGPoint = CGPointMake(iv.center.x + 5,iv.center.y)
    VAR to_value:NSValue = NSValue(CGPoint:to_point)    shake.fromValue = from_value
    shake.toValue = to_value
    iv.layer.addAnimation(摇,forKey:位置)
    CATransaction.commit()
}

或者你也可以组中的两个动画一起在一个 CATransaction 如下:

  FUNC shakeView(四:UIImageView的){
    VAR摇:CABasicAnimation = CABasicAnimation(的keyPath:位置)
    shake.duration = 0.1
    shake.repeatCount = 21
    shake.autoreverses =真    VAR from_point:CGPoint = CGPointMake(iv.center.x - 5,iv.center.y)
    VAR from_value:NSValue = NSValue(CGPoint:from_point)    VAR to_point:CGPoint = CGPointMake(iv.center.x + 5,iv.center.y)
    VAR to_value:NSValue = NSValue(CGPoint:to_point)    shake.fromValue = from_value
    shake.toValue = to_value
    iv.layer.addAnimation(摇,forKey:位置)
}覆盖FUNC viewDidLoad中(){
    CATransaction.begin()    CATransaction.setCompletionBlock({
        self.image1.hidden =真
        self.image2.hidden =真
    })
    shakeView(此搜索)
    shakeView(图)
  CATransaction.commit()
}

I am trying to animate a UIImageView and then hide the image view after animation is complete. However the imageview gets hidden before the animation is completed. I looked at similar questions and they recommend implementing an animation listener or executing the .hidden code within the animation code upon completion however I'm not sure how to affect this within the shakeView() function below.

How do I show the shake animation and hide the image view only after the animation is complete?

Animation is called using the following code:

shakeView(image1!)
shakeView(image2)
image1!.hidden = true
image2.hidden = true

The animation function itself looks like this:

func shakeView(iv: UIImageView){
    var shake:CABasicAnimation = CABasicAnimation(keyPath: "position")
    shake.duration = 0.1
    shake.repeatCount = 2
    shake.autoreverses = true

    var from_point:CGPoint = CGPointMake(iv.center.x - 5, iv.center.y)
    var from_value:NSValue = NSValue(CGPoint: from_point)

    var to_point:CGPoint = CGPointMake(iv.center.x + 5, iv.center.y)
    var to_value:NSValue = NSValue(CGPoint: to_point)

    shake.fromValue = from_value
    shake.toValue = to_value
    iv.layer.addAnimation(shake, forKey: "position")
}

解决方案

You can use a CATransaction to call a completion block after the animation completes.

func shakeView(iv: UIImageView){

    CATransaction.begin()
    CATransaction.setCompletionBlock({
        iv.hidden = true
    })
    var shake:CABasicAnimation = CABasicAnimation(keyPath: "position")
    shake.duration = 0.1
    shake.repeatCount = 21
    shake.autoreverses = true

    var from_point:CGPoint = CGPointMake(iv.center.x - 5, iv.center.y)
    var from_value:NSValue = NSValue(CGPoint: from_point)

    var to_point:CGPoint = CGPointMake(iv.center.x + 5, iv.center.y)
    var to_value:NSValue = NSValue(CGPoint: to_point)

    shake.fromValue = from_value
    shake.toValue = to_value
    iv.layer.addAnimation(shake, forKey: "position")
    CATransaction.commit()
}

Or you can also group both animations together in a CATransaction as follows.

func shakeView(iv: UIImageView){
    var shake:CABasicAnimation = CABasicAnimation(keyPath: "position")
    shake.duration = 0.1
    shake.repeatCount = 21
    shake.autoreverses = true

    var from_point:CGPoint = CGPointMake(iv.center.x - 5, iv.center.y)
    var from_value:NSValue = NSValue(CGPoint: from_point)

    var to_point:CGPoint = CGPointMake(iv.center.x + 5, iv.center.y)
    var to_value:NSValue = NSValue(CGPoint: to_point)

    shake.fromValue = from_value
    shake.toValue = to_value
    iv.layer.addAnimation(shake, forKey: "position")
}

override func viewDidLoad() {
    CATransaction.begin()

    CATransaction.setCompletionBlock({
        self.image1.hidden = true
        self.image2.hidden = true
    })
    shakeView(image1)
    shakeView(image)
  CATransaction.commit()
}

这篇关于等待斯威夫特动画执行code前完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆