UIView animateWithDuration不改变UIView的框架

4

我正在尝试实现一个下拉菜单视图,当用户按下按钮时,它会向下移动,并在用户再次按下相同的按钮时再次上升。 我正在使用以下代码来实现这个目的:

[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中使用相同的代码,则此代码完美地工作。在这两种情况下,我都是在storyboard中创建下拉视图。为什么代码不起作用呢? 编辑 当我在第一次动画之前记录了约束时,日志如下:
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

这是我想要实现的目标: 这是动画前的视图。一旦我点击箭头按钮,视图应像所示向下移动。 这是动画前的视图。一旦我点击箭头按钮,视图应像所示向下移动。

这里,安排会议和发布音乐会是我的下拉视图的两个按钮。它们在动画之前就已经存在了,但是它们在褐色视图后面,因此它们没有显示出来。一旦我按下箭头按钮,它们就会向下移动并变为可见。

这里,安排会议和发布音乐会是我的下拉视图的两个按钮。它们在动画之前就已经存在了,但是它们在褐色视图后面,因此它们没有显示出来。一旦我按下箭头按钮,它们就会向下移动并变为可见。


日志信息显示了什么? - matt
你正在使用AutoLayout吗? - Fogmeister
你怎么知道的?动画开始之前你没有记录日志;请在动画开始前和结束后记录日志,并且请“展示”日志信息,不要只是“谈论”它们。 - matt
@Fogmeister 是的,我正在使用自动布局。我在两个视图控制器中都使用了它,但是我没有在这个下拉视图上使用任何约束。 - Muhammad Jahanzaib
添加一个答案,但你需要根据自己的定制情况进行更改... - Fogmeister
显示剩余12条评论
2个回答

8
当您使用AutoLayout时,不能直接更新应用程序的frame或center。这是行不通的。定义frame的约束将覆盖您所做的任何更改,并且它将保持在原地。对于您的动画,您需要设置类似于以下内容的约束... 您可能希望设置左侧空间和右侧空间,而不是固定宽度等。然后需要将顶部约束存储在属性中...
@property (nonatomic, weak) IBOutlet NSLayoutConstraint *topConstraint;

我们可以改变约束条件来实现视图框架的动画效果,而唯一可写的约束条件部分是constant属性。(有点讽刺)

无论如何,您的动画代码将类似于以下内容...

self.topConstraint.constant += 42;

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

帮我修复http://stackoverflow.com/questions/40149922/setting-and-accessing-value-in-many-to-many-relationship-in-code-data。 - Avijit Nagare
嘿,我使用了上面的解决方案,对我来说很好用。我请求你帮忙链接问题,我卡在那里了。 - Avijit Nagare

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

你不需要在这里设置框架,这完全是多余的。你只需要使用我在答案中提供的代码,它仍然可以正常工作。 - Fogmeister

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