天堂向右,我依然向左

天下之大,虽离家千里,何处不可往!何事不可为!
生活之路,纵坎坷曲折,当奋斗不息,则精彩纷呈!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

在iPhone中动画过度非常简单.

 

首先获取当前的图形上下文:

Java代码 
  1. CGContextRef context = UIGraphicsGetCurrentContext();  

 

接着设置一些动画属性用于开始动画:

 

Java代码 
  1. [UIView beginAnimations:nil context:context];  
  2. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
  3. [UIView setAnimationDuration:1.0];  
 

然后设置想要过度的对象的最终状态.

 

Java代码 
  1. [big setFrame:SMALLRECT];  
  2. [big setAlpha:0.5];  
  3. [little setFrame:BIGRECT];  
  4. [little setAlpha:1.0];  

 

最后提交动画,这样一个动画就会自动生成了

 

Java代码 
  1. [UIView commitAnimations];  

 

其中,setAnimationCurve是设置动画的方式,他有下面集中方式:

 

  • UIViewAnimationCurveEaseInOut  //开始和结束时动画效果比较慢
  • UIViewAnimationCurveEaseIn       //开始动画效果比较慢
  • UIViewAnimationCurveEaseOut     //结束动画效果比较慢
  • UIViewAnimationCurveLinear         //平滑的动画效果
而,setAnimationDuration则是设置动画的持续时间.
 
下面是两个UIView之间的动画过度
Iphone代码 
  1.        // Start Animation Block  
  2. //CGContextRef context = UIGraphicsGetCurrentContext();  
  3. CGContextRef context = nil;  
  4. [UIView beginAnimations:nil context:context];  
  5. [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:[self superview] cache:YES];  
  6. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
  7. [UIView setAnimationDuration:1.0];  
  8.   
  9. // Animations  
  10. [[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1];  
  11.   
  12. // Commit Animation Block  
  13. [UIView commitAnimations];  
 
页面之间的过度主要依靠UIView setAnimationTransition: forView: cache: 这个方法以及 exchangeSubviewAtIndex:withSubviewAtIndex:
前者通过UIView静态方法设置过度的动画种类,后者实现真正的过度函数掉用.种类有如下五种:

 

Iphone代码 
  1. typedef enum {  
  2.     UIViewAnimationTransitionNone,  
  3.     UIViewAnimationTransitionFlipFromLeft,  
  4.     UIViewAnimationTransitionFlipFromRight,  
  5.     UIViewAnimationTransitionCurlUp,  
  6.     UIViewAnimationTransitionCurlDown,  
  7. } UIViewAnimationTransition;  
 

 

除了这种简单的动画方式外,其实还有一种利用QuartzCore来做过度动画.不同的地方在于,这个过度动画作用于层,换句话说,他动画直接做用于整个UIView,而不像UIView的动画,可以作用于UIView局部或本身.当UIView作用与本身时,实际上也就相当于是对层的动画了.

 

Iphone代码 
  1. CATransition *animation = [CATransition animation];  
  2. [animation setDelegate:self];  
  3. [animation setDuration:1.0f];  
  4. [animation setTimingFunction:UIViewAnimationCurveEaseInOut];  
  5. [animation setType: kCATransitionMoveIn];  
  6. [animation setSubtype: kCATransitionFromBottom];  
  7.   
  8.   
  9. [[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1];  
  10. [[[self superview] layer] addAnimation:animation forKey:@"transitionViewAnimation"];      
 

setDuration:和UIView中的动画效果一样,持续时间.

setTimingFunction:是动画的种类,和UIView一样,比如匀速动画,快速开始结束等.

setType:是指定动画的类型,他有:

 

  1. kCATransitionFade (淡入淡出来补给动画)
  2. kCATransitionMoveIn(从一个方向覆盖的方式来补给动画)
  3. kCATransitionPush(推送的方式来补给动画)
  4. kCATransitionReveal(一个试图展现出另外另外一个试图的方式)
当除了第一种方式外(淡入淡出),可以通过setSubType:来制定动画的方向(因为这些动画都是直接着用于层的,所以相当于只有试图间的切换过渡).动画方向有4个:
  1. kCATransitionFromRight
  2. kCATransitionFromLeft
  3. kCATransitionFromTop
  4. kCATransitionFromBottom
 
除此之外,还存在一些未公开的动画方式,这些方式有些不能在模拟器中看到效果,但是真机可行.
Iphone代码 
  1. // Curl the image up or down. This runs only on the iPhone and will not  
  2.     // produce any effect from the simulator  
  3.     CATransition *animation = [CATransition animation];  
  4.     [animation setDelegate:self];  
  5.     [animation setDuration:1.0f];  
  6.     [animation setTimingFunction:UIViewAnimationCurveEaseInOut];  
  7.     [animation setType:(notCurled ? @"pageCurl" : @"pageUncurl")];  
  8.       
  9.     /*  
  10.         mapCurl, mapUncurl  
  11.         pageCurl, pageUncurl  
  12.         suckEffect, spewEffect,  
  13.         cameraIris, cameraIrisHollowOpen, cameraIrisHollowClose  
  14.         genieEffect, unGenieEffect,  
  15.         rippleEffect,  
  16.         twist,  
  17.         tubey,  
  18.         swirl,  
  19.         charminUltra  
  20.         zoomIn, zoomOut  
  21.         oglFlip  
  22.      */  
  23.       
  24.     //让他不给删除掉  
  25.     [animation setRemovedOnCompletion:NO];  
  26.     [animation setFillMode: @"extended"];  
  27.     [animation setRemovedOnCompletion: NO];  
  28.       
  29.     notCurled = !notCurled;  
  30.       
  31.     [[[self.view viewWithTag:TOP_LAYER_VIEW] layer] addAnimation:animation forKey:@"pageFlipAnimation"]; 
posted on 2010-08-24 15:55  老舟  阅读(1344)  评论(0编辑  收藏  举报