动画学习1 Animating Views with Blocks

dszfasdfasdfz


有五个函数

 ios 4.0 以后才支持

Animating Views with Blocks


这三个函数,用来支持多个不同的view(view1,view2。。。。。。)的动画
Animate changes to one or more views
这个函数,用来对一个容器view的一个动画进行操作,这个容器内的所有subview将一起动画

这个函数,用来对一个容器view的多个subview进行动画,容器view中的FromViewtoView的切换等动画
比如:subview1翻转后显示subview2

/* 翻转示例

     这个方法完成firstView翻转过来显示背面的secondview,并且将firstview移除。

     需要注意的事情是,我们在翻转的时候要给firstViewsecondview增加一个共同的容器view

     将这两个view作为一个容器viewsubview,才能正确执行,否则系统将会默认将self.view作为容器view,导致效果不正确。

     

     viewdidload中增加一下代码:

     - (void)viedDidLoad{

        [self.secondView removeFromSuperview];

        UIView *containerView = [[UIView alloc] initWithFrame:self.firstView.frame];

        self.firstView.frame = containerView.bounds;

        self.secondView.frame = containerView.bounds;

        [containerView addSubview:self.secondView];

        [containerView addSubview:self.firstView];

        [self.view addSubview:containerView];

     

     [super viewDidLoad];

     }

     

     - (void)animation {

        [UIView transitionFromView:self.firstView toView:self.secondView duration:2.f options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionShowHideTransitionViews completion:^(BOOL finished) {

        }];

     }

*/

     



options的选项:
看下面代码中的注释:


