【问题标题】:Animating resizing and moving UIView at the same time同时动画调整大小和移动 UIView
【发布时间】:2010-03-17 06:42:24
【问题描述】:

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

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

我也尝试将动画分成两部分,但我无法保持正确的位置。

非常感谢任何帮助!

【问题讨论】:

  • 您找到解决方案了吗?我也遇到了同样的问题,但很奇怪,为什么视图会立即调整大小然后开始动画?

标签: iphone cocoa-touch uiview core-animation


【解决方案1】:

您可能需要下拉到 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"];

【讨论】:

  • 谢谢,但这会扩大视野。我需要的是改变框架,所以它可以相应地重新布局它的子视图。基本上,它类似于 Mobile Safari 中的搜索字段行为(当您点击它时,它会向左扩展)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-04
  • 2013-07-16
  • 2023-04-02
  • 2010-12-25
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
相关资源
最近更新 更多