Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize and move a UIView with Core Animation (CAKeyFrameAnimation)

Is this possible? I can change the opacity and position (center) of the layer but whenever I try to change the size or origin, it doesn't work.

    CAAnimationGroup* anigroup = [CAAnimationGroup new];

    CGMutablePathRef thePath = CGPathCreateMutable();
    CGPathAddRect(thePath, NULL, CGRectMake(0,0, 320, 480));
    CGPathAddRect(thePath, NULL, CGRectMake(location.x - 16,location.y-24, 32, 48));
    CGPathAddRect(thePath, NULL, CGRectMake(190, 20, 32, 48));


    CAKeyframeAnimation* AniLoc = [CAKeyframeAnimation animationWithKeyPath:@"frame"];
    AniLoc.path = thePath;
    AniLoc.keyTimes= [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f],
                                             [NSNumber numberWithFloat:0.3f],
                                             [NSNumber numberWithFloat:1.0f],nil];
    AniLoc.duration = 5;

    CFRelease(thePath);

    anigroup.animations = [NSArray arrayWithObjects:AniLoc,nil];
    anigroup.duration = 5;

    [focusview.layer addAnimation:anigroup forKey:nil];
like image 875
ACBurk Avatar asked Dec 11 '09 10:12

ACBurk


1 Answers

You can both change the size and position of a view or layer in an animation, but you'll want to do so as an animation group, not by creating a path from a series of rectangles.

I provide an example of a grouped animation that moves a view along a path while shrinking it and lowering its opacity in my answer here. This works by using a CAKeyframeAnimation for the curved path that the view follows (only animating its position), and then using a basic animation to adjust the size of the view's bounds. You could also use a CAKeyframeAnimation there, with multiple CGSizes for each size keyframe you wanted, making sure to wrap them in an NSValue before placing them in an NSArray of keyframes.

To animate two properties at once, I wrap the two animations in a CAAnimationGroup and apply that to the view's layer.

like image 54
Brad Larson Avatar answered Nov 14 '22 23:11

Brad Larson