从CABasicAnimation转换代码

时间:2014-04-12 16:57:38

标签: ios ios7 core-animation uiviewanimation cabasicanimation

我有一个使用CABasicAnimation编写的动画代码,如下所示:

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
[anim setDelegate:self];
anim.fromValue = [NSValue valueWithCGPoint:img.center];

if (newValue == 0) {
    imgCentre.y = centerStart.y - 11 * kCounterDigitDiff;
    anim.toValue = [NSValue valueWithCGPoint:imgCentre];
} else
    anim.toValue = [NSValue valueWithCGPoint:imgCentre];

anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
anim.duration = 0.3;
[img.layer addAnimation:anim forKey:@"rollLeft"];
img.frame = frame;

由于某种原因,我希望应用相同的动画,但使用以下类型的方法:

[UIView animateWithDuration:0.3 delay:0.0 options:(UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseOut) animations:^{
        ... 
    } completion:^(BOOL finished) {
         ...
        }

我想出的就像是:

[img setCenter:imgCentre];

[UIView animateWithDuration:0.3 delay:0.0 options:(UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseOut) animations:^{
    if (newValue == 0) {
        CGPoint newPoint= CGPointMake(imgCentre.x, centerStart.y - 11 * kCounterDigitDiff);
        [img setCenter:newPoint];
    } else{
        [img setCenter:imgCentre];
    }
} completion:^(BOOL finished) {
    // animation completion code
}];
img.frame = frame;

整体情况看起来不错,但我对转换有点悲观?有什么我错过了吗?

1 个答案:

答案 0 :(得分:1)

你有正确的想法。但是,与CAAnimations不同,您不必将属性设置为结束值。 UIView动画调用会为您完成所有这些操作。

UIView动画通常比CAAnimation更容易,更直观。