【发布时间】:2017-03-29 14:32:00
【问题描述】:
我正在尝试使用CAEmitterLayer 和几个CAEmitterCell 为爆炸制作动画。这应该在用户看到视图后的短暂延迟后发生。我在viewDidAppear 开始我的动画。
粒子动画本身可以正常工作,但在这个问题中 Initial particles from CAEmitterLayer don't start at emitterPosition 除非我设置 emitterLayer.beginTime = CACurrentMediaTime() 动画对用户来说似乎已经运行了一段时间。
现在要真正实现爆炸,我必须在某个时刻停止发射粒子。我尝试使用此代码设置CABasicAnimation,它会在一段时间后停止发射器:
// emitter layer is reused (remove all animations, remove all cells, remove from superlayer)
... // emitter layer setup in a function "explode" which is called from viewDidAppear
emitterLayer.beginTime = CACurrentMediaTime()
let birthRateAnimation = CABasicAnimation(keyPath: "birthRate")
birthRateAnimation.toValue = 0
birthRateAnimation.timingFunction = CAMediaTimingFunction.init(name:kCAMediaTimingFunctionEaseOut)
birthRateAnimation.beginTime = CACurrentMediaTime() + 1
birthRateAnimation.duration = 5
birthRateAnimation.delegate = self // (self is view controller)
birthRateAnimation.setValue("expl", forKey: "animName")
emitterLayer.add(birthRateAnimation, forKey: "birthRateAnimation")
self.view.layer.addSublayer(emitterLayer)
所以现在这个代码birthRateAnimation实际上并没有被触发。我登录了animationDidStop 和animationDidStart,它们不打印任何内容。
现在,如果我在点击按钮时调用 explode 函数,我根本不会看到 no 粒子动画,但在日志中我会看到“动画开始”/“动画停止”消息。
知道为什么吗?
【问题讨论】:
标签: ios cabasicanimation caanimation caemitterlayer