设备旋转时是否可以设置自动布局动画(约束)?
如我所见,AutoLayouts会立即设置之前旋转动画.对于过渡动画(例如比例),我必须从-(void)willRotateToInterfaceOrientation
调用我的自定义方法.因此,当设备旋转时,是否有一些动画可以对自动布局(约束)进行动画处理?
As I see, AutoLayouts sets before rotate animation, instantly. And for animation of transition (scale, for example) I must call my custom method from -(void)willRotateToInterfaceOrientation
.
So, is there something to animate autolayout (constraints) when device rotates?
UPD::我检查了一下,它确实可以工作,但仅适用于视图.标签有什么用吗?
UPD: I check it up, and it realy works, but only with views. Is there something to work with labels?
// 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问题.标签缩小不会动画.
UPD2: I test animation and it's look like UILabels problem. Labels downsizing not animates.
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关于AutoLayout约束有点奇怪.它定义了一个 intrinsicContentSize
,该代码是根据其包含的文本计算得出的. intrinsicContentSize
充当隐式约束:它固定标签的宽度和高度.因此,如果您还向标签添加前导约束和尾随约束,那么约束将发生冲突,从而导致您看到的行为.
UILabel is a bit of a strange beast regarding AutoLayout constraints. It defines an intrinsicContentSize
that is computed from the text that it contains. The intrinsicContentSize
acts as an implicit constraint: it fixes the width and height of the label. Therefore, if you also add leading and trailing constraints to the label, then you have conflicting constraints, resulting in the behaviour that you see.
对于多行标签,此行为很复杂,因为 preferredMaxLayoutWidth
确定其首选宽度是什么.
This behaviour is complicated for multi-line labels by the preferredMaxLayoutWidth
, determining what its preferred width is.
说了这么多,我仍然认为UILabel仍然没有改变其大小变化的动画仍然是一个错误.
With all that said, I still think it is still a bug in UILabel that it does not animate its size change.
同时,您可能需要问自己为什么要调整标签的大小(例如,而不是将其居中放置在其父标签中).
In the meantime, you probably need to ask yourself why you want the label to resize (instead of centering it in its parent, for example).