2010-10-10 56 views
2

我有一個UIView及其子視圖。當我動畫myView(size.x + 20)的動畫時,我的子視圖也被動畫,但是我想同時獨立翻譯我的子視圖(origin.x-40)(沒有子視圖因爲myView的大小調整而移動)。ObjC - UIViews:獨立動畫視圖和子視圖?

我能夠通過調節翻譯做(或更好的位置,因爲我可能要修改y後).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沒有幫助)

在此先感謝!

回答

3

這樣做的方法是使兩個視圖的兄弟姐妹,而不是使一個視圖的另一個子視圖。只需將視圖頂部的子視圖放在父視圖的頂部,它應該看起來像你想要的那樣。

如果您希望「子視圖」剪輯到前父視圖的內容,您可能需要將視圖作爲父視圖的同級視圖的同級視圖中的子視圖,並將容器視圖設置爲opaque = no並且背景顏色清晰。但是,這會影響性能。