下面的代码段来自《Beginning iOS 5 Development》第六章,是一个多view应用的例子,本想贴整个例子,但是代码实在太多。
-(IBAction)switchViews:(id)sender
{
[UIView beginAnimations:@"View Flip" context:nil];
//开始动画,View Flip是动画类型,对文档不是很熟,找不到所有支持的类型
[UIView setAnimationDuration:1.5];
//动画时间
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//开始和结束的时候速度慢,中间速度快
if(self.yellowViewController.view.superview==nil)
{//当yellowViewController存在,且它的view被显示时,它的superview才不会等于nil
//即,这里的代码在yellowViewController.view显示时不会执行,在blueViewController.view显示时才执行
if(self.yellowViewController==nil)
{//如果yellowViewController未初始化,则调用YelowView.xib文件初始化,参数中无xib扩展名
self.yellowViewController=[[BIDYellowViewController alloc] initWithNibName:@"YellowView" bundle:nil];
}
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
//从右往左翻,启用缓存
[blueViewController.view removeFromSuperview];
[self.view insertSubview:self.yellowViewController.view atIndex:0];
//移除blueViewController.view,并把yellowViewController.view加入self.view
}else
{
if(self.blueViewController==nil)
{//在内存资源不足时,blueViewController资源可能会被系统回收,所以也需要判断
self.blueViewController=[[BIDBlueViewController alloc] initWithNibName:@"BlueView" bundle:nil];
}
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp
forView:self.view cache:YES];
//从下往上翻
[yellowViewController.view removeFromSuperview];
[self.view insertSubview:self.blueViewController.view atIndex:0];
}
[UIView commitAnimations];
//标记动画结束
}