设备旋转时是否有动画自动布局(约束)的动画?

时间:2014-11-22 22:05:28

标签: ios iphone xcode

如我所见,AutoLayouts在旋转动画之前设置立即设置。对于过渡动画(例如缩放),我必须从-(void)willRotateToInterfaceOrientation调用我的自定义方法。 那么,当设备旋转时,是否有动画自动布局(约束)的动画?

UPD:我检查一下,它确实有效,但只有视图。有没有可以使用标签的东西?

// 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);
    }];

1 个答案:

答案 0 :(得分:1)

UILabel对于AutoLayout约束有点奇怪。它定义了一个intrinsicContentSize,它是根据它包含的文本计算出来的。 intrinsicContentSize充当隐式约束:它修复了标签的宽度和高度。因此,如果您还向标签添加前导和尾随约束,那么您将遇到冲突的约束,从而导致您看到的行为。

preferredMaxLayoutWidth对多行标签的此行为很复杂,确定其首选宽度是什么。

尽管如此,我仍然认为它仍然是UILabel的一个错误,它没有动画它的尺寸变化。

与此同时,您可能需要问问自己为什么要调整标签大小(例如,不要将其居中放在其父级中)。