iPhone动画效果类型及实现方法

本文要介绍的内容,主要介绍了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:有四种类型:

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

setSubtype:有四种类型:

  1. kCATransitionFromRight;                    
  2. kCATransitionFromLeft(默认值)                    
  3. kCATransitionFromTop;                    
  4. kCATransitionFromBottom         
  5. 注: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"];    

 

可以用的效果主要有:

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

posted on 2013-05-07 15:13  骑驴走天下  阅读(268)  评论(0编辑  收藏  举报

导航