lef*_*pin 1 cabasicanimation ios caemitterlayer
I am trying to animate a CAEmitterLayer's emitterPosition like this:
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"emitterPosition.x"] ;
animation.toValue = (id) toValue ;
animation.removedOnCompletion = NO ;
animation.duration = self.translationDuration ;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut] ;
animation.completion = ^(BOOL finished)
{
[self animateToOtherSide] ;
} ;
[_emitterLayer addAnimation:animation forKey:@"emitterPosition"] ;
CGPoint newEmitterPosition = CGPointMake(toValue.floatValue, self.bounds.size.height/2.0) ;
_emitterLayer.emitterPosition = newEmitterPosition ;
Run Code Online (Sandbox Code Playgroud)
Note that animation.completion is declared in a category that just calls the corresponding CAAnimation delegate method.
The problem is that this doesn't animate at all and shows the emitter in its final position. I was under the impression that once you add the animation to the layer, you should change the actual model behind it to its final position so that when the animation completes the model is in its final state; i.e., to prevent the animation from "snapping back" to its original position.
I have tried placing the last two lines in the animation.completion block, and this does indeed animate as expected. However, when the animation finishes some particles are intermittently emitted at the emitter's original position. If you put the system under load (for example, scrolling a tableview while the animation is playing), this happens more often.
Another solution I was thinking about is to not move the emitterPosition at all but just move the CAEmitterLayer itself, although I haven't tried that yet.
Thanks in advance for any help you can provide.
也许emitterPosition.x不是动画的有效关键路径.请尝试使用emitterPosition(因此您必须提供包含在NSValue中的CGPoint值).
我只是在自己的机器上试过这个并且工作正常:
CABasicAnimation* ba = [CABasicAnimation animationWithKeyPath:@"emitterPosition"];
ba.fromValue = [NSValue valueWithCGPoint:CGPointMake(30,100)];
ba.toValue = [NSValue valueWithCGPoint:CGPointMake(200,100)];
ba.duration = 6;
ba.autoreverses = YES;
ba.repeatCount = HUGE_VALF;
[emit addAnimation:ba forKey:nil];
Run Code Online (Sandbox Code Playgroud)
其他需要考虑的事情:
你通常可以使用nil作为键addAnimation:forKey:,除非你以后需要找到这个动画(例如删除它或以某种方式覆盖它).关键路径是重要的.
设置removedOnCompletion为NO几乎总是错误的,并且通常是恶棍的最后避难所(即由于不理解动画如何工作).
如果,如你所说,_emitterLayer.emitterPosition = newEmitterPosition在完成块内设置动画,那么你为什么要使用CABasicAnimation?为什么不调用UIView animate...并在动画块中设置emitterPosition?如果这样可行的话,它将一石二鸟,移动位置并使其动画化.
| 归档时间: |
|
| 查看次数: |
1944 次 |
| 最近记录: |