gpt4 book ai didi

ios - UIView自动布局滑入屏或滑出屏动画

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:42:30 24 4
gpt4 key购买 nike

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

我添加这样的约束:

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 位置增长。如何实现滑入/滑出动画?

最佳答案

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

@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 的使用

更新:您仍然需要水平约束。请留下这些行:

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/

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