ObjC - UIViews:独立制作动画视图和子视图?

时间:2010-10-10 02:07:21

标签: objective-c animation uiview core-animation

我有一个UIView及其子视图。当我为myView设置动画(size.x + 20)时,我的子视图也会被动画化,但我想同时独立翻译我的子视图(origin.x-40)(由于myView的大小调整,子视图没有移动)。

我能够通过调整翻译(或更好的位置,因为我可能想稍后更改).fromValue,.toValue和.duration来补偿由myView更改引起的移动。

UIView beginAnimations:nil context:NULL];
[myView setFrame:pVnewFrame];
[UIView commitAnimations];

CABasicAnimation *bBTranslateAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; 
bBTranslateAnimation.delegate = self;
bBTranslateAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(160, 480)];
bBTranslateAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(160, 436)];
bBTranslateAnimation.duration = 0.35;
[buttonBar addAnimation:bBTranslateAnimation forKey:@"anim"];

但这不是正确的解决方案。有独立移动视图和子视图的好方法吗? (设置autoresizeSubviews和autoresizingMask没有帮助)

提前致谢!

1 个答案:

答案 0 :(得分:3)

这样做的方法是制作两个视图的兄弟,而不是让一个视图成为另一个视图的子视图。只需将作为子视图的视图放在作为父视图的视图顶部,它应该看起来像你想要的那样。

如果希望“子”视图剪切到前父视图的内容,则可能需要将作为子视图的视图放在容器视图中,该容器视图是父视图的兄弟,并且将容器视图设置为opaque = no并具有清晰的背景颜色。但是,这会影响表现。