当父 UIView 的框架(大小)动画时自动调整 UILabel 的框架大小

发布于 2024-12-12 00:43:16 字数 808 浏览 0 评论 0原文

我有一个父 UIView,它有一个子 UIView(下面的代码中使用的 UILabel),其框架设置为父视图的边界,其 autoresizingMask 设置为灵活的宽度和高度:

UIView* parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
UILabel* childLabel = [[UILabel alloc] initWithFrame:parentView.bounds];
childLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
childLabel.textAlignment = UITextAlignmentCenter;
childLabel.text = @"Hello";

我希望能够对父视图的框架进行动画处理,特别是其框架大小,并将子视图调整大小作为动画的一部分:

[UIView animateWithDuration:1.0 animations:^{ parentView.frame = CGRectMake(0, 0, 160, 240); }];

由于此动画,我希望 UILabel 的文本与父视图的动画一起进行动画处理,因此在视觉上您会看到文本从以 (160, 240) 为中心移动到 (80, 120)。但是,子视图的框架似乎没有设置动画,而是立即设置为动画结束时应具有的值,因此您会看到动画开始时文本的位置立即跳转。

有没有办法让子视图作为动画的一部分自动调整大小?

I have a parent UIView that has child UIView (UILabel used in the code below) whose frame is set to the parent's bounds and whose autoresizingMask is set to flexible width and height:

UIView* parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
UILabel* childLabel = [[UILabel alloc] initWithFrame:parentView.bounds];
childLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
childLabel.textAlignment = UITextAlignmentCenter;
childLabel.text = @"Hello";

I want to be able to animate the parent view's frame, specifically its size, and have the subview resize as part of the animation:

[UIView animateWithDuration:1.0 animations:^{ parentView.frame = CGRectMake(0, 0, 160, 240); }];

As a result of this animation I would want the text of the UILabel to animate along with the parent view's animation, so visually you'd see the text move from being centered at (160, 240) to (80, 120). However, instead of animating it appears the subview's frame is being immediately set to the value it should have at the end of the animation, so you see the position of the text immediately jump when the animation starts.

Is there a way to get subviews to autoresize as part of an animation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗼ふ静 2024-12-19 00:43:16

我并不完全明白发生了什么,但我认为核心问题是 UIKit 不想重新渲染动画的每一帧文本,因此 UILabel 的内容不可设置动画。默认情况下,UILabel 的 contentMode 属性为 UIViewContentModeRedraw,这意味着一旦设置该属性,它将以目标大小重绘 UILabel。

如果将 contentMode 更改为 UIViewContentModeCenter,内容将不会重新绘制,并将保持在 UILabel 的中心位置。

childLabel.contentMode = UIViewContentModeCenter;

I don't completely have my head around what's going on, but I think the core issue is that UIKit doesn't want to have to re-render the text every frame of the animation, so the contents of a UILabel aren't animatable. By default, the contentMode property of UILabel is UIViewContentModeRedraw, meaning that it'll redraw the UILabel at the target size as soon as the property is set.

If you change the contentMode to UIViewContentModeCenter, the contents won't redraw and will remain centered in the UILabel.

childLabel.contentMode = UIViewContentModeCenter;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文