【问题标题】:UIView animateWithDuration not changing the frame of UIViewUIView animateWithDuration 不改变 UIView 的框架
【发布时间】:2015-01-07 00:40:38
【问题描述】:

我正在尝试实现一个下拉视图,当用户按下按钮时它会向下移动,一旦用户按下相同的按钮,它就会再次上升。为此,我使用以下代码:

[UIView animateWithDuration:0.25f animations:^{
        self.dropdownView.frame =
        CGRectMake(self.dropdownView.frame.origin.x,
                   self.dropdownView.frame.origin.y+42,
                   self.dropdownView.frame.size.width,
                   self.dropdownView.frame.size.height);
    }
                     completion:^(BOOL finished){
        NSLog(@"%@",self.dropdownView);
    }];

我在这里尝试将原点向下移动 42 点以创建下拉式动画。如果向上移动,我正在使用:

[UIView animateWithDuration:0.25f animations:^{
        self.dropdownView.frame =
        CGRectMake(self.dropdownView.frame.origin.x,
                   self.dropdownView.frame.origin.y-42,
                   self.dropdownView.frame.size.width,
                   self.dropdownView.frame.size.height);
    }
                     completion:^(BOOL finished){
    NSLog(@"%@",self.dropdownView);

    }];

但是,如果我检查日志,下拉视图的框架不会更新。但是,如果我在另一个具有不同 UI 元素的 VC 中使用相同的代码,则该代码可以正常工作。在这两种情况下,我都在情节提要中创建了下拉视图。代码有什么问题导致它无法正常工作?

编辑

当我在第一个动画之前记录约束时,日志是:

Constraints: (
"<NSIBPrototypingLayoutConstraint:0x7fcb01797460 'IB auto generated at build time for view with fixed frame' H:|-(0)-[UIImageView:0x7fcb01481970](LTR)   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017c0ac0 'IB auto generated at build time for view with fixed frame' V:|-(0)-[UIImageView:0x7fcb01481970]   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017c0b10 'IB auto generated at build time for view with fixed frame' H:[UIImageView:0x7fcb01481970(320)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017aa360 'IB auto generated at build time for view with fixed frame' V:[UIImageView:0x7fcb01481970(42)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017aa3b0 'IB auto generated at build time for view with fixed frame' H:|-(0)-[UIButton:0x7fcb017e8080](LTR)   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017824e0 'IB auto generated at build time for view with fixed frame' V:|-(3)-[UIButton:0x7fcb017e8080]   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb01782530 'IB auto generated at build time for view with fixed frame' H:[UIButton:0x7fcb017e8080(79)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb0179fe70 'IB auto generated at build time for view with fixed frame' V:[UIButton:0x7fcb017e8080(36)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb0179fec0 'IB auto generated at build time for view with fixed frame' H:|-(82)-[UIButton:0x7fcb01796ea0](LTR)   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb01776490 'IB auto generated at build time for view with fixed frame' V:|-(3)-[UIButton:0x7fcb01796ea0]   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017764e0 'IB auto generated at build time for view with fixed frame' H:[UIButton:0x7fcb01796ea0(79)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017956b0 'IB auto generated at build time for view with fixed frame' V:[UIButton:0x7fcb01796ea0(36)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb01795700 'IB auto generated at build time for view with fixed frame' H:|-(164)-[UIButton:0x7fcb01699f60](LTR)   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb01795860 'IB auto generated at build time for view with fixed frame' V:|-(3)-[UIButton:0x7fcb01699f60]   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017958b0 'IB auto generated at build time for view with fixed frame' H:[UIButton:0x7fcb01699f60(79)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017aef40 'IB auto generated at build time for view with fixed frame' V:[UIButton:0x7fcb01699f60(36)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017aef90 'IB auto generated at build time for view with fixed frame' H:|-(242)-[UIButton:0x7fcb01667610](LTR)   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017b44b0 'IB auto generated at build time for view with fixed frame' V:|-(3)-[UIButton:0x7fcb01667610]   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017b4500 'IB auto generated at build time for view with fixed frame' H:[UIButton:0x7fcb01667610(79)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017bbde0 'IB auto generated at build time for view with fixed frame' V:[UIButton:0x7fcb01667610(36)]>")

动画完成后的约束日志是:

