0

我可以像这样动画地移动一个图层:

CABasicAnimation *animation = [CABasicAnimation animation];
[animation setFromValue:[NSValue valueWithCGPoint:self.layer.position]];
[animation setToValue:[NSValue valueWithCGPoint:CGPointMake(100, 100)]];
[self.layer addAnimation:animation forKey:@"position"];
self.layer.position = CGPointMake(100, 100);

但是,我必须在动画结束后设置 layer.position,有没有办法在使用显式动画时自动设置它?

4

1 回答 1

0

根据 Apple 文档,您必须手动设置:

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreAnimation_guide/CreatingBasicAnimations/CreatingBasicAnimations.html#//apple_ref/doc/uid/TP40004514-CH3-SW3

CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeAnim.fromValue = [NSNumber numberWithFloat:1.0];
fadeAnim.toValue = [NSNumber numberWithFloat:0.0];
fadeAnim.duration = 1.0;
[theLayer addAnimation:fadeAnim forKey:@"opacity"];

// Change the actual data value in the layer to the final value.
theLayer.opacity = 0.0;

与更新图层对象数据值的隐式动画不同,显式动画不会修改图层树中的数据。显式动画只产生动画。在动画结束时,Core Animation 从图层中移除动画对象并使用其当前数据值重新绘制图层。如果您希望显式动画的更改是永久的,您还必须更新图层的属性,如前面的示例所示。

于 2013-03-04T16:33:29.660 回答