简体   繁体   中英

Animating resizing and moving UIView at the same time

I'd like to "Stretch" a UIView to the right side, meaning increase it's frame.size.width by foo pixels and at the same time decreasing it's frame.origin.x by foo pixels, using [UIView beginAnimations] syntax. However, if I do that, when the animation begins the view immediately resizes, and then starts the animation for the origin.

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've also tried breaking the animation down to 2 parts but then I can't keep the right position in place.

Any help is much appreciated!

You might need to drop down to Core Animation and break this 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"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM