• 转一篇简洁的UIView动画编程方法


    iOS  中的 UIView 动画编程其实还是很简单的,像 CSS3 一样,在给定的时间内完成状态连续性的变化呈现。比如背景色,Frame 大小,位移、翻转,特明度等。

    以前我使用的编程方式都是用下面那样的三段式处理:

    1
    2
    3
    4
    5
    6
    7
    8
        [UIView beginAnimations:nil context: nil];
        [UIView setAnimationDuration:1.0];
         
        //要动画改变的属性
        self.view.alpha = 0.0;
        self.view.frame = CGRectMake(10, 10, 50, 50);
         
        [UIView commitAnimations];

    那么被 beginAnimations 和  commitAnimations 框起来的代码就会让你产生动画效果,这种方式像是数据库中的事物编程一样。

    还有另一种编程方式,使用代码块,对于常常书写 JS 代码的同志会比较适应,还是来个简单的代码片段:

    1
    2
    3
    4
    5
        [UIView animateWithDuration:1.0
                         animations:^{
                             self.view.alpha = 0.0;
                             self.view.frame = CGRectMake(10, 10, 50, 50);
                         }];

    对于不太复杂的动画,上面的写法很精练,因为只有一条语句,如果动画太过复杂了,写在这样一条语句中就会显得冗长了,对于代码调试没那么方便。

    animateWithDuration 有三个重载方法:

    比如我们用最后面那个重载方法,可以比 beginAnimations...commitAnimations 更轻松的实现动画完后执行的动作,如:

    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
        [UIView animateWithDuration:1.0
                              delay: 0.0
                            options: UIViewAnimationOptionCurveEaseIn
                         animations:^{
                             self.view.alpha = 0.0;
                             self.view.frame = CGRectMake(10, 10, 50, 50);
                         }
                         completion:^(BOOL finished){
                             NSLog(@"Do something after the animation.");
                         }];

    再回头看看 beginAnimations...commitAnimations 该如何实现上面同样的行为:

    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
        [UIView beginAnimations:nil context: nil];
        [UIView setAnimationDuration:1.0];
         
        [UIView setAnimationDelay:0.0];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
         
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(animationStopped)];
         
        self.view.alpha = 0.0;
        self.view.frame = CGRectMake(10, 10, 50, 50);
         
        [UIView commitAnimations];

    还要给当前类加一个方法 animationStopped:

    1
    2
    3
    -(void) animationStopped {
        NSLog(@"Do something after the animation.");
    }

    代码是多些,但是 beginAnimations...commitAnimations 编程方式是全能的,而 animateWithDuration 是有局限性的,因为它的目的就是让编写代码更简洁。在 animateWithDuration 中只提供了 completion 代码块,意即在动画完成后执行的动作,而要达成

    [UIView setAnimationWillStartSelector:@selector(animationWillStart)]

    这样的在动画即将启动之前的动作却是无能为力,还有些动画设置也是 animateWithDuration 做不到。所以一旦你先前用 animateWithDuration  实现的动画方式要增加稍复杂的功能而不得不用 beginAnimations...commitAnimations 来改写时,付出就大了。

    该用哪种方式,只能自己斟酌吧。

    关于 UIView 的动画请参考官方文档:Animations

  • 相关阅读:
    spring 事物(一)—— 事物详解
    XMPP即时通讯协议使用(十一)——Openfire表结构汇总
    java 关键字汇总
    基于数据库的分布式锁实现
    hibernate 参数一览
    数据库并发及锁机制及Hibernate锁实现
    redis 分布式锁的正确实现方式
    支持跨域的html元素
    九度OJ 上剑指 offer 习题目录
    Leetcode: Palindrome Partition I II
  • 原文地址:https://www.cnblogs.com/lxd2502/p/5706327.html
Copyright © 2020-2023  润新知