在执行代码之前等待Swift动画完成

zee*_*han 31 animation ios swift

我试图动画UIImageView,然后在动画完成后隐藏图像视图.但是,在动画完成之前,imageview会被隐藏.我查看了类似的问题,他们建议在完成后实现动画监听器或在动画代码中执行.hidden代码但是我不确定如何在下面的shakeView()函数中影响它.

如何在动画完成后显示摇动动画并隐藏图像视图?

使用以下代码调用动画:

shakeView(image1!)
shakeView(image2)
image1!.hidden = true
image2.hidden = true
Run Code Online (Sandbox Code Playgroud)

动画功能本身如下所示:

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")
}
Run Code Online (Sandbox Code Playgroud)

rak*_*hbs 70

CATransaction动画完成后,您可以使用a 调用完成块.

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()
}
Run Code Online (Sandbox Code Playgroud)

或者您也可以将两个动画组合在一起,CATransaction如下所示.

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()
}
Run Code Online (Sandbox Code Playgroud)


Lyn*_*ott 16

正如Paulw11在他的评论中提到的,你可以使用animationDidStop:文档中提到的方法.但此外,您应该为CABasicAnimation/CAAnimation对象添加一个键,以便您的animationDidStop:方法知道特定的对象已完成.

所以在你的摇动方法中添加一个值键对来识别动画并将委托设置为self,例如:

func shakeView(iv: UIImageView){

    var shake:CABasicAnimation = CABasicAnimation(keyPath: "position")
    shake.duration = 0.1
    shake.repeatCount = 2
    shake.autoreverses = true

    // Add these two lines:
    shake.setValue("shake", forKey: "animationID")
    shake.delegate = self

    // ...
Run Code Online (Sandbox Code Playgroud)

然后添加animationDidStop:委托方法以提醒您动画何时完成,以便您可以开始执行代码:

override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
    // Unwrap the optional value for the key "animationID" then
    // if it's equal to the same value as the relevant animation,
    // execute the relevant code
    if let animationID: AnyObject = anim.valueForKey("animationID") {
        if animationID as NSString == "shake" {
            // execute code
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记在 viewController 中符合 CAAnimationDelegate (3认同)
  • 正在寻找这个确切的例子。谢谢! (2认同)