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

10

示例屏幕截图:

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) {
                     }];
}

1
不,子视图应该有动画效果。请展示一下您的动画代码,并描述子视图的自动调整大小或自动布局约束。 - matt
Matt,我在我的问题中添加了代码。当橙色父视图的框架发生变化时,它会进行动画,但子视图却没有。 - Justin
找到了一个解释:子UILabel默认不进行动画处理,因为它的默认内容模式为UIViewContentModeRedraw:https://dev59.com/questions/VHI-5IYBdhLWcg3wsKh4 - Justin
已经过去了几年,但在你的解释下,我避免了数小时的挫败感并解决了我的动画问题。在我的案例中,不是UILabel而是ARSCNView。其默认的内容模式是.center; 更改为.scaleToFill即可解决我的问题。因此,非常感谢您!! - Dave Goldman
3个回答

3

如果您想要动画显示视图及其子视图,您可以使用以下代码:

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

更多信息

如果您使用CGAffine进行动画操作,则无法同时使用frame、bounds或center。因此,如果您还需要移动视图,请使用以下方法:

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

3
我猜你的黄色子视图是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)];
}];

Wish help ~


1

使用UIViewAnimationOptionLayoutSubviews:

  1. 在子视图上添加适当的前导、尾随、顶部和底部约束。
  2. [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{ //调整父视图大小 } completion:nil];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接