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

实现iphone漂亮的动画效果主要有两种方法,一种是UIView层面的,一种是使用CATransition进行更低层次的控制,
第一种是UIView,UIView方式可能在低层也是使用CATransition进行了封装,它只能用于一些简单的、常用的效果展现,这里写一个常用的示例代码,供大家参考。

view plain copy to clipboard print ?

    [UIView beginAnimations:@ "Curl"context:nil]; //动画开始   
  1. [UIView setAnimationDuration:0.75];  [UIView setAnimationDelegate:self];  
  2. [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:myview cache:YES];  [myview removeFromSuperview];  
  3. [UIView commitAnimations];  

[UIView beginAnimations:@"Curl"context:nil];//动画开始[UIView setAnimationDuration:0.75];[UIView setAnimationDelegate:self];[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:myview cache:YES];[myview removeFromSuperview];[UIView commitAnimations]; 

第二种方式相对复杂一些,但如果更好的进行控制,还是使用这种方法吧,基本使用方法可以看一下如下例子:

view plain copy to clipboard print ?

    CATransition *animation = [CATransition animation];  
  1. [animation setDuration:1.25f];  [animation setTimingFunction:[CAMediaTimingFunction  
  2. functionWithName:kCAMediaTimingFunctionEaseIn]];  [animation setType:kCATransitionReveal];  
  3. [animation setSubtype: kCATransitionFromBottom];  [self.view.layer addAnimation:animation forKey:@"Reveal"];  

CATransition *animation = [CATransition animation];[animation setDuration:1.25f];[animation setTimingFunction:[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseIn]];[animation setType:kCATransitionReveal];[animation setSubtype: kCATransitionFromBottom];[self.view.layer addAnimation:animation forKey:@"Reveal"]; 

这里使用了setType与setSubtype组合,这使用个比较保险,因为他的参数就是官方API里定义的,他们的参数说明可以参考如下:

view plain copy to clipboard print ?

    setType:可以返回四种类型:  
  1. kCATransitionFade淡出  kCATransitionMoveIn覆盖原图  
  2. kCATransitionPush推出  kCATransitionReveal底部显出来  
  3. setSubtype:也可以有四种类型:  kCATransitionFromRight;  
  4. kCATransitionFromLeft(默认值)  kCATransitionFromTop;  
  5. kCATransitionFromBottom  

setType:可以返回四种类型:kCATransitionFade淡出kCATransitionMoveIn覆盖原图kCATransitionPush推出kCATransitionReveal底部显出来setSubtype:也可以有四种类型:kCATransitionFromRight;kCATransitionFromLeft(默认值)kCATransitionFromTop;kCATransitionFromBottom 

还有一种设置动画类型的方法,不用setSubtype,只用setType

view plain copy to clipboard print ?

    [animation setType:@ "suckEffect"];  

[animation setType:@"suckEffect"]; 

这里的suckEffect就是效果名称,可以用的效果主要有:

view plain copy to clipboard print ?

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

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

最后再给出一种常用代码供大家参考。

view plain copy to clipboard print ?

    // Curl the image up or down   
  1. CATransition *animation = [CATransition animation];  [animation setDuration:0.35];  
  2. [animation setTimingFunction:UIViewAnimationCurveEaseInOut];  if (!curled){  
  3. //animation.type = @"mapCurl";   animation.type = @"pageCurl";  
  4. animation.fillMode = kCAFillModeForwards;  animation.endProgress = 0.99;  
  5. else {  //animation.type = @"mapUnCurl";   
  6. animation.type = @"pageUnCurl";  animation.fillMode = kCAFillModeBackwards;  
  7. animation.startProgress = 0.01;  }  
  8. [animation setRemovedOnCompletion:NO];  [view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];  
  9. [view addAnimation:animation forKey"pageCurlAnimation"];  // Disable user interaction where necessary   
  10. if (!curled) {     
  11. else {     
  12. }  curled = !curled;  

// Curl the image up or downCATransition *animation = [CATransition animation];[animation setDuration:0.35];[animation setTimingFunction:UIViewAnimationCurveEaseInOut];if (!curled){//animation.type = @"mapCurl";animation.type = @"pageCurl";animation.fillMode = kCAFillModeForwards;animation.endProgress = 0.99;} else {//animation.type = @"mapUnCurl";animation.type = @"pageUnCurl";animation.fillMode = kCAFillModeBackwards;animation.startProgress = 0.01;}[animation setRemovedOnCompletion:NO];[view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];[view addAnimation:animation forKey"pageCurlAnimation"];// Disable user interaction where necessaryif (!curled) { } else { }curled = !curled; 

你可能感兴趣的:(iPhone的动画效果类型及实现方法)