你有containerView嗎?有什麼可以在那裏,所以你可以添加和刪除它的子視圖?如果你有兩個viewController,動畫可能會中斷,一個來去一個,沒有一個containerView。我使用rootViewController並使用後面的rootViewcontroller將所有頁面彼此進行動畫化。這裏是我的代碼翻轉,你可能需要做一些編輯,使它爲你工作:
(請記住,自我是rootViewcontroller,一個視圖控制器與一個空白的視圖(顏色,所以它匹配你的看法))
- (void)switchTwoViews:(UIViewController *)view1 otherView:(UIViewController *)view2
{
/*
This method is called to switch views.
It flips the displayed view from the main view to the flipside view and vice-versa.
*/
UIViewController *coming = nil;
UIViewController *going = nil;
UIViewAnimationTransition transition;
[view1.view setUserInteractionEnabled: NO];
[view2.view setUserInteractionEnabled: NO];
if (view1.view.superview == nil) {
coming = view1;
going = view2;
transition = UIViewAnimationTransitionFlipFromLeft;
}
else {
coming = view2;
going = view1;
transition = UIViewAnimationTransitionFlipFromRight;
}
// in some cases the following is needed to size the view
// coming.view.frame = [UIScreen mainScreen].applicationFrame;
// going.view.alpha = 1.0; //uncomment these lines if we want fading of views
// coming.view.alpha = 0.0;
NSArray *viewArray = [[NSArray alloc] initWithObjects:coming, going, nil];
[coming viewWillAppear:YES];
[going viewWillDisappear:YES];
[UIView beginAnimations:@"View Flip" context:viewArray]; {
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidEnd:finished:context:)];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
// coming.view.alpha = 1.0; //uncomment these lines if we want fading of views
// going.view.alpha = 0.0;
[UIView setAnimationTransition:transition forView:self.view cache:YES];
[self.view addSubview: coming.view];
}
[UIView commitAnimations];
}
- (void) animationDidEnd:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
NSArray *viewArray = context;
[((UIViewController *)[viewArray objectAtIndex:1]).view removeFromSuperview];
[[viewArray objectAtIndex:1] viewDidDisappear:YES];
[[viewArray objectAtIndex:0] viewDidAppear:YES];
[[[viewArray objectAtIndex:0] view] setUserInteractionEnabled: YES];
[viewArray release];
}
我有它現在翻轉除了當我回到父視圖。我從翻轉視圖[self.view removeFromSuperview]執行此操作。而你正在做這個[self.view addSubview:coming.view]。我的代碼導致它在返回到父級時不動畫。 – 4thSpace 2009-11-01 17:04:12
我也無法使用animationDidEnd:代碼,因爲這會導致我的翻轉視圖消失。 – 4thSpace 2009-11-01 17:04:54
如果您調用BeginAnimation而不是animationDidEnd,則beginAnimation之後的調用將排隊並且尚未發生。離開事情不是一個好狀態。 翻轉視圖時,會添加一個視圖,另一個視圖會被刪除。如果你不刪除它,那麼它就停留在你的視圖之後,並且你將會遇到問題(至少在上面的代碼中)。繼續玩這些例程,直到你知道發生了什麼。一旦你掌握了,你也可以找到其他方法來做到這一點。 – mahboudz 2009-11-01 17:42:08