本文要介绍的内容,主要介绍了iPhone动画的实现方法。

在iOS中,主要有用到四种动画效果

1、Core Animation

2、CATransition

实现iphone漂亮的动画效果主要有两种方法,一种是UIView层面的,一种是使用CATransition进行更低层次的控制.

1、UIView(Core Animation)

例子:

模式一:

CGContextRef context = UIGraphicsGetCurrentContext();  
[UIView beginAnimations:nil context:context];  
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
[UIView setAnimationDelegate:self];  
[UIView setAnimationDuration:1.0];          //动画持续的时间  
//这里添加你对UIView所做改变的代码  
//[UIView setAnimationDidStopSelector:@selector(animationFinished:)];   //动画停止后,执行某个方法  
[UIView commitAnimations];

 

模式二:(使用Cocoa Touch)

CGContextRef context = UIGraphicsGetCurrentContext();  
[UIView beginAnimations:nil context:context];  
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
[UIView setAnimationDuration:1.0];  
// Cocoa Touch    
[UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView:myView cache:YES];  
[UIView setAnimationDelegate:self];  
//[UIView setAnimationDidStopSelector:@selector(animationFinished:)]; //动画停止后,执行某个方法  
[UIView commitAnimations];

 

动画方式(UIViewAnimationTransition): 

UIViewAnimationTransitionFlipFromLeft              //从左向右翻转  
UIViewAnimationTransitionFlipFromRight             //从右向左翻转  
UIViewAnimationTransitionCurlUp                    //从下向上翻页  
UIViewAnimationTransitionCurlDown                  //从上向下翻页

 

模式三:

UIView的 + (void)animateWithDuration
:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

方法。
这个方法是在iOS4.0之后才支持的。

[UIView animateWithDuration:1.0 animations:^{
        firstView.alpha = 0.0;
        secondView.alpha = 1.0;
}];

[UIView animateWithDuration:2.0
                 animations:^{
                     oldImageView.alpha = 0.0;
                     newImageView.alpha = 1.0;
                     //imageView.center = CGPointMake(500.0, 512.0);
                 }
                 completion:^(BOOL finished){
                     [UIView animateWithDuration:4.0
                                      animations:^{
                                          newImageView.center = CGPointMake(500.0, 512.0);
                                      }];
                 }];

2、CATransition

CATransition *animation = [CATransition animation];   
animation.delegate = self;  
animation.duration = 1.0f;       //动画执行时间  
animation.timingFunction = UIViewAnimationCurveEaseInOut;  
animation.type = kCATransitionFade;  
animation.subtype = kCATransitionFromRight;  
// 这里添加你对UIView所做改变的代码  
[[myView layer] addAnimation:animation forKey:@"animation"];

 

setType:有四种类型:

kCATransitionFade  //交叉淡化过渡                     
 
kCATransitionMoveIn   //移动覆盖原图                     
 
kCATransitionPush     //新视图将旧视图推出去                     
 
kCATransitionReveal   //底部显出来

setSubtype:有四种类型:

kCATransitionFromRight;                     
 
kCATransitionFromLeft(默认值)                     
 
kCATransitionFromTop;                     
 
kCATransitionFromBottom          
 
注:kCATransitionFade 不支持Subtype

2.1、CATransition(只使用setType,参数是NSString)    

CATransition *animation = [CATransition animation];       
animation.delegate = self;       
animation.duration = 1.0f;   //动画执行时间       
animation.timingFunction = UIViewAnimationCurveEaseInOut;       
animation.type = @"suckEffect";// 这里添加你对UIView所做改变的代码       
[[myView layer] addAnimation:animation forKey:@"animation"];

 

可以用的效果主要有:

pageCurl     //向上翻一页       
 
pageUnCurl   //向下翻一页        
 
rippleEffect   //滴水效果        
 
suckEffect     //收缩效果,如一块布被抽走     
 
cube       //立方体效果      
 
oglFlip      //上下翻转效果