[cpp]  view plain copy
  1. - (NSInteger)animationOption: (NSInteger)index {  
  2.     NSInteger type = 0;  
  3.     switch (index) {  
  4.         case 0: {// 跟父类作为一个整体一起动画,此方式为默认方式  
  5.             type = UIViewAnimationOptionLayoutSubviews;  
  6.             break;  
  7.         }  
  8.         case 1: {// 在动画运行过程中,允许用户与之交互操作  
  9.             /* 
  10.              注意1,UIViewAnimationOptionAllowUserInteraction只是说允许用户点击,但在动画过程中, 
  11.              比如一个让btnOne从(0,0)移动到(100,0)的动画,在动画播放的过程中其实btnOne的坐标已经是(100,0)了 你点击在view上显示的btn是没办法获取响应的,你需要在(100,0)处进行点击才能让btnOne响应你的点击事件。 
  12.              解决这个问题的一种方法就是用定时器不断刷,把动画微分化: 
  13.              - (void)viewDidLoad 
  14.              { 
  15.                 _btnMove = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
  16.                 [_btnMove addTarget:self action:@selector(btnMoveDidSelect) forControlEvents:UIControlEventTouchUpInside]; 
  17.                 _btnMove.frame = CGRectMake(0, 0, 80, 40); 
  18.                 [_btnMove setTitle:@"touch me!" forState:UIControlStateNormal]; 
  19.                 [self.view addSubview:_btnMove]; 
  20.                 [NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector:@selector(buttonMoved) userInfo:nil repeats:YES]; 
  21.                 [super viewDidLoad]; 
  22.                 return; 
  23.              } 
  24.               
  25.              - (void)buttonMoved 
  26.              { 
  27.                 if(_btnMove.frame.origin.x != 250) 
  28.                 { 
  29.                     [UIView beginAnimati*****:nil context:nil]; 
  30.                     [UIView setAnimationDuration:0.01f]; 
  31.                     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
  32.                     [_btnMove setFrame: CGRectMake(_btnMove.frame.origin.x+1, _btnMove.frame.origin.y,_btnMove.frame.size.width, _btnMove.frame.size.height)]; 
  33.                     [UIView commitAnimati*****]; 
  34.                 } 
  35.              } 
  36.               
  37.              注意2:如果你设置了UIViewAnimationOptionAllowUserInteraction,界面上其他的button等控件可以接受事件,但是你不设置UIViewAnimationOptionAllowUserInteraction,其他的控件事件都收不到。也就是说如果设置了UIViewAnimationOptionAllowUserInteraction,在动画期间,主线程会有时间接受事件。 
  38.              */  
  39.             type = UIViewAnimationOptionAllowUserInteraction;  
  40.             break;  
  41.         }  
  42.         case 2: {// 从当前状态开始动画。例如如下代码  
  43.             /* 
  44.              - (void)doAnimation1 { 
  45.                 [UIView animateWithDuration:5.0 
  46.                                       delay:0.0 
  47.                                     options:nil 
  48.                 animations:^{ 
  49.                     self.firstView.frame = CGRectMake(0, 0, 200, 200); 
  50.                     [self performSelector:@selector(doAnimation2) withObject:nil afterDelay:1]; 
  51.                 } 
  52.                 completion:^(BOOL finished){}]; 
  53.             } 
  54.               
  55.              - (void)doAnimation2 { 
  56.                 [UIView animateWithDuration:5.0 
  57.                                       delay:0.0 
  58.                                     options:UIViewAnimationOptionBeginFromCurrentState 
  59.                 animations:^{ 
  60.                     self.firstView.frame = CGRectMake(660, 660, 200, 200); 
  61.                 } 
  62.                 completion:^(BOOL finished){}]; 
  63.             } 
  64.              当5秒的doAnimation1动画运行1秒后,开始doAnimation2动画(doAnimation2设定了UIViewAnimationOptionBeginFromCurrentState参数),则doAnimation2的动画是从doAnimation1运行到1秒的状态为开始状态继续运行。如果不传入这个参数,则doAnimation2的开始状态为doAnimation1完整运行结束后的状态。 
  65.              */  
  66.             type = UIViewAnimationOptionBeginFromCurrentState;  
  67.             break;  
  68.         }  
  69.         case 3: {// 重复执行一个动画,从初始状态到结束状态,然后瞬间跳到初始状态继续无限次的执行同一动作  
  70.             type = UIViewAnimationOptionRepeat;  
  71.             break;  
  72.         }  
  73.         case 4: {// 反向执行一个动画。当动画执行完一遍后,沿原路径反向执行一遍。这个属性必须跟UIViewAnimationOptionRepeat一起使用  
  74.             type = UIViewAnimationOptionAutoreverse;  
  75.             break;  
  76.         }  
  77.         case 5: {// 忽略子层嵌套的动画的时间间隔。例如如下代码  
  78.             /* 
  79.              [UIView transitionWithView:self.firstView duration:4.f options:nil animations:^{ 
  80.               
  81.                     self.firstView.frame = CGRectMake(0, 0, 200, 200);  
  82.               
  83.                     [UIView transitionWithView:self.firstView duration:1.f options:nil animations:^{ 
  84.               
  85.                             self.secondView.frame = CGRectMake(0, 0, 200, 200);        
  86.               
  87.                     } completion:^(BOOL finished) {}]; 
  88.              } completion:^(BOOL finished) {}]; 
  89.              self.firstView的动画时间是4秒,self.secondView的动画时间是1秒, 
  90.              如果子层不设置UIViewAnimationOptionOverrideInheritedDuration(如父层和子层都是nil), 
  91.              则子层默认继承父层的时间,忽略自己的时间。 
  92.               
  93.              若是子层设置UIViewAnimationOptionOverrideInheritedDuration属性, 
  94.              则子层将按照自身设置的1秒的时间执行。 
  95.              */  
  96.             type = UIViewAnimationOptionOverrideInheritedDuration;  
  97.             break;  
  98.         }  
  99.         case 6: {// 忽略子层嵌套的动画属性的时间 (如UIViewAnimationOptionCurveEaseInOut) 同上  
  100.             type = UIViewAnimationOptionOverrideInheritedCurve;  
  101.             break;  
  102.         }  
  103.         case 7: {// 允许同一个view的多个动画同时进行  
  104.             type = UIViewAnimationOptionAllowAnimatedContent;  
  105.             break;  
  106.         }  
  107.         case 8: {// 控制两个subview的显示和隐藏  
  108.             /* 
  109.              控制两个subview的显示和隐藏(功能类似于addsubview和removefromsuperview),使用这个属性(一般在transitionFromView:toView:duration:options:completion:这个方法中使用),FromView会被隐藏,toView会被显示 
  110.               
  111.              - (void)viedDidLoad{ 
  112.                 [self.secondView removeFromSuperview]; 
  113.                 UIView *containerView = [[UIView alloc] initWithFrame:self.firstView.frame]; 
  114.                 self.firstView.frame = containerView.bounds; 
  115.                 self.secondView.frame = containerView.bounds; 
  116.                 [containerView addSubview:self.secondView]; 
  117.                 [containerView addSubview:self.firstView]; 
  118.                 [self.view addSubview:containerView]; 
  119.               
  120.                 [super viewDidLoad]; 
  121.              } 
  122.               
  123.              - (void)animation { 
  124.                 [UIView transitionFromView:self.firstView toView:self.secondView duration:2.f options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionShowHideTransitionViews completion:^(BOOL finished) {}]; 
  125.              } 
  126.               
  127.              */  
  128.             type = UIViewAnimationOptionShowHideTransitionViews;  
  129.             break;  
  130.         }  
  131.             // ========= 动画过渡动作的速度  
  132.         case 9: {// 先慢后快再慢  
  133.             type = UIViewAnimationOptionCurveEaseInOut;  
  134.             break;  
  135.         }  
  136.         case 10: {// 先慢后快  
  137.             type = UIViewAnimationOptionCurveEaseIn;  
  138.             break;  
  139.         }  
  140.         case 11: {// 先快后慢  
  141.             type = UIViewAnimationOptionCurveEaseOut;  
  142.             break;  
  143.         }  
  144.         case 12: {// 匀速  
  145.             type = UIViewAnimationOptionCurveLinear;  
  146.             break;  
  147.         }  
  148.             // ========= 动画过渡过程的方式:  
  149.         case 13: {// 不指定方式  
  150.             type = UIViewAnimationOptionTransitionNone;  
  151.             break;  
  152.         }  
  153.         case 14: {// 翻转  
  154.             type = UIViewAnimationOptionTransitionFlipFromLeft;  
  155.             break;  
  156.         }  
  157.         case 15: {// 翻转  
  158.             type = UIViewAnimationOptionTransitionFlipFromRight;  
  159.             break;  
  160.         }  
  161.         case 16: {// 翻转  
  162.             type = UIViewAnimationOptionTransitionFlipFromTop; // 5.0以后  
  163.             break;  
  164.         }  
  165.         case 17: {// 翻转  
  166.             type = UIViewAnimationOptionTransitionFlipFromBottom;// 5.0以后  
  167.             break;  
  168.         }  
  169.         case 18: {// 重叠,当一个view从一个位置到另一个位置时,当前的view会由透明状态逐渐显示到目的位置,原来的位置将会保留一个影子,并逐渐消失  
  170.             type = UIViewAnimationOptionTransitionCrossDissolve;// 5.0以后  
  171.             break;  
  172.         }  
  173.         case 19: {// 翻页  
  174.             type = UIViewAnimationOptionTransitionCurlUp;  
  175.             break;  
  176.         }  
  177.         case 20: {// 翻页  
  178.             type = UIViewAnimationOptionTransitionCurlDown;  
  179.             break;  
  180.         }  
  181.         default:  
  182.             break;  
  183.     }  
  184.       
  185.     return type;  
  186. }  


   原文地址: http://blog.csdn.net/namehzf/article/details/7650416


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值