【发布时间】:2014-01-21 05:28:52
【问题描述】:
我只是想知道这是否是使用 CABasicAnimation 为 CALayer 设置动画的正确方法。
在 Stack Overflow 上,我学会了如何通过在运行 CABasicAnimation 之前设置新位置来为 UI 对象设置动画:
动画 UI 对象示例
gameTypeControl.center = CGPointMake(gameTypeControl.center.x, -slidingUpValue/2);
CABasicAnimation *removeGameTypeControl = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
[removeGameTypeControl setFromValue:[NSNumber numberWithFloat:slidingUpValue]];
[removeGameTypeControl setToValue:[NSNumber numberWithFloat:0]];
[removeGameTypeControl setDuration:1.0];
[removeGameTypeControl setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:0.8 :-0.8 :1.0 :1.0]];
[[gameTypeControl layer] addAnimation:removeGameTypeControl forKey:@"removeGameTypeControl"];
现在我已经在 CALayer 上尝试过这种方法,但它的工作方式似乎有所不同。让我得到同样的结果。我将 ToValue 设置为新的 y 位置,而不是像我对 UI 对象动画所做的那样使用值 0。
为 CALayer 示例制作动画
serveBlock2.position = CGPointMake((screenBounds.size.height/4)*3, -screenBounds.size.width/2);
CABasicAnimation *updateCurrentServe2 = [CABasicAnimation animationWithKeyPath:@"position.y"];
updateCurrentServe2.fromValue = [NSNumber numberWithFloat:slidingUpValue/2];
[updateCurrentServe2 setToValue:[NSNumber numberWithFloat:-screenBounds.size.width/2]];
[updateCurrentServe2 setDuration:1.0];
[serveBlock2 addAnimation:updateCurrentServe2 forKey:@"serveBlock2 updateCurrentServe2"];
这是正确的吗?我这样做对吗?
【问题讨论】:
-
serveBlock2是一个 CALayer。[gameTypeControl layer]是一个 CALayer。例子都是一样的。
标签: ios core-animation calayer cabasicanimation