详解iPhone开发中各种动画实现效果

来源:互联网 发布:大数据人工智能培训 编辑:程序博客网 时间:2024/06/04 23:31

iPhone开发中各种动画实现效果是本文要介绍的内容,iphone中存在很多好看的动画效果,用于页面的切换等。其中某些是apple私有的,据说私有的无法通过apple的审批。最近工作中刚好用到过其中的某些动画,所以在网上搜了下资料,了解了下这些动画。这里就自己的理解做一下总结,如有错误或遗漏,尽请谅解。

1、UIView 动画

官方API中,使用UIView可以设置5个动画效果,分别为:

  1. UIViewAnimationTransitionNone    不使用动画  
  2.  
  3. UIViewAnimationTransitionFlipFromLeft    从左向右旋转翻页  
  4.  
  5. UIViewAnimationTransitionFlipFromRight    从右向左旋转翻页,与UIViewAnimationTransitionFlipFromLeft相反  
  6.  
  7. UIViewAnimationTransitionCurlUp    卷曲翻页,从下往上  
  8.  
  9. UIViewAnimationTransitionCurlDown    卷曲翻页,从上往下  
  10.  
  11. 详细请参见UIViewAnimationTransition 

例子:

  1. [UIView beginAnimations:@"animationID" context:nil];//开始一个动画块,第一个参数为动画块标识
  2.  
  3. [UIView setAnimationDuration:0.5f];//设置动画的持续时间  
  4.  
  5. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
  6.        //设置动画块中的动画属性变化的曲线,此方法必须在beginAnimations方法和commitAnimations,默认即为UIViewAnimationCurveEaseInOut效果。
  7. 详细请参见UIViewAnimationCurve  
  8.  
  9. [UIView setAnimationRepeatAutoreverses:NO];//设置是否自动反转当前的动画效果  
  10.  
  11. [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
  12. //设置过渡的动画效果,此处第一个参数可使用上面5种动画效果  
  13.  
  14. [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];//页面翻转  
  15.  
  16. [UIView commitAnimations];//提交动画 

2、公共动画效果

使用CATransiton可以设置4种动画效果,分别为:

  1. NSString * const kCATransitionFade;//渐渐消失  
  2.  
  3. NSString * const kCATransitionMoveIn;//覆盖进入  
  4.  
  5. NSString * const kCATransitionPush;//推出  
  6.  
  7. NSString * const kCATransitionReveal;//与MoveIn相反 

例子:

  1. CATransition *animation = [CATransition animation];  
  2.  
  3. animation.duration = 0.5f;  
  4.  
  5. animation.timingFunction = UIViewAnimationCurveEaseInOut;  
  6.  
  7. animation.type = kCATransitionPush;//设置上面4种动画效果  
  8.  
  9. animation.subtype = kCATransitionFromTop;//设置动画的方向,有四种,
  10.  
  11. 分别为kCATransitionFromRight、kCATransitionFromLeft、kCATransitionFromTop、kCATransitionFromBottom  
  12.  
  13. [self.view.layer addAnimation:animation forKey:@"animationID"]; 

3、私有动画

iphone种还有很多动画是苹果私有的,例如删除照片的动画等,

私有动画可以直接在animation.type中传入动画的字符串即可。动画有以下几种:

  1. cube:像立方体一样翻转  
  2.  
  3. suckEffect:渐渐缩小,与删除照片动画一样  
  4.  
  5. oglFlip:上下旋转,当subType为fromLeft或者fromRight时,
  6. 与UIViewAnimationTransitionFlipFromLeft和UIViewAnimationTransitionFlipFromRight一样  
  7.  
  8. rippleEffect:水波效果  
  9.  
  10. pageCurl:与UIViewAnimationTransitionCurlUp一样  
  11.  
  12. pageUnCurl:与UIViewAnimationTransitionCurlDown一样  
  13.  
  14. cameraIrisHollowOpen:First half of cameraIris.  
  15.  
  16. cameraIrisHollowClose:Second half of cameraIris 

以上所有动画效果的demo请见http://www.cocoachina.com/bbs/read.php?tid-11820.html,在此感谢楼主的分享,给我的学习带来很到的帮助。

UIViewAnimationState描述:http://www.iphonedevwiki.net/index.php/UIViewAnimationState

同时,本人在使用UIView实现suckEffect缩小的效果过程中遇到一个问题(不知道如何定位),经过搜索终觅得解决方法,分享如下:

  1. [UIView beginAnimations:@"suck" context:NULL];  
  2. [UIView setAnimationTransition:103 forView:self.view cache:YES];  
  3. [UIView setAnimationDuration:0.5f];  
  4. if (self.interfaceOrientation  == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
  5. {  
  6.  [UIView setAnimationPosition:CGPointMake(44, 42)];  
  7. }else {  
  8. [UIView setAnimationPosition:CGPointMake(320 , 42)];  
  9. }  
  10. [UIView commitAnimations]; 

其中setAnimationPosition方法就是用于设置缩小点的位置的,此处虽然会报一个警告,但是结果还是正确的。

小结:详解iPhone开发中各种动画实现效果的内容介绍完了,希望通过本文的学习能对你有所帮助!

原创粉丝点击