【问题标题】:CoreAnimation CABasicAnimation, when to add the layerCoreAnimation CABasicAnimation,何时添加图层
【发布时间】:2012-01-27 03:41:20
【问题描述】:

我正在阅读 Apple 关于 CA 的文档,并尝试制作一个简单的动画。我想知道为什么这不起作用:

CABasicAnimation *goRight = [CABasicAnimation animationWithKeyPath:@"position"];
goRight.duration = 5;
goRight.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
NSLog(@":%@", [goRight.fromValue description]);
goRight.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
CALayer *thelayer = [CALayer layer];
[thelayer addAnimation:goRight forKey:nil];
[self.aView.layer addSublayer:thelayer];

但如果我将最后几行更改为:

//    CALayer *thelayer = [CALayer layer];
//    [thelayer addAnimation:goRight forKey:nil];
//    [self.aView.layer addSublayer:thelayer];
    [self.aView.layer addAnimation:goRight forKey:nil];

这行得通。我想我在某处读到了 .layer 属性上的隐式或显式动画已关闭,所以我认为我应该练习将子层添加到主 .layer 属性并让动画作用于该层。

有人可以帮助解释我做错了什么吗?谢谢。

【问题讨论】:

    标签: iphone core-animation


    【解决方案1】:

    您正在创建一个没有内容的全新图层并为其添加动画。有两个问题:

    1. 图层默认大小为0×0,您无需更改其大小。您需要通过设置其framebounds 使其大于0×0。
    2. 图层没有使其可见的内容或其他属性。有很多方法可以让它可见。一种方法是设置其背景颜色。例如:

      thelayer.backgroundColor = UIColor.redColor.CGColor;
      

    另外,当你将动画添加到图层时,通常应该使用动画属性名称作为键。例如:

    [thelayer addAnimation:goRight forKey:@"position"];
    

    如果你想学习核心动画,我强烈推荐观看WWDC videos。有两个视频,Core Animation in Practice 第 1 部分和第 2 部分,来自 WWDC 2010。有一个视频,Core Animation Essentials,来自 WWDC 2011。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 2014-09-28
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      相关资源
      最近更新 更多