自动布局约束更改不会设置动画 [英] Auto Layout constraint change does not animate
问题描述
我正在使用教程中的动画学习自动布局
I was learning the auto layout with animations from the tutorial
http://weblog.invasivecode.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;
查看加载设置方法(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屋!