CATransaction是立即的,并忽略过渡时间

时间:2010-11-19 09:37:37

标签: iphone objective-c cocoa-touch uikit core-animation

我无法弄清楚为什么这段代码:

CALayer *viewLayer = [aView layer];
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:10.0f]
                 forKey:kCATransactionAnimationDuration];
viewLayer.position = CGPointMake(200.0f, 200.0f);
viewLayer.position = CGPointMake(320.0f, 480.0f);

[CATransaction commit];

移动视图,但移动不动画(移动是立即的)。 aView是UIView中的UIImageView。

1 个答案:

答案 0 :(得分:4)

对与视图关联的图层禁用隐式操作。视图始终是其自己的图层的委托,并且它实现-actionForKey:禁用隐式动画,并且仅在UIView动画块内添加动画。最好的办法是简单地使用明确的CABasicAnimations。假设您想要从第一个点到第二个点进行动画处理,您可以使用类似

的内容
CALayer *layer = aView.layer;

layer.position = CGPointMake(320, 480); // final position
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
anim.toValue = [NSValue valueWithCGPoint:layer.position]; // I believe this line is optional, it should default to current position
[layer addAnimation:anim forKey:@"position"];