自动布局约束更改没有动画 [英] Auto Layout constraint change does not animate
问题描述
我从教程中学习了带有动画的自动布局
I was learning the auto layout with animations from the tutorial
http://weblog.入侵code.com/post/42362079291/auto-layout-and-core-animation-auto-layout-was
一切都很完美.
当我尝试在我的应用程序中使用这个概念时,尝试从下到上为设置屏幕(一个 UIView)设置动画,当设置屏幕只是一个空的 UIView 时效果很好,
When I tried to use this concept in my application, trying to animate a settings screen(a UIView) from bottom to top,it works great when the settings screen is just an empty UIView,
但如果我将 UILabel 作为子视图添加到此设置屏幕,动画就会消失.从设置屏幕中删除此 UILabel 后,动画可见.
But in case I add a UILabel as a subview to this settings screen, the animation just vanishes. On removing this UILabel form the settings screen, the animation is visible.
这是我连接的插座
__weak IBOutlet UIView *settingsView;
__weak IBOutlet NSLayoutConstraint *settingsBottomConstraint;
__weak IBOutlet NSLayoutConstraint *settingsViewHeightConstraint;
View 确实加载了设置方法(setupViews)
View did load setup method(setupViews)
-(void)setupViews
{
settingsBottomConstraint.constant = - settingsViewHeightConstraint.constant;
[settingsView setNeedsUpdateConstraints];
[settingsView layoutIfNeeded];
isSettingsHidden = YES;
}
隐藏/取消隐藏方法
- (IBAction)showSettingsScreen:(id)sender {
if (isSettingsHidden) {
settingsBottomConstraint.constant = 0;
[settingsView setNeedsUpdateConstraints];
[UIView animateWithDuration:.3 animations:^{
[settingsView layoutIfNeeded];
}];
}
else{
settingsBottomConstraint.constant = - settingsViewHeightConstraint.constant;
[settingsView setNeedsUpdateConstraints];
[UIView animateWithDuration:0.3 animations:^{
[settingsView layoutIfNeeded];
}];
}
isSettingsHidden = !isSettingsHidden;
}
我的问题似乎与UIView 自动布局动画问题
推荐答案
我找到了答案.
取而代之,
[settingsView layoutIfNeeded];
这条线使它像魅力一样工作,
this line made it worked like charm,
[self.view layoutIfNeeded];
我想我们需要在父视图上执行 layoutIfNeeded 方法,而不仅仅是我们想要制作动画的视图.
I suppose we need to perform layoutIfNeeded method on the parent view not just the view we are trying to animate.
更新:正如 codyko 在评论中指出的那样,这是 iOS 7、iOS 10 所必需的.对于 iOS 8,此问题不存在.
UPDATE: As pointed out in a comment by codyko, this is required for iOS 7, iOS 10. For iOS 8 this issue does not exists.
这篇关于自动布局约束更改没有动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!