一个更好的方法来animation调整UIView和子视图的大小?

示例屏幕截图:

http://i.stack.imgur.com/CyOQi.gif

什么是一个更好的方式来animation包含子视图的UIView的大小调整?

我目前的方法:

在截屏中,父UIView(橙色)有一个子视图UILabel(黄色+自动调整)。 animateWithDurationanimation调整父UIView框架的大小。 这种方法的问题是它不animation子视图的大小调整。 它突然改变,它应该在那里animation地缩放。

是明确animation的子视图最好的,还是有一个更有效的方法? (如CGAffineTransform?)

示例代码:

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.parentView = [[UIView alloc] initWithFrame:CGRectMake(10, 100, 200, 200)]; self.parentView.backgroundColor = [UIColor orangeColor]; [self.view addSubview:self.parentView]; self.childLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 180, 180)]; self.childLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; self.childLabel.backgroundColor = [UIColor yellowColor]; [self.parentView addSubview:self.childLabel]; [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(randomizeParentViewSize) userInfo:nil repeats:YES]; } - (void)randomizeParentViewSize { CGFloat width = arc4random_uniform(100) + 50; CGFloat height = arc4random_uniform(100) + 50; [UIView animateWithDuration:1.5f delay:0.0f options:0 animations:^{ self.parentView.frame = CGRectMake(10, 50 + height, width, height); } completion:^(BOOL finished) { }]; } 

我猜你的黄色子视图是UILabel?

当UILabel改变UIViewAnimation中的帧大小将立即缩放

除非你使用CGAffineTransformScale


一些例子

什么时候

 [self.view addSubview:view_parent]; [view_parent addSubview:view_component]; 

如果view_parent和view_component都是UIView

 [UIView animateWithDuration:2.0 animations:^{ [view_parent setFrame:CGRectMake(0, 20, 160, 160)]; [view_component setFrame:CGRectMake(20, 20, 80, 80)]; }]; 

工作正常

但是当view_parent是UIView和view_component是UILabel

你需要做一些像…

 [UIView animateWithDuration:2.0 animations:^{ [view_parent setFrame:CGRectMake(0, 20, 160, 160)]; view_component.transform = CGAffineTransformScale(view_component.transform, 0.5, 0.5); [view_component setFrame:CGRectMake(20, 20, 80, 80)]; }]; 

祝帮助〜

要animation的视图和子视图,你可以使用这个代码:

 [UIView animateWithDuration:0.5f animations:^{ self.testView.transform = CGAffineTransformMakeScale(0.5, 0.5); }]; 

更多信息

如果您还使用CGAffineanimation,则无法使用框架,边界或中心。因此,如果您还需要移动视图,请使用如下所示的内容:

  CGAffineTransform transform = CGAffineTransformMakeTranslation(150,400); transform = CGAffineTransformScale(transform, 0.5, 0.5); 

使用UIViewAnimationOptionLayoutSubviews:

  1. 在子视图上添加正确的前导,尾随,顶部和底部约束。
  2. [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{ //resize the superview } completion:nil];