【发布时间】:2014-05-13 00:57:23
【问题描述】:
屏幕截图示例:
有什么更好的动画方式来调整包含子视图的 UIView 的大小?
我目前的方法:
在屏幕截图中,父 UIView(橙色)有一个子视图 UILabel(黄色 + autoresizing)。 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) {
}];
}
【问题讨论】:
-
不,子视图应该动画。请显示有关您的动画代码的更多信息。还请描述子视图的自动调整大小或自动布局约束。
-
马特,我将代码添加到我的问题中。当橙色父视图的框架发生变化时,它会动画,但子视图不会。
-
找到一个解释:子UILabel默认没有动画,因为它的默认内容模式是UIViewContentModeRedraw:stackoverflow.com/questions/1729596/…
-
已经有几年了,但是在经历了数小时的挫折之后,我的动画问题被您在此处重新内容模式的解释解决了。就我而言,它不是 UILabel,而是 ARSCNView。它的默认内容模式是.center;切换到 .scaleToFill 解决了我的问题。所以,谢谢!!
标签: ios iphone objective-c ios7 uiview