2010-11-19 42 views
2

我想不通为什么这个代码:CATransaction是立竿见影的,而忽略过渡时间

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。

回答

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"];