OC学习之UIView动画设置

来源:互联网 发布:网络安全技术是什么 编辑:程序博客网 时间:2024/06/05 18:45

ios各种动画效果

最普通动画: 

//开始动画 [UIView beginAnimations:nil context:nil];  //设定动画持续时间 [UIView setAnimationDuration:2]; //动画的内容 frame.origin.x += 150; [img setFrame:frame]; //动画结束 [UIView commitAnimations]; 
连续动画:一个接一个地显示一系列的图像 
NSArray *myImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"myImage1.png"], [UIImage imageNamed:@"myImage2.png"], [UIImage imageNamed:@"myImage3.png"], [UIImage imageNamed:@"myImage4.gif"], nil]; UIImageView *myAnimatedView = [UIImageView alloc]; [myAnimatedView initWithFrame:[self bounds]]; myAnimatedView.animationImages = myImages; //animationImages属性返回一个存放动画图片的数组 myAnimatedView.animationDuration = 0.25; //浏览整个图片一次所用的时间 myAnimatedView.animationRepeatCount = 0; // 0 = loops forever 动画重复次数 [myAnimatedView startAnimating]; [self addSubview:myAnimatedView]; [myAnimatedView release]; 
CATransition Public API动画: 
CATransition *animation = [CATransition animation]; //动画时间     animation.duration = 0.5f; //先慢后快     animation.timingFunction = UIViewAnimationCurveEaseInOut; animation.fillMode = kCAFillModeForwards; //animation.removedOnCompletion = NO; //各种动画效果 /* kCATransitionFade; kCATransitionMoveIn; kCATransitionPush;z kCATransitionReveal; */ /* kCATransitionFromRight; kCATransitionFromLeft; kCATransitionFromTop; kCATransitionFromBottom; */ //各种组合 animation.type = kCATransitionPush; animation.subtype = kCATransitionFromRight; [self.view.layer addAnimation:animation forKey:@"animation"]; 
CATransition Private API动画: 
animation.type可以设定为以下效果 
动画效果汇总: 
/* suckEffect(三角) rippleEffect(水波抖动) pageCurl(上翻页) pageUnCurl(下翻页) oglFlip(上下翻转) cameraIris/cameraIrisHollowOpen/cameraIrisHollowClose  (镜头快门,这一组动画是有效果,只是很难看,不建议使用 而以下为则黑名单: spewEffect: 新版面在屏幕下方中间位置被释放出来覆盖旧版面. - genieEffect: 旧版面在屏幕左下方或右下方被吸走, 显示出下面的新版面 (阿拉丁灯神?). - unGenieEffect: 新版面在屏幕左下方或右下方被释放出来覆盖旧版面. - twist: 版面以水平方向像龙卷风式转出来. - tubey: 版面垂直附有弹性的转出来. - swirl: 旧版面360度旋转并淡出, 显示出新版面. - charminUltra: 旧版面淡出并显示新版面. - zoomyIn: 新版面由小放大走到前面, 旧版面放大由前面消失. - zoomyOut: 新版面屏幕外面缩放出现, 旧版面缩小消失. - oglApplicationSuspend: 像按"home" 按钮的效果. */ UIView Animations 动画: [UIView beginAnimations:@"animationID" context:nil]; [UIView setAnimationDuration:0.5f]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationRepeatAutoreverses:NO]; //以下四种效果 /* [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];//oglFlip, fromLeft [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];//oglFlip, fromRight  [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES]; [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES]; */ [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0]; [UIView commitAnimations]; 
IOS4.0新方法: 
方法: 
+(void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations; 

方法实现:
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion; //多一个动画结束后可以执行的操作. //下边是嵌套使用,先变大再消失的动画效果. [UIView animateWithDuration:1.25 animations:^{ CGAffineTransform newTransform = CGAffineTransformMakeScale(1.2, 1.2); [firstImageView setTransform:newTransform]; [secondImageView setTransform:newTransform];} completion:^(BOOL finished){ [UIView animateWithDuration:1.2 animations:^{ [firstImageView setAlpha:0]; [secondImageView setAlpha:0];} completion:^(BOOL finished){ [firstImageView removeFromSuperview]; [secondImageView removeFromSuperview]; }]; }];


  1. -(void)dochange1  
  2. {  
  3.     [UIView beginAnimations:@"animation" context:nil];  
  4.     [UIView setAnimationDuration:1.0f];  
  5.     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//翻页路线  
  6. //    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight  
  7. //            forView:self.view cache:YES];  
  8. //    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft  
  9. //                           forView:self.view cache:YES];  
  10.     [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown  
  11.                            forView:self.view cache:YES];  
  12. //    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp  
  13. //                           forView:self.view cache:YES];  
  14. //    [UIView setAnimationTransition:UIViewAnimationTransitionNone  
  15. //                           forView:self.view cache:YES];  
  16.     [UIView commitAnimations];  
  17. //    BackView *next=[[BackView alloc] init];  
  18. //    [self.navigationController pushViewController:next animated:YES];  
  19. }  
  20.   
  21. -(void)dochange2  
  22. {  
  23.     [UIView beginAnimations:@"animation" context:nil];  
  24.     [UIView setAnimationDuration:1.0f];  
  25.     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
  26.     [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];  
  27.     //交换本视图控制器中的两个view  
  28.     [self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:0];  
  29.     [UIView setAnimationDidStopSelector:@selector(animationFinish:)];  
  30.     [UIView commitAnimations];  
  31.       
  32. }  
  33.   
  34. -(void)dochange3  
  35. {  
  36.       
  37.    CATransition *animation=[CATransition animation];  
  38.     animation.delegate=self;  
  39.    animation.duration=2.0f;//间隔时间  
  40.     animation.timingFunction=UIViewAnimationCurveEaseInOut;  
  41.       
  42.     //transition.type的四种类型  
  43.       
  44.   
  45. //    animation.type=kCATransitionPush;//揭开效果  
  46.       
  47. //    animation.type=kCATransitionFade;//淡化效果  
  48. //    animation.type=kCATransitionMoveIn;//推挤效果  
  49.     animation.type=kCATransitionReveal;//覆盖效果  
  50.       
  51.       
  52.     //私有类型动画  
  53. //    animation.type=@"cube";//立方体效果  
  54. //    animation.type=@"suckEffect";//吸收效果  
  55. //    animation.type=@"oglFlip";//翻转效果   //subtype中的right和left效果一样  
  56. //    animation.type=@"rippleEffect";//波纹效果  
  57. //    animation.type=@"pageCurl";//翻页效果  
  58. //    animation.type=@"pageUnCurl";//反翻页效果  
  59. //    animation.type=@"cameraIrisHollowOpen";//镜头开效果  
  60. //    animation.type=@"cameraIrisHollowClose";//镜头关效果  
  61. //    animation.type=@"puzzle";  
  62.       
  63.     animation.subtype=kCATransitionFromTop;//从上面  
  64.       
  65. //    animation.subtype=kCATransitionFromBottom;//从下面  
  66.       
  67. //    animation.subtype=kCATransitionFromLeft;//从左边  
  68. //    animation.subtype=kCATransitionFromRight;//从右边  
  69.       
  70.   [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];  
  71.   [self.view.layer addAnimation:animation forKey:@"animation"];  
  72.        
  73. }  

原创粉丝点击