动画调整UIView和子视图大小的更好方法?

Jus*_*tin 9 iphone objective-c uiview ios ios7

屏幕截图示例:

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

有什么更好的方法来动画调整包含子视图的UIView的大小?

我目前的方法:

在屏幕截图中,父UIView(橙色)有一个子视图UILabel(黄色+自动调整).animateWithDuration动画调整父UIView框架的大小.此方法的问题是它没有为子视图的大小调整设置动画.它突然改变,它应该动画缩放.

是否最明确地为子视图设置动画,还是有更有效的方法?(例如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) {
                     }];
}
Run Code Online (Sandbox Code Playgroud)

小智 3

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

当 UILabel 更改 UIViewAnimation 中的帧大小时,将立即缩放

除非你使用CGAffineTransformScale


一些例子

什么时候

[self.view addSubview:view_parent];
[view_parent addSubview:view_component];
Run Code Online (Sandbox Code Playgroud)

如果 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)];
}];
Run Code Online (Sandbox Code Playgroud)

工作正常吗

但是当 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)];
}];
Run Code Online (Sandbox Code Playgroud)

望帮忙~