zoukankan      html  css  js  c++  java
  • POP动画[3]

    POP动画[3]

    这一节主要讲解POP动画的自定义动画属性.

    POP动画中有一个参数,叫timingFunction,与CoreAnimation中的一个参数CAMediaTimingFunction基本一样,下图表示的是kCAMediaTimingFunctionEaseInEaseOut的曲线图.

    下图是Spring动画效果:

    我们可以使用自定义的属性来实现POP的库中没有提供的动画.

    实现的效果:

    源码:

    //
    //  RootViewController.m
    //  YXPOP
    //
    //  Copyright (c) 2014年 Y.X. All rights reserved.
    //
    
    #import "RootViewController.h"
    #import "POP.h"
    
    @interface RootViewController ()
    
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.view.backgroundColor   = [UIColor blackColor];
        
        // 数值型label
        UILabel *numberLabel        = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
        numberLabel.center                 = self.view.center;
        numberLabel.userInteractionEnabled = YES;
        numberLabel.textAlignment          = NSTextAlignmentCenter;
        numberLabel.textColor       = [UIColor redColor];
        numberLabel.text            = @"0";
        numberLabel.font            = [UIFont fontWithName:@"HelveticaNeue-UltraLight"
                                                      size:50.f];
        [self.view addSubview:numberLabel];
        
        // 添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                              action:@selector(tap:)];
        [numberLabel addGestureRecognizer:tap];
    
    }
    
    - (void)tap:(UITapGestureRecognizer *)tap
    {
        UILabel *tmp                  = (UILabel *)tap.view;
        POPBasicAnimation *animation  = [POPBasicAnimation animation];
        animation.fromValue           = @([tmp.text intValue]);
        animation.toValue             = @(arc4random()%10000 + 2000);
        animation.duration            = 1.f;
        
        // 计算从fromValue到toValue插值的曲线
        animation.timingFunction      = 
            [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
        // 将计算出来的值通过writeBlock动态给控件设定
        animation.property            = 
            [POPMutableAnimatableProperty propertyWithName:@"textLabel" initializer:^(POPMutableAnimatableProperty *prop) {
             prop.writeBlock      = ^(id obj, const CGFloat values[]) {
                 UILabel *label   = (UILabel *)obj;
                 NSNumber *number = @(values[0]);
                 int num          = [number intValue];
                 label.text       = [@(num) stringValue];
             };
         }];
        
        
        [tmp pop_addAnimation:animation forKey:@"numberLabelAnimation"];
    }
    
    @end

    他们彼此间凌乱的关系如下所示:

    duration代表x轴(时间轴)

    fromValue与toValue代表y轴的最小值与最大值

    timingFunction代表时间曲线(EaseOut曲线)

    曲线中的每一个小点代表的是根据上述各个值计算出来的一个中间值,而这个中间值就是我们用来做动画而用的动画设定值.

    以下网址是介绍如何设定CAMediaTimingFunction的(http://netcetera.org/camtf-playground.html).

  • 相关阅读:
    利用ajax.dll类库文件实现无刷新
    给input的按钮控件添加onserverclick事件
    wpf datagrid 如何让标头 及内容居中
    MVC中Url请求与控制器的默认约定
    ASP.NET MVC中实现多个按钮提交的几种方法
    default(T)的含义
    MVC中Html.Listbox的用法实例
    编写高质量代码改善C#程序的157个建议——建议101:使用扩展方法,向现有类型“添加”方法
    编写高质量代码改善C#程序的157个建议——建议100:静态方法和实例方法没有区别
    编写高质量代码改善C#程序的157个建议——建议99:重写时不应使用子类参数
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3830862.html
Copyright © 2011-2022 走看看