Ios UIView自动布局滑入屏幕或滑出屏幕动画

Ios UIView自动布局滑入屏幕或滑出屏幕动画,ios,autolayout,uiviewanimation,Ios,Autolayout,Uiviewanimation,我不知道如何使用自动布局来制作滑入/滑出动画 我添加了如下约束: NSDictionary *viewsDic = @{@"tmpView" : tmpView}; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tmpView]|" options:0 metrics:nil views:viewsDic]]; [self.view addConstraints:[NSLayoutC

我不知道如何使用自动布局来制作滑入/滑出动画

我添加了如下约束:

NSDictionary *viewsDic = @{@"tmpView" : tmpView};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tmpView]|" options:0 metrics:nil views:viewsDic]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[tmpView]|" options:0 metrics:nil views:viewsDic]];

[UIView animateWithDuration:.4 animations:^{
        [self.view layoutIfNeeded];
    }];
 [self.view layoutIfNeeded];

 [UIView animateWithDuration:.4 animations:^{
    self.topPadding.constant = self.superview.bounds.size.height;

    [self.view layoutIfNeeded];
}];

但这会使动画从0,0位置开始增长。如何实现滑入/滑出动画?

据我所知,您需要获得移位效果。如果我的假设是正确的,您需要在动画期间手动管理水平或垂直填充。您需要获得水平/垂直约束上的参考:

@property (nonatomic, strong) NSLayoutConstraint *topPadding;
@property (nonatomic, strong) NSLayoutConstraint *heightConstraint;
添加具有初始值的约束,以便tmpView不在其superview的可见部分:

self.heightConstraint = [NSLayoutConstraint constraintWithItem:tmpView
                                               attribute:NSLayoutAttributeHeight
                                               relatedBy:NSLayoutRelationEqual
                                                  toItem:nil
                                               attribute:NSLayoutAttributeNotAnAttribute
                                              multiplier:1.0
                                                constant:0.0];

self.topPadding = [NSLayoutConstraint constraintWithItem:tmpView
                                           attribute:NSLayoutAttributeTop
                                           relatedBy:NSLayoutRelationEqual
                                              toItem:self.view
                                           attribute:NSLayoutAttributeTop
                                          multiplier:1.0
                                            constant:self.view.bounds.size.height];

[self.view addConstraint:self.topPadding];
[self.view addConstraint:self.heightConstraint];
然后,我们只需通过动画显示tmpView:

 [self.view layoutIfNeeded];

 [UIView animateWithDuration:.4 animations:^{
    self.topPadding.constant = 0.0;
    self.heightConstraint.constant = self.view.bounds.size.height;

    [self.view layoutIfNeeded];
}];
在该示例中,tmpView从底部显示。 请确保self.topPadding不会与其他约束冲突

要隐藏视图,可以使用以下方法:

NSDictionary *viewsDic = @{@"tmpView" : tmpView};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tmpView]|" options:0 metrics:nil views:viewsDic]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[tmpView]|" options:0 metrics:nil views:viewsDic]];

[UIView animateWithDuration:.4 animations:^{
        [self.view layoutIfNeeded];
    }];
 [self.view layoutIfNeeded];

 [UIView animateWithDuration:.4 animations:^{
    self.topPadding.constant = self.superview.bounds.size.height;

    [self.view layoutIfNeeded];
}];
UPD:有时还需要添加高约束。在我的示例中添加了self.heightConstraint的用法

UPD:您仍然需要水平约束。 请留下这几行:

NSDictionary *viewsDic = @{@"tmpView" : tmpView};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tmpView]|" options:0 metrics:nil views:viewsDic]];

仅添加此约束将无法正确绘制视图。我还尝试在动画之前添加自己的约束。但它不起作用:更新了我的答案,也许会有帮助。注意,您必须删除以前的垂直约束谢谢您的帮助。但现在它确实可以从屏幕右侧进行缩放。请尝试添加[self.view layoutifneed];和[需要tmpView布局];在动画块之前和内部,那么,您将如何消除此视图?