通过更新约束滑入 UIView

Posted

技术标签:

【中文标题】通过更新约束滑入 UIView【英文标题】:Slide In A UIView by updating constraints 【发布时间】:2017-11-03 11:23:31 【问题描述】:

我正在尝试通过更新其约束来为 UIView 设置动画。我想最初将视图放在屏幕底部(屏幕外),然后将顶部约束设置为 0,以便动画显示视图向上移动。

我已经尝试了以下没有运气,动画不正确。

CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
//CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;

self.viewSearchWrapper.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addSubview:self.viewSearchWrapper];

NSLayoutConstraint *trailing = [NSLayoutConstraint constraintWithItem:self.viewSearchWrapper
                                   attribute:NSLayoutAttributeTrailing
                                   relatedBy:NSLayoutRelationEqual
                                   toItem:self.view
                                   attribute:NSLayoutAttributeTrailing
                                   multiplier:1.0f
                                   constant:0.f];

// Leading

NSLayoutConstraint *leading = [NSLayoutConstraint
                                   constraintWithItem:self.viewSearchWrapper
                                   attribute:NSLayoutAttributeLeading
                                   relatedBy:NSLayoutRelationEqual
                                   toItem:self.view
                                   attribute:NSLayoutAttributeLeading
                                   multiplier:1.0f
                                   constant:0.f];

// Bottom
NSLayoutConstraint *bottom = [NSLayoutConstraint
                                  constraintWithItem:self.viewSearchWrapper
                                  attribute:NSLayoutAttributeBottom
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:self.view
                                  attribute:NSLayoutAttributeBottom
                                  multiplier:1.0f
                                  constant:0.f];

// Bottom
NSLayoutConstraint *top = [NSLayoutConstraint
                              constraintWithItem:self.viewSearchWrapper
                              attribute:NSLayoutAttributeTop
                              relatedBy:NSLayoutRelationEqual
                              toItem:self.view
                              attribute:NSLayoutAttributeTop
                              multiplier:1.0f
                              constant:screenHeight];

[self.view addConstraint:trailing];
[self.view addConstraint:bottom];
[self.view addConstraint:leading];
[self.view addConstraint:top];                        

[self.viewSearchWrapper setNeedsUpdateConstraints];

[top setConstant:0.f];

[UIView animateWithDuration:1 animations:^
    [self.view layoutIfNeeded];
];

【问题讨论】:

您只是在减少顶部约束。尝试同时增加底部约束。或者您可以设置greaterThanOrEqual 关系而不是equal 作为底部约束。 【参考方案1】:

添加layoutIfNeeded 以首先将初始约束设置为viewSearchWrapper

// .... keep the code assigning the constraints as it is.

[self.view layoutIfNeeded];  // This will set the initial frame of viewSearchWrapper
[self.viewSearchWrapper setNeedsUpdateConstraints];
[top setConstant:0.f];
[UIView animateWithDuration:1 animations:^
   [self.view layoutIfNeeded];
];

【讨论】:

【参考方案2】:

animateWithDuration里面添加[top setConstant:0.f]如下图:

[UIView animateWithDuration:1 animations:^
        [top setConstant:0.f];
        [self.view layoutIfNeeded];
    ];

【讨论】:

以上是关于通过更新约束滑入 UIView的主要内容,如果未能解决你的问题,请参考以下文章

滑入iOS菜单

通过其约束为 UIView(包含带约束的子组件)设置动画

UIView 动画滑入

通过动画调整 UIView 大小时约束不起作用

使用自动布局约束时,UIView 无法通过 UIpangesture 顺利拖动

使用NSView.layoutSubtreeIfNeeded()动画自动布局约束不适用于macOS High Sierra