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

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

 

实现iPhone漂亮的动画效果主要有两种方法,

   一种是UIView层面的,

  一种是使用CATransition进行更低层次的控制,

      第一种是UIView,UIView方式可能在低层也是使用CATransition进行了封装,它只能用于一些简单的、常用的效果展现,这里写一个常用的示例代码,供大家参考。

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

       第二种方式相对复杂一些,但如果更好的进行控制,还是使用这种方法吧,

基本使用方法可以看一下如下例子:

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

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

[animation setType:@"suckEffect"];

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

pageCurl 向上翻一页pageUnCurl 向下翻一页 
rippleEffect 滴水效果 
suckEffect 收缩效果,如一块布被抽走 
cube 立方体效果 
oglFlip 上下翻转效果
更多的效果参考:http://iphonedevwiki.net/index.php?title=CATransition
最后再给出一种常用代码供大家参考

//创建CATransition对象

CATransition *animation = [CATransitionanimation];

//相关参数设置

[animation setDelegate:self];

[animation setDuration:1.0f];

[animation setTimingFunction:UIViewAnimationCurveEaseInOut];

//向上卷的参数

if(!isCurl)

{

//设置动画类型为pageCurl,并只卷一半

[animation setType:@"pageCurl"];   

animation.endProgress=0.8;

}

//向下卷的参数

else

{

//设置动画类型为pageUnCurl,并从一半开始向下卷

[animation setType:@"pageUnCurl"];

animation.startProgress=0.2;

}

//卷的过程完成后停止,并且不从层中移除动画

[animation setFillMode:kCAFillModeForwards];

//[animation setSubtype:kCATransitionFromBottom];

[animation setSubtype:kCATransitionFromRight];

[animation setRemovedOnCompletion:NO];

isCurl=!isCurl;

[self.viewexchangeSubviewAtIndex:0withSubviewAtIndex:1];

[[self.viewlayer] addAnimation:animation forKey:@"pageCurlAnimation"];

原文地址:https://www.cnblogs.com/hellocby/p/2530875.html