通过自动调整子视图动画UIView帧大小更改

jjo*_*son 7 iphone animation subview ios

我有一个包含两个子视图的UIView.我想用动画改变superview的帧大小,并让子视图的大小根据其自动大小的掩码而改变.

[UIView beginAnimations:@"Resize" context:nil];
[UIView setAnimationDuration:1.0];
CGRect frame = self.myView.frame;
frame.size.height += 30.0;
self.myView.frame = frame;
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

视图self.myView按预期调整大小超过1秒,但子视图会立即调整大小.有谁知道为什么会发生这种情况以及我如何让子视图动画调整大小?

从我的谷歌搜索,我认为它可能与contentMode财产有关.不幸的是,我不清楚该财产的作用.任何帮助将非常感激.

XJo*_*nes 14

虽然我会告诉你你在做什么,但是很难让别人给你正确的答案[UIView commitAnimations].不过,很高兴你明白了.我建议你使用块动画.通过使用完成块,它们更容易避免这种类型的错误.例:

[UIView animateWithDuration:1.0
                 animations:^{
                     CGRect frame = self.myView.frame;
                     frame.size.height += 30.0;
                     self.myView.frame = frame;
                 }
                 completion:^(BOOL finished){
                     // whatever you need to do when animations are complete
                 }];
Run Code Online (Sandbox Code Playgroud)


jjo*_*son 1

好的,所以我在阅读以下问题的答案后解决了这个问题:Animating a UIView frame, subview UIScrollView does not always animate

在安排动画后,我基本上正在做一些工作,该动画导致 -layoutSubviews在每个子视图上调用,这会自动导致这些视图排列在动画的终点。通过在这项工作完成后安排我的动画,我能够解决我的问题。