gpt4 book ai didi

ios - 动画调整 UIView 和 subview 大小的更好方法?

转载 作者:技术小花猫 更新时间:2023-10-29 11:23:05 24 4
gpt4 key购买 nike

示例屏幕截图:

http://i.stack.imgur.com/CyOQi.gif

什么是动画调整包含 subview 的 UIView 大小的更好方法?

我目前的方法:

在屏幕截图中,父 UIView(橙色)有一个 subview UILabel(黄色 + 自动调整大小)。 animateWithDuration 正在动画调整父 UIView 框架的大小。这种方法的问题是它不会动画化 subview 的大小调整。它突然改变,它应该动画地缩放。

显式动画 subview 最好,还是有更有效的方法? (例如 CGAffineTransform?)

示例代码:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

self.parentView = [[UIView alloc] initWithFrame:CGRectMake(10, 100, 200, 200)];
self.parentView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:self.parentView];

self.childLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 180, 180)];
self.childLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
self.childLabel.backgroundColor = [UIColor yellowColor];
[self.parentView addSubview:self.childLabel];

[NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:@selector(randomizeParentViewSize)
userInfo:nil
repeats:YES];
}

- (void)randomizeParentViewSize {
CGFloat width = arc4random_uniform(100) + 50;
CGFloat height = arc4random_uniform(100) + 50;

[UIView animateWithDuration:1.5f
delay:0.0f
options:0
animations:^{
self.parentView.frame = CGRectMake(10, 50 + height, width, height);
}
completion:^(BOOL finished) {
}];
}

最佳答案

我猜你的黄色 subview 是 UILabel ?

当 UILabel 在 UIViewAnimation 中改变帧尺寸时会立即缩放

除非你使用 CGAffineTransformScale


一些例子

什么时候

[self.view addSubview:view_parent];
[view_parent addSubview:view_component];

如果 view_parent 和 view_component 都是 UIView

[UIView animateWithDuration:2.0 animations:^{
[view_parent setFrame:CGRectMake(0, 20, 160, 160)];
[view_component setFrame:CGRectMake(20, 20, 80, 80)];
}];

工作正常

但是当 view_parent 是 UIView 而 view_component 是 UILabel 时

你需要做一些像...

[UIView animateWithDuration:2.0 animations:^{
[view_parent setFrame:CGRectMake(0, 20, 160, 160)];
view_component.transform = CGAffineTransformScale(view_component.transform, 0.5, 0.5);
[view_component setFrame:CGRectMake(20, 20, 80, 80)];
}];

希望得到帮助~

关于ios - 动画调整 UIView 和 subview 大小的更好方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22799654/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com