我的代码工作正常,并在根视图控制器中正确设置动画。 但当我将它移动到另一个VC并使用presentViewController在那里导航时,图像静态显示并且没有动画。
@IBAction func glassFillingButtonClicked(sender: UIButton) {
let storyBoard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("waterFilling") as! GlassFillingViewController
self.presentViewController(nextViewController, animated:true, completion:nil)
class GlassFillingViewController: UIViewController {
@IBOutlet weak var dropView: UIView!
@IBOutlet weak var dropImage: UIImageView!
@IBOutlet weak var dropImage2: UIImageView!
@IBOutlet weak var dropImage3: UIImageView!
@IBOutlet weak var dropImage4: UIImageView!
@IBOutlet weak var dropImage5: UIImageView!
@IBOutlet weak var dropImage6: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
dropAnimation()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addWaterDroppingAnimation(beginTime: CFTimeInterval) -> CAKeyframeAnimation {
let waterDroppingAnimation = CAKeyframeAnimation.init(keyPath: "opacity")
waterDroppingAnimation.beginTime = beginTime
waterDroppingAnimation.duration = 1.0
waterDroppingAnimation.values = [0.2,1,0.2]
waterDroppingAnimation.repeatCount = HUGE
return waterDroppingAnimation
}
func dropAnimation() {
dropImage.layer.addAnimation(addWaterDroppingAnimation(0.0), forKey: "basic")
dropImage2.layer.addAnimation(addWaterDroppingAnimation(0.2), forKey: "basic")
dropImage3.layer.addAnimation(addWaterDroppingAnimation(0.4), forKey: "basic")
dropImage4.layer.addAnimation(addWaterDroppingAnimation(0.6), forKey: "basic")
dropImage5.layer.addAnimation(addWaterDroppingAnimation(0.8), forKey: "basic")
dropImage6.layer.addAnimation(addWaterDroppingAnimation(1.0), forKey: "basic")
}
}
答案 0 :(得分:2)
您不应该在viewDidLoad中执行动画,因为它刚刚初始化并且还没有超视图,因此动画不起作用。尝试将代码移动到viewDidAppear中。