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

本文对比了iOS中UIView.animateWithDuration和UIView.beginAnimations两种动画实现方式,详细介绍了它们的区别及如何通过设置option选项解决触摸响应问题,并提供了实际应用案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >




看过官方文档的都知道,官方推荐在iOS4以后使用[UIView animateWithDuration:animations:],而不是原来的[UIView beginAnimations:context:],来完成动画,虽然二者功能几乎完全相同,但使用前者在一些情况下会方便不少,这些内容可以参考官方文档View Programming Guide For iOS的Animation一节.
二者有一个值得新手注意的区别就是[UIView animateWithDuration:animations:]默认会禁止触摸,手势等的响应,这可以通过设置option选项来解决(直接引用StackOverFlow的一段了):

UIViewAnimationOptions options = UIViewAnimationCurveLinear | UIViewAnimationOptionAllowUserInteraction;
[UIView animateWithDuration:0.2 delay:0.0 options:options animations:^
 {
     highlightView.alpha = 1.0;
 
 } completion:nil];

[UIView animateWithDuration:duration
                          delay:0.0
                        options:UIViewAnimationCurveEaseInOut //设置动画类型
                     animations:^{
                         //开始动画
                         [self updateArrowBtnTitle:YES];
                         rotateView.transform = CGAffineTransformMakeRotation((stickToDegrees/180)*M_PI);
                     }
                     completion:^(BOOL finished){
                         // 动画结束时的处理
                     }];
[UIView animateWithDuration:] 方法仅支持ios4.0及以上版本。如果要兼容以前的版本的话,还是需要使用 [UIView beginAnimation:] 方法

[UIView beginAnimations:nil context:nil];
    // fade out
    helpImageBtn.alpha = 0.0f;
    // set animation did stop selector
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView commitAnimations];
   - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    if (self.retainedHelpImageBtn.superview) //先判断父视图再执行视图移除
        [self.retainedHelpImageBtn removeFromSuperview];
   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值