Constraints: (
"<NSIBPrototypingLayoutConstraint:0x7fcb01797460 'IB auto generated at build time for view with fixed frame' H:|-(0)-[UIImageView:0x7fcb01481970](LTR)   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017c0ac0 'IB auto generated at build time for view with fixed frame' V:|-(0)-[UIImageView:0x7fcb01481970]   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017c0b10 'IB auto generated at build time for view with fixed frame' H:[UIImageView:0x7fcb01481970(320)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017aa360 'IB auto generated at build time for view with fixed frame' V:[UIImageView:0x7fcb01481970(42)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017aa3b0 'IB auto generated at build time for view with fixed frame' H:|-(0)-[UIButton:0x7fcb017e8080](LTR)   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017824e0 'IB auto generated at build time for view with fixed frame' V:|-(3)-[UIButton:0x7fcb017e8080]   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb01782530 'IB auto generated at build time for view with fixed frame' H:[UIButton:0x7fcb017e8080(79)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb0179fe70 'IB auto generated at build time for view with fixed frame' V:[UIButton:0x7fcb017e8080(36)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb0179fec0 'IB auto generated at build time for view with fixed frame' H:|-(82)-[UIButton:0x7fcb01796ea0](LTR)   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb01776490 'IB auto generated at build time for view with fixed frame' V:|-(3)-[UIButton:0x7fcb01796ea0]   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017764e0 'IB auto generated at build time for view with fixed frame' H:[UIButton:0x7fcb01796ea0(79)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017956b0 'IB auto generated at build time for view with fixed frame' V:[UIButton:0x7fcb01796ea0(36)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb01795700 'IB auto generated at build time for view with fixed frame' H:|-(164)-[UIButton:0x7fcb01699f60](LTR)   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb01795860 'IB auto generated at build time for view with fixed frame' V:|-(3)-[UIButton:0x7fcb01699f60]   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017958b0 'IB auto generated at build time for view with fixed frame' H:[UIButton:0x7fcb01699f60(79)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017aef40 'IB auto generated at build time for view with fixed frame' V:[UIButton:0x7fcb01699f60(36)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017aef90 'IB auto generated at build time for view with fixed frame' H:|-(242)-[UIButton:0x7fcb01667610](LTR)   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017b44b0 'IB auto generated at build time for view with fixed frame' V:|-(3)-[UIButton:0x7fcb01667610]   (Names: '|':UIView:0x7fcb014a16b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017b4500 'IB auto generated at build time for view with fixed frame' H:[UIButton:0x7fcb01667610(79)]>",
"<NSIBPrototypingLayoutConstraint:0x7fcb017bbde0 'IB auto generated at build time for view with fixed frame' V:[UIButton:0x7fcb01667610(36)]>")

编辑 2

这就是我想要实现的目标: 这是动画之前的视图。单击箭头按钮后,视图应向下移动,如图所示

在这里,安排会议和发布音乐会是两个按钮,它们是我的 dropdownView 的子视图。它们在那里,甚至在动画之前,但它们在棕色视图后面,所以它们没有被显示,一旦我按下箭头按钮,它们就会向下移动并变得可见

【问题讨论】:

  • 日志信息是怎么说的?
  • 它显示与动画开始之前相同的视图帧值
  • 你在使用 AutoLayout 吗?
  • 你怎么知道的?您没有在动画开始之前记录;请在动画前后记录,并请显示日志消息的内容,不要只是谈论它们
  • @Fogmeister 是的,我正在使用自动布局。我在两个 VC 中都使用它,但我没有对这个 dropdownView 使用任何约束。

标签: ios objective-c uiview uiviewanimation


【解决方案1】:

当您使用 AutoLayout 时,您无法直接更新应用的 framecenter

它只是行不通。定义框架的约束将覆盖您所做的任何更改,并且它将保持在原来的位置。

对于您的动画,您需要设置类似这样的约束...

您可能想要设置左侧空间和右侧空间而不是固定宽度等...

然后需要将顶部约束存储在属性中...

@property (nonatomic, weak) IBOutlet NSLayoutConstraint *topConstraint;

正是这个约束,我们可以改变以动画视图的框架。 NSLayoutConstraint 中唯一可写的部分是 constant 属性。 (讽刺)

不管怎样,你的动画代码会是这样的......

self.topConstraint.constant += 42;

[UIView animateWithDuration:0.25f
                 animations:^{
                     [self.view setNeedsLayout];
                 }
                 completion:^(BOOL finished) {
                     NSLog(@"%@",self.dropdownView);
                 }];

【讨论】:

【解决方案2】:

最后,通过使用@Fogmeister 的帮助以及使用我的原始代码,我做到了。使用代码

self.topConstraint.constant += 42;

[UIView animateWithDuration:0.25f
             animations:^{
                 [self.view setNeedsLayout];
             }
             completion:^(BOOL finished) {
                 NSLog(@"%@",self.dropdownView);
             }];

视图的框架正在更新,但它没有与动画(类似下拉菜单)一起使用,所以我将它与我的原始代码一起使用(它导致了动画,但在动画结束时,视图回到其初始位置)以获得所需的结果。我的最终代码是:

[UIView animateWithDuration:0.25f
                     animations:^{
                         self.topMarginConstraint.constant += 42;
                         [self.dropdownView setNeedsLayout];

                         self.dropdownView.frame =
                         CGRectMake(self.dropdownView.frame.origin.x,
                                    self.dropdownView.frame.origin.y+42,
                                    self.dropdownView.frame.size.width,
                                    self.dropdownView.frame.size.height);
                     }
                     completion:^(BOOL finished) {
                         NSLog(@"%@",self.dropdownView);
                     }];

【讨论】:

  • 这里不需要设置框架。这完全是多余的。您可以使用我在答案中输入的代码,它仍然可以工作。
猜你喜欢
  • 1970-01-01
  • 2014-02-13
  • 1970-01-01
  • 2014-04-26
  • 2012-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-08
相关资源
最近更新 更多