[UIView animateWithDuration:animations:] 与[UIView beginAnimations:context]

原文链接:http://longtimenoc.com/archives/uiview-beginanimationscontext%E4%B8%8Euiview-animatewithdurationanimations%E5%80%BC%E5%BE%97%E6%B3%A8%E6%84%8F%E7%9A%84%E4%B8%80%E4%B8%AA%E5%8C%BA%E5%88%AB

看过官方文档的都知道,官方推荐在iOS4以后使用[UIView animateWithDuration:animations:],而不是原来的[UIView beginAnimations:context:],来完成动画,虽然二者功能几乎完全相同,但使用前者在一些情况下会方便不少,这些内容可以参考官方文档View Programming Guide For iOS的Animation一节.

二者有一个值得新手注意的区别就是[UIView animateWithDuration:animations:]默认会禁止触摸,手势等的响应,这可以通过设置option选项来解决(直接引用StackOverFlow的一段了):
  1. UIViewAnimationOptions options = UIViewAnimationCurveLinear | UIViewAnimationOptionAllowUserInteraction;  
  2.    
  3. [UIView animateWithDuration:0.2 delay:0.0 options:options animations:^  
  4.  {  
  5.      highlightView.alpha = 1.0;  
  6.    
  7.  } completion:nil];  

就是这么一点事儿,害我走了不少弯路(我也是新手哈),在这里写一下,提示一下有可能遇到同样问题的人.


  1. [UIView animateWithDuration:duration  
  2.                           delay:0.0  
  3.                         options:UIViewAnimationCurveEaseInOut //设置动画类型  
  4.                      animations:^{  
  5.                          //开始动画  
  6.                          [self updateArrowBtnTitle:YES];  
  7.                          rotateView.transform = CGAffineTransformMakeRotation((stickToDegrees/180)*M_PI);  
  8.                      }  
  9.                      completion:^(BOOL finished){  
  10.                          // 动画结束时的处理  
  11.                      }];  


[UIView animateWithDuration:] 方法仅支持ios4.0及以上版本。如果要兼容以前的版本的话,还是需要使用 [UIView beginAnimation:] 方法

  1. [UIView beginAnimations:nil context:nil];  
  2.     // fade out  
  3.     helpImageBtn.alpha = 0.0f;  
  4.     // set animation did stop selector  
  5.     [UIView setAnimationDelegate:self];  
  6.     [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];  
  7.     [UIView commitAnimations];  
  8.   
  9.    - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {  
  10.     if (self.retainedHelpImageBtn.superview) //先判断父视图再执行视图移除  
  11.         [self.retainedHelpImageBtn removeFromSuperview];  
  12.    } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值