[UIView beginAnimations:context:]与[UIView animateWithDuration:animations:]值得注意的一个区别...

原文链接: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选项来解决(直接引用*的一段了):

  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.    }  

[UIView beginAnimations:context:]与[UIView animateWithDuration:animations:]值得注意的一个区别...






本文转自夏雪冬日博客园博客,原文链接:http://www.cnblogs.com/heyonggang/p/3494468.html,如需转载请自行联系原作者