gpt4 book ai didi

ios - UIView animateWithDuration 不改变 UIView 的框架

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:57:54 25 4
gpt4 key购买 nike

我正在尝试实现一个下拉 View ,当用户按下按钮时,该 View 将向下移动,一旦用户按下同一按钮,该 View 将再次上升到上方。为此,我使用以下代码:

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

}];

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

编辑

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

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

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

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

Here, the Arrange meeting and Post concert are two buttons which are subview of my 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

最佳答案

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

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

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

enter image description here

你可能想设置左间距和右间距而不是固定宽度等...

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

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

我们可以更改此约束以动画 View 的框架。 NSLayoutConstraint 中唯一可写的部分是 constant 属性。 (讽刺)

无论如何,你的动画代码将是这样的......

self.topConstraint.constant += 42;

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

关于ios - UIView animateWithDuration 不改变 UIView 的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26848398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com