Iphone 动画完成后,CABasicAnimation不更改其“位置”属性
我正在为层的位置属性应用CABasicAnimation,它的动画效果很好,但在动画完成后,它会返回到原始位置。如何避免这种情况,并使图像仅保持在动画位置?有几种不同的方法来解决此问题。我最喜欢的是在动画结束时设置对象,并使用fromValue而不是toValue来创建动画。也可以同时设置fromValue和toValue来创建此效果。即Iphone 动画完成后,CABasicAnimation不更改其“位置”属性,iphone,ios,opengl-es,core-animation,Iphone,Ios,Opengl Es,Core Animation,我正在为层的位置属性应用CABasicAnimation,它的动画效果很好,但在动画完成后,它会返回到原始位置。如何避免这种情况,并使图像仅保持在动画位置?有几种不同的方法来解决此问题。我最喜欢的是在动画结束时设置对象,并使用fromValue而不是toValue来创建动画。也可以同时设置fromValue和toValue来创建此效果。即 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.duration = 1.0;
animation.fromValue = [NSValue valueWithCGPoint:startPoint];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
viewToAnimate.center = endPoint;
[viewToAnimate.layer addAnimation:animation forKey:@"slide"];
另一种方法是将自己设置为委托人并实施:
-(void)animationDidStop:(id)sender finished:(BOOL)finished {
viewToAnimate.center = endPoint;
}
您需要设置最终位置。如果仅使用动画fromValue和toValue,它将捕捉回原始起点。动画在“播放”时使用视图/层的副本,然后动画完成后将显示原始视图/层 如果不显式设置,它将显示为回缩,即使视图/图层从未真正移动 startPoint和endPoint是CGPoints,myView是正在设置动画的UIView
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.duration = .3;
animation.fromValue = [NSValue valueWithCGPoint:startPoint];
animation.toValue = [NSValue valueWithCGPoint:endPoint];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[myView.layer addAnimation:animation forKey:@"position"];
myView.layer.position = endPoint; // Must set end position, otherwise it jumps back
如果您查看addAnimation:forKey:的文档,它会说“通常这是隐式调用的”。换句话说,你不应该直接给它打电话。实际上,您需要执行以下操作,这比设置“从”和“到”值更容易,您只需直接在图层上设置值:
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation.duration = .3;
myLayer.actions = @{@"opacity": animation};
myLayer.opacity = 0;
这将使层的不透明度变为零。要在动画结束时设置对象,只需编写如下代码
#import <QuartzCore/QuartzCore.h>
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.duration = 1.0;
animation.fromValue = [NSValue valueWithCGPoint:startPoint];
animation.timingFunction = [CAMediaTimingFunction functionWithName:easing];
animation.fillMode = kCAFillModeForwards; // This line will stop the animation at the end position of animation
animation.autoreverses = NO;
viewToAnimate.center = endPoint;
[viewToAnimate.layer addAnimation:animation forKey:@"slide"];
#导入
cabasicanition*animation=[cabasicanition animationWithKeyPath:@“position”];
animation.duration=1.0;
animation.fromValue=[NSValue valueWithCGPoint:startPoint];
animation.timingFunction=[CAMediaTimingFunction,名称:easing];
animation.fillMode=kCAFillModeForwards;//此行将在动画的结束位置停止动画
animation.autoreverses=否;
viewToAnimate.center=端点;
[viewToAnimate.layer addAnimation:animation forKey:@“slide”];
如果要保留最终值,只需将removedOnCompletion设置为“否”。别忘了设置填充模式
斯威夫特4+
目标-C
animation.fillMode = kCAFillModeForwards; //The receiver remains visible in its final state when the animation is completed.
animation.removedOnCompletion = NO;
另外,不要忘记将动画的removedOnCompletion属性设置为NO
animation.removedOnCompletion=NO代码>这里,“放松”的价值是什么?我相信如果不设置放松,它会预加载到线性,但如果您正在寻找放松/放松或放松/放松,那么添加它应该很简单。我在文档中没有看到这一点,这可能只是OSX的事情吗?我在看卡莱尔。大多数苹果教程都包括对addAnimation:forKey:
的调用,例如。
animation.fillMode = kCAFillModeForwards; //The receiver remains visible in its final state when the animation is completed.
animation.removedOnCompletion = NO;