正如我所见,AutoLayouts 在旋转动画之前立即设置。对于过渡动画(例如缩放),我必须从 -(void)willRotateToInterfaceOrientation
调用自定义方法。
那么,当设备旋转时,是否有一些东西可以为自动布局(约束)设置动画?
UPD:我检查了一下,它确实有效,但仅限于 View 。有什么东西可以处理标签吗?
// UIView
UIView *yellowView = [[UIView alloc] init];
yellowView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:yellowView];
yellowView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:yellowView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1
constant:40]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:yellowView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:-40]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:yellowView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1
constant:40]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:yellowView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:-40]];
[yellowView layoutIfNeeded];
// UILabel
UILabel *greenLabel = [[UILabel alloc] initWithFrame:self.view.frame];
greenLabel.backgroundColor = [UIColor greenColor];
[self.view addSubview:greenLabel];
greenLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:greenLabel
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1
constant:40]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:greenLabel
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:-40]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:greenLabel
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1
constant:40]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:greenLabel
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:-40]];
[greenLabel layoutIfNeeded];
UPD2:我测试了动画,它看起来像 UILabels 问题。标签缩小而不是动画。
UILabel *pupureLabel = [[UILabel alloc] init];
pupureLabel.backgroundColor = [UIColor pupureColor];
[self.view addSubview:pupureLabel];
pupureLabel.frame = CGRectMake(0, 0, 320, 568);
[UIView animateWithDuration:1
animations:^{
pupureLabel.frame = CGRectMake(0, 0, 100, 60);
}];
最佳答案
关于自动布局约束,UILabel 有点奇怪。它定义了一个从它包含的文本计算出来的intrinsicContentSize
。 intrinsicContentSize
充当隐式约束:它固定标签的宽度和高度。因此,如果您还向标签添加前导和尾随约束,则约束会发生冲突,从而导致您看到的行为。
对于多行标签,此行为很复杂,由 preferredMaxLayoutWidth
确定其首选宽度。
尽管如此,我仍然认为它仍然是 UILabel 中的一个错误,它没有动画化其大小变化。
与此同时,您可能需要问自己为什么要调整标签大小(例如,而不是使其在其父级中居中)。
关于ios - 设备旋转时是否有动画自动布局(约束)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27083069/