动画调整大小并同时移动UIView

时间:2010-03-17 06:42:25

标签: iphone cocoa-touch uiview core-animation

我想向右侧“拉伸”一个UIView,意思是通过foo像素增加它的frame.size.width,同时使用[UIView beginAnimations]通过foo像素减少它的frame.origin.x句法。 但是,如果我这样做,当动画开始时,视图会立即调整大小,然后启动原点的动画。

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

我也尝试将动画分解为2部分,但后来我无法保持正确的位置。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:4)

您可能需要下拉到Core Animation并将其分解为两个明确的动画:

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"];