简体   繁体   English

UIView animateWithDuration不改变UIView的框架

[英]UIView animateWithDuration not changing the frame of UIView

I am trying to implement a drop down view which will move down when the user press the button and will be risen above again once the user press the same button. 我正在尝试实现一个下拉视图,当用户按下按钮时它将向下移动,一旦用户按下相同的按钮,它将再次上升。 I am using following code for this purpose: 我为此目的使用以下代码:

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

Here what I am trying is to move the origin 42 points down to create dropdown sort of animation. 我在这里尝试的是将原点42点向下移动以创建下拉类型的动画。 In case of moving up, I am using: 如果向上移动,我正在使用:

[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);

    }];

However, if I check the log, the frame of the dropdown view is not updated. 但是,如果我检查日志,则不会更新下拉视图的框架。 However, If I use the same code in another VC with different UI elements, this code is working perfectly fine. 但是,如果我在具有不同UI元素的另一个VC中使用相同的代码,则此代码工作正常。 In both cases, I have created the dropdown view in storyboard. 在这两种情况下,我都在故事板中创建了下拉视图。 What is wrong with the code so that it isn't working? 代码有什么问题,以致它无法正常工作?

Edit 编辑

When I logged the constraints before first animation, the log was: 当我在第一个动画之前记录约束时,日志是:

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)]>")

and the log of constraints once the animation was completed was: 动画完成后的约束日志是:

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)]>")

Edit 2 编辑2

That is what I want to achieve: This is the view before animation. 这就是我想要实现的目标:这是动画前的视图。 Once I click the arrow button, the view should move down as shown 单击箭头按钮后,视图应向下移动,如图所示 这是动画前的视图。单击箭头按钮后,视图应向下移动,如图所示

Here, the Arrange meeting and Post concert are two buttons which are subview of my dropdownView. 在这里,安排会议和发布会音乐会是两个按钮,它们是我的dropdownView的子视图。 They are there, even before the animation but they are behind the brown view, so they were not being shown, once I pressed the arrow button, they moved down and become visible 他们在那里,甚至在动画之前,但他们在棕色视图后面,所以他们没有被显示,一旦我按下箭头按钮,他们向下移动并变得可见

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

When you are using AutoLayout you cannot update the frame or center of an app directly. 使用AutoLayout时,无法直接更新应用程序的framecenter

It just won't work. 它不会起作用。 The constraints defining the frame will override any change you make and it will stay exactly where it is. 定义框架的约束将覆盖您所做的任何更改,并且它将保持精确的位置。

For your animation you need to set up constraints something like this... 对于你的动画,你需要设置这样的约束......

在此输入图像描述

You might want to set left space and right space instead of fixed width etc... 您可能想要设置左侧空间和右侧空间而不是固定宽度等...

The top constraint needs to then be stored in a property... 然后需要将顶部约束存储在属性中...

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

It is this constraint that we can then change to animate the frame of the view. 正是这种约束我们可以改变为动画视图的框架。 The only part of NSLayoutConstraint that is writable is the constant property. 可写的NSLayoutConstraint的唯一部分是constant属性。 (Ironic) (讽刺)

Anyway, your animation code will be something like this... 无论如何,你的动画代码将是这样的......

self.topConstraint.constant += 42;

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

Finally I got it right by using the help from @Fogmeister along with utilising my original code. 最后,通过使用@Fogmeister的帮助以及使用我的原始代码,我做对了。 Using the code 使用代码

self.topConstraint.constant += 42;

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

the frame of the view was being updated but it wasn't with the animation (a dropdown like stuff), so I utilised this with my original code (which was causing the animation, but at the end of animation, the view went back to its initial position) to get the desired results. 视图的框架正在更新,但它不是动画(下拉类似的东西),所以我用我的原始代码(导致动画,但在动画结束时,视图回到它的初始位置)以获得理想的结果。 My final code is: 我的最终代码是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM