【问题标题】:setDuration only affects one CALayer when animating multiple CALayers设置多个 CALayer 动画时,setDuration 仅影响一个 CALayer
【发布时间】:2014-06-25 04:00:47
【问题描述】:

我正在尝试同时为多个 CALayers 的位置变化设置动画。隐式动画似乎工作正常,但如果我尝试为每个 CALayer 的持续时间属性显式指定一个值,则只有一个动画,而另一个成功更改但没有任何动画。这是我正在使用的代码:

CGPoint initialPt = CGPointMake(160.0, 10);
CGPoint pt2 = CGPointMake(160.0, 185.0);
CGPoint pt3 = CGPointMake(160.0, 270.0);

CABasicAnimation *firstAnim = [CABasicAnimation animationWithKeyPath:@"postion"];
[firstAnim setFromValue:[NSValue valueWithCGPoint:initialPt]];
[firstAnim setToValue:[NSValue valueWithCGPoint:pt2]];
[firstAnim setDuration:1.0];
[layer1 setPosition:p2];
[layer1 addAnimation:firstAnim forKey:@"Slide"];

CABasicAnimation *secondAnim = [CABasicAnimation animationWithKeyPath:@"position"];
[secondAnim setFromValue:[NSValue valueWithCGPoint:initialPt]];
[secondAnim setToValue:[NSValue valueWithCGPoint:pt3]];
[secondAnim setDuration:1.0];
[layer2 setPosition:p3];
[layer2 addAnimation:secondAnim forKey:@"Slide2"];

我也已经尝试过使用 CATransaction,但没有任何改变。如果我注释掉 setDuration 方法,两个图层都会毫无问题地为位置更改设置动画,除了它们处于固定持续时间。似乎出于某种原因明确地设置动画会使事情变得混乱,但我不知道为什么。任何帮助将非常感激!谢谢。

【问题讨论】:

  • 为什么附加代码有编译错误?你不是从你的 xcode 中复制代码吗?
  • @Danyun 对不起!为了简洁起见,我更改了一些内容。

标签: ios core-animation calayer


【解决方案1】:

问题是你打错字了!

CABasicAnimation *firstAnim = [CABasicAnimation animationWithKeyPath:@"postion"]; change the 'postion' to 'position'

应该是

CABasicAnimation *firstAnim = [CABasicAnimation animationWithKeyPath:@"position"]; change the 'postion' to 'position'

'位置'到位置

【讨论】:

  • 哇.. 我不敢相信这是问题所在。现在可以了。出于某种原因,Xcode 没有给我一个编译错误,因为 keyPath 拼写错误。谢谢@Danyun。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-25
  • 1970-01-01
  • 2017-10-12
  • 1970-01-01
相关资源
最近更新 更多