通过更新约束来滑入UIView

时间:2017-11-03 11:23:32

标签: ios objective-c autolayout

我正在尝试通过更新它的约束来为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];
        }];

2 个答案:

答案 0 :(得分:3)

添加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];
}];

答案 1 :(得分:0)

添加" [top setConstant:0.f];"在animateWithDuration内,如下所示

 for (socketID in io.nsps['/'].adapter.rooms[room_name].sockets) {
   const nickname = io.nsps['/'].connected[socketID].nickname;
   // do stuff with nickname
 }