动画调整UIView和子视图大小的更好方法?
屏幕截图示例:
动画调整包含子视图的UIView大小的更好方法是什么?
What is a better way to animate the resizing of a UIView that includes subviews?
我当前的方法:
在屏幕截图中,父UIView(橙色)有一个子视图UILabel(黄色+自动调整大小)。 animateWithDuration
正在调整父UIView框架的大小。此方法的问题是它没有为子视图的大小调整设置动画。它会突然改变,它应该动画缩放。
In the screen capture, the parent UIView (orange) has a subview UILabel (yellow + autoresizing). animateWithDuration
is animating the resizing of the parent UIView's frame. The problem with this method is it does not animate the resizing of the subview. It abruptly changes, where it should scale animatedly.
是否明确地为子视图设置动画,或者是否有更有效的方法? (例如CGAffineTransform?)
Is explicitly animating the subview best, or is there a more efficient method? (e.g. 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) {
}];
}
我猜你的黄色子视图是UILabel?
I Guess Your Yellow SubView is UILabel ?
当UILabel在UIViewAnimation中更改帧大小时会立即缩放
When UILabel change frame size in UIViewAnimation will scale Immediately
除非你使用CGAffineTransformScale
unless you use the CGAffineTransformScale
一些例子
当
[self.view addSubview:view_parent];
[view_parent addSubview:view_component];
如果view_parent和view_component都是UIView
if both view_parent and view_component is 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
but when view_parent is UIView and view_component is UILabel
你需要做类似的事情......
you need do something like ...
[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)];
}];
希望得到帮助〜