Animating resizing and moving UIView at the same time

I would like to “stretch” the UIView on the right side, that is, increase its frame.size.width by the pixels foo and at the same time reduce its frame.origin.x by the pixels foo using the [UIView beginAnimations] syntax. However, if I do this when the animation starts, the view immediately resizes and then starts the animation to start.

CGRect currFrame = someView.frame;
currFrame.size.width += 100;
currFrame.origin.x -= 100;

[UIView beginAnimations:@"Anim1" context:nil];
someView.frame = currFrame;
[UIView setAnimationDuration:0.7];
[UIView commitAnimations];

I also tried to split the animation into two parts, but then I can’t keep the correct position in place.

Any help is much appreciated!

+5
source share
1 answer

You may need to collapse in Core Animation and break it into two explicit animations:

CABasicAnimation *stetchAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
stetchAnimation.toValue = [NSNumber numberWithDouble:(someView.frame.size.width+100)/someView.frame.size.width];

CABasicAnimation *slideAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
slideAnimation.toValue = [NSNumber numberWithDouble:-100];

CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.animations = [NSArray arrayWithObjects:stetchAnimation,slideAnimation,nil];
animationGroup.removedOnCompletion = NO;
animationGroup.fillMode = kCAFillModeForwards;
animationGroup.duration = 0.7;

[someView.layer addAnimation:animationGroup forKey:@"animations"];
+4
source

All Articles