Firdous
Firdous

Reputation: 4652

Converting code from CABasicAnimation

I have a animation code that is written using CABasicAnimation just as following:

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;

Due to some reason I wish to apply same animation but by using following type of approach:

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

What I come up with is like following:

[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;

Overall things look good but I am a little bit pessimistic about the conversion? is there something that i have missed?

Upvotes: 0

Views: 191

Answers (1)

Duncan C
Duncan C

Reputation: 131398

You've got the right idea. However, unlike with CAAnimations, you don't have to set the properties to the end value. The UIView animation call takes care of all that for you.

UIView animation is generally easier and more intuitive to use than CAAnimation.

Upvotes: 1

Related Questions