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

标签 ios iphone xcode

正如我所见,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 有点奇怪。它定义了一个从它包含的文本计算出来的intrinsicContentSizeintrinsicContentSize 充当隐式约束:它固定标签的宽度和高度。因此,如果您还向标签添加前导和尾随约束,则约束会发生冲突,从而导致您看到的行为。

对于多行标签,此行为很复杂,由 preferredMaxLayoutWidth 确定其首选宽度。

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

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

关于ios - 设备旋转时是否有动画自动布局(约束)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27083069/

相关文章:

ios - OpenCV MatToUIImage 导致内存泄漏

iphone - 通过仪器跟踪 "message sent to deallocated instance"

ios - 如何在核心数据发生变化时预览数据库?

ios - 在 2 个 View Controller 之间传递一个字符串

ios - 您必须提供 5.5 英寸 Retina 显示屏的屏幕截图(适用于 4.7 英寸)

objective-c - UIWindow 创建 NSAutoresizingMaskLayoutConstraints 而不是 NSLayoutConstraint

ios - Cocoapods循环依赖问题

ios - 带有表情符号的自定义键盘(如 kimoji)?

ios - 如何为在 iPad 上运行的 iPhone SwiftUI 应用程序锁定纵向方向?

ios - 如何在 iOS swift4 中更改特定的 UITableviewcell 颜色