【问题标题】:A better way to animate resizing of UIView and subviews?动画调整 UIView 和子视图大小的更好方法?
【发布时间】:2014-05-13 00:57:23
【问题描述】:

屏幕截图示例:

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

我目前的方法:

在屏幕截图中,父 UIView(橙色)有一个子视图 UILabel(黄色 + autoresizing)。 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) {
                     }];
}

【问题讨论】:

  • 不,子视图应该动画。请显示有关您的动画代码的更多信息。还请描述子视图的自动调整大小或自动布局约束。
  • 马特,我将代码添加到我的问题中。当橙色父视图的框架发生变化时,它会动画,但子视图不会。
  • 找到一个解释:子UILabel默认没有动画,因为它的默认内容模式是UIViewContentModeRedraw:stackoverflow.com/questions/1729596/…
  • 已经有几年了,但是在经历了数小时的挫折之后,我的动画问题被您在此处重新内容模式的解释解决了。就我而言,它不是 UILabel,而是 ARSCNView。它的默认内容模式是.center;切换到 .scaleToFill 解决了我的问题。所以,谢谢!!

标签: ios iphone objective-c ios7 uiview


【解决方案1】:

要为视图和子视图设置动画,您可以使用以下代码:

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

更多信息

如果您还使用 CGAffine 对事物进行动画处理,则不能使用框架、边界或中心,因此如果您还需要移动视图,请使用以下内容:

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

【讨论】:

    【解决方案2】:

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

    求帮助~

    【讨论】:

      【解决方案3】:

      使用 UIViewAnimationOptionLayoutSubviews:

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

      【讨论】:

        猜你喜欢
        • 2023-04-02
        • 1970-01-01
        • 2013-01-22
        • 2014-09-02
        • 2013-04-12
        • 2017-06-08
        • 2012-08-28
        • 1970-01-01
        • 2014-04-04
        相关资源
        最近更新 更多