屏幕截图示例:
为包含子视图的UIView的大小调整设置动画的更好方法是什么?
我目前的方法:
在屏幕截图中,父UIView(橙色)有一个子视图UILabel(黄色+自动调整)。 animateWithDuration
正在动画调整父UIView框架的大小。此方法的问题是它没有为子视图的大小调整设置动画。它会突然改变,它应该动画缩放。
是否最明确地为子视图设置动画,还是有更有效的方法? (例如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) {
}];
}
答案 0 :(得分:3)
要为视图和子视图设置动画,您可以使用以下代码:
[UIView animateWithDuration:0.5f animations:^{
self.testView.transform = CGAffineTransformMakeScale(0.5, 0.5);
}];
如果您还使用CGAffine为动画设置动画,则不能使用边框,边界或中心。因此,如果您还需要移动视图,请使用以下内容:
CGAffineTransform transform = CGAffineTransformMakeTranslation(150,400);
transform = CGAffineTransformScale(transform, 0.5, 0.5);
答案 1 :(得分:3)
我猜你的黄色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)];
}];
希望得到帮助〜
答案 2 :(得分:0)
使用UIViewAnimationOptionLayoutSubviews:
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{ //resize the superview
} completion:nil];