在执行代码之前等待Swiftanimation完成

我想animation一个UIImageView,然后隐藏animation完成后的图像视图。 但是,在animation完成之前,imageview会隐藏起来。 我看了类似的问题,他们build议实现一个animation监听器或完成后执行animation代码中的.hidden代码,但我不知道如何影响下面的shakeView()函数。

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

使用以下代码调用animation:

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

animationfunction本身如下所示:

 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") } 

您可以使用CATransaction在animation完成后调用完成块。

 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() } 

或者,您也可以按以下方式将CATransaction两个animation组合在一起。

 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() } 

正如Paulw11在他的评论中提到的那样,您可以使用文档中提到的animationDidStop:方法。 但是,您应该为您的CABasicAnimation/CAAnimation对象添加一个键值,以使您的animationDidStop:方法知道哪一个特别完成了。

所以在你的摇动方法中添加一个值 – 密钥对来识别animation,并将委托设置为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 // ... 

然后添加animationDidStop: delegate方法来提醒您animation何时完成,以便您可以开始执行代码:

 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 } } }