我正在学习Core Animation并试用示例示例。CATransaction设置动画持续时间不起作用
当我使用下面的代码时,动画的持续时间工作
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//Modifying base layer
self.view.layer.backgroundColor = [UIColor orangeColor].CGColor;
self.view.layer.cornerRadius = 20.0;
self.view.layer.frame = CGRectInset(self.view.layer.frame, 20, 20);
//Adding layer
mylayer=[CALayer layer]; //mylayer declared in .h file
mylayer.bounds=CGRectMake(0, 0, 100, 100);
mylayer.position=CGPointMake(100, 100); //In parent coordinate
mylayer.backgroundColor=[UIColor redColor].CGColor;
mylayer.contents=(id) [UIImage imageNamed:@"glasses"].CGImage;
[self.view.layer addSublayer:mylayer];
}
- (IBAction)Animate //Simple UIButton
{
[CATransaction begin];
// change the animation duration to 2 seconds
[CATransaction setValue:[NSNumber numberWithFloat:2.0f] forKey:kCATransactionAnimationDuration];
mylayer.position=CGPointMake(200.0,200.0);
mylayer.zPosition=50.0;
mylayer.opacity=0.5;
[CATransaction commit];
}
@end
在另一方面,如果我在viewDidLoad中按钮,以便它发生不按压任何按钮的底部集总的动画的方法的代码,动画持续时间不受尊重。我只看到没有任何动画的最终结果。
有什么想法?
感谢 九巴
感谢罗布。这工作。我猜这些括号开始提交是可选的。 – Spectravideo328
大括号是可选的。我喜欢缩进'begin'和'commit'之间的代码,并且大括号使Xcode自动缩进它。 –
我强烈推荐参考WWDC视频 - 这是一个很好的介绍了CoreAnimation的许多陷阱。 – MaxGabriel