OGeek|极客世界-中国程序员成长平台

标题: ios - UIView 自动布局在屏幕内或屏幕外滑动动画 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 08:56
标题: ios - UIView 自动布局在屏幕内或屏幕外滑动动画

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

我添加这样的约束:

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

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



Best Answer-推荐答案


据我了解,您需要获得移位效果。如果我的假设是正确的,您需要在动画期间手动管理水平或垂直填充。你需要得到水平/垂直约束的引用:

@property (nonatomic, strong) NSLayoutConstraint *topPadding;
@property (nonatomic, strong) NSLayoutConstraint *heightConstraint;

添加具有初始值的约束,以便 tmpView 不在其父 View 的可见部分:

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 不会与您的其他约束冲突

要隐藏 View ,您可以使用以下内容:

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

关于ios - UIView 自动布局在屏幕内或屏幕外滑动动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25423278/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4