【问题标题】:UIView animation trying to change the frame size doesn't work in iOS 8 but in iOS 7尝试更改帧大小的 UIView 动画在 iOS 8 中不起作用,但在 iOS 7 中不起作用
【发布时间】:2014-09-26 08:34:07
【问题描述】:

我想调整 UIView 的大小,我的代码在 iOS 7.1 中运行良好,但是当我在 iOS 8 中运行时它不能正常运行(我将在下面解释)。

我的 UIView 带有值 (0,67,511,320),我想在 iPad 上调整为全屏,所以我添加了以下代码:

        [UIView animateWithDuration:0.5 animations:^{

            CGRect frame = containerView.frame;
            frame.origin.x = 0;
            frame.origin.y = 67;
            frame.size.height = 643;
            frame.size.width = 1024;
            containerView.frame = frame;
            ...

        }completion:^(BOOL finished) {
            ...
        }];

我想要类似的东西:

 _________________________________
|           |                     |
|           |                     |
|     A     |                     |
|___________|                     |
|                                 |
|                BACKGROUND       |
|                                 |
|                                 |
|_________________________________|

                |
                V
 _________________________________
|                                 |
|                                 |
|                                 |
|               A                 |
|                                 |
|                                 |
|                                 |
|                                 |
|_________________________________|

但它会像 (0,67,0,0) 到 (0,67,511,320) 一样启动动画

关于发生了什么的任何线索?还是替代方案?

【问题讨论】:

  • 请注意,当您覆盖所有成员时,您不需要从containerView.frame 获取框架。
  • 是的,我知道,谢谢您的建议。

标签: ios objective-c uiview ios8 uianimation


【解决方案1】:

您应该禁用预生成的约束。最简单的方法是使用自动调整掩码而不是约束来动画视图。

containerView.translatesAutoresizingMaskIntoConstraints = YES;

您可以将此代码放入您的 -viewDidLoad 方法或在 [UIView animateWithDuration:... 上方

【讨论】:

  • 如果我在 scrollview 中有 scrollview 和 uiview 并且在这个 uiview 中发生动画,那么 contrainerView 应该是什么。会是那个uiview吗?
【解决方案2】:

是否启用了自动布局? 也许在 iOS 8 中,在构建期间会添加一些额外的约束

尝试在动画块中调用[self.view layoutIfNeeded];,在动画代码本身之后

更新

__block CGRect frame = containerView.frame;
frame.origin.x = 0;
frame.origin.y = 67;
frame.size.height = 643;
frame.size.width = 1024;

[UIView animateWithDuration:0.5 animations:^{

        containerView.frame = frame;
        ...

    }completion:^(BOOL finished) {
        ...
    }];

【讨论】:

  • 我在情节提要中添加了我的视图,我该如何禁用它?
  • [self.view layoutIfNeeded] 有帮助吗?还要看看如何使用约束为视图设置动画stackoverflow.com/questions/17626802/…
  • 它不起作用,但我终于用@LorikMalork回答解决了谢谢你的帮助!
猜你喜欢
  • 2014-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-29
  • 1970-01-01
  • 1970-01-01
  • 2015-05-20
相关资源
最近更新 更多