当前位置: 首页 > news >正文

用POP动画编写带富文本的自定义动画效果

用POP动画编写带富文本的自定义动画效果

【源码】

https://github.com/YouXianMing/UI-Component-Collection

 

【效果】

 

【特点】

* 支持富文本

* 可定制型强(继承父类重写父类的startAnimation方法即可)

* 支持动画的中断与持续

* 支持CAMediaTimingFunction

* 数据与UI隔离,便于你封装属于你的类

 

【核心】

//
//  POPNumberCount.h
//  POP
//
//  Created by XianMingYou on 15/4/10.
//  Copyright (c) 2015年 XianMingYou. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "POP.h"

@class POPNumberCount;

@protocol POPNumberCountDelegate <NSObject>
@optional
/**
 *  最原始的方法
 *
 *  @param numberCount 对象自己
 *  @param number      变化的值
 */
- (void)numberCount:(POPNumberCount *)numberCount currentNumber:(NSNumber *)number;

/**
 *  子类可以实现的方法
 *
 *  @param numberCount 对象自己
 *  @param string      子类返回的富文本
 */
- (void)numberCount:(POPNumberCount *)numberCount currentAttributedString:(NSAttributedString *)string;

/**
 *  子类可以实现的方法
 *
 *  @param numberCount 对象自己
 *  @param string      子类返回的文本
 */
- (void)numberCount:(POPNumberCount *)numberCount currentString:(NSString *)string;

@end


@interface POPNumberCount : NSObject

@property (nonatomic, weak)   id<POPNumberCountDelegate> delegate;       // 代理

@property (nonatomic, assign) CGFloat                    fromValue;      // 开始值
@property (nonatomic, assign) CGFloat                    toValue;        // 结束值
@property (nonatomic, assign) CGFloat                    currentValue;   // 当前值
@property (nonatomic, assign) NSTimeInterval             duration;       // 持续时间

@property (nonatomic, strong) CAMediaTimingFunction     *timingFunction; // 时间函数
@property (nonatomic, strong) POPBasicAnimation         *conutAnimation;

/*
 *  存储值(执行动画之前,请先将设置的值进行存储)
 */
- (void)saveValues;

/*
 *  开始动画(继承的子类需要重写本方法来,然后与代理一起配合使用)
 */
- (void)startAnimation;

/*
 *  停止动画
 */
- (void)stopAnimation;

@end


//
//  POPNumberCount.m
//  POP
//
//  Created by XianMingYou on 15/4/10.
//  Copyright (c) 2015年 XianMingYou. All rights reserved.
//

#import "POPNumberCount.h"
#import "ConfigAttributedString.h"

@implementation POPNumberCount

- (instancetype)init {
    self = [super init];
    if (self) {
        self.conutAnimation = [POPBasicAnimation animation];
    }
    return self;
}

- (void)saveValues {
    self.conutAnimation.fromValue = @(self.fromValue);
    self.conutAnimation.toValue   = @(self.toValue);
    self.conutAnimation.duration  = (self.duration <= 0 ? 0.4f : self.duration);
    if (self.timingFunction) {
        self.conutAnimation.timingFunction = self.timingFunction;
    }
}

- (void)startAnimation {
    // 只有执行了代理才会执行计数引擎
    if (self.delegate && [self.delegate respondsToSelector:@selector(numberCount:currentNumber:)]) {
        /* 将计算出来的值通过writeBlock动态给控件设定 */
        self.conutAnimation.property = \
        [POPMutableAnimatableProperty propertyWithName:@"conutAnimation"
                                           initializer:^(POPMutableAnimatableProperty *prop) {
                                               prop.writeBlock      = ^(id obj, const CGFloat values[]) {
                                                   NSNumber *number = @(values[0]);
                                                   _currentValue    = values[0];
                                                   
                                                   /* ------------- 子类重写父类的这个方法 ------------- */
                                                   [_delegate numberCount:self currentNumber:number];
                                                   /* ---------------------------------------------- */
                                               };
                                           }];
        
        // 添加动画
        [self pop_addAnimation:self.conutAnimation forKey:nil];
    }
}

- (void)stopAnimation {
    [self pop_removeAllAnimations];
}

@end

【说明】

父类  POPNumberCount 的设计类似于设计模式中的策略模式,提供接口让子类重写来实现不一样的功能,父类基本不会修改,具体到某个子类,只需要修改子类中的特定实现即可。(父类中的协议类似于一个个策略,具体由子类来实现)

相关文章:

  • 〖Linux〗不知谁写的,很实用的Linux命令
  • 带动画渐进效果与颜色渐变的圆弧进度控件设计
  • [Android]Tool-Systrace
  • 抱歉,我不接私单了
  • Java中数据库连接池原理机制的详细讲解(转)
  • App安全之网络传输安全
  • 记录:C#编程的一点小细节
  • 8000端口下 调用pdo数据库连接的报错原因
  • MYSQL导入导出.sql文件
  • hadoop
  • 每日编译的入门实践
  • Managing Versions of an Application
  • 矩形的个数
  • 系统服务之广告
  • cvsacl error
  • 【159天】尚学堂高琪Java300集视频精华笔记(128)
  • angular组件开发
  • Essential Studio for ASP.NET Web Forms 2017 v2,新增自定义树形网格工具栏
  • EventListener原理
  • gops —— Go 程序诊断分析工具
  • gulp 教程
  • js正则,这点儿就够用了
  • Node项目之评分系统(二)- 数据库设计
  • PHP 小技巧
  • Redis 懒删除(lazy free)简史
  • Unix命令
  • vagrant 添加本地 box 安装 laravel homestead
  • vue数据传递--我有特殊的实现技巧
  • Vue源码解析(二)Vue的双向绑定讲解及实现
  • 初识MongoDB分片
  • 解析带emoji和链接的聊天系统消息
  • 紧急通知:《观止-微软》请在经管柜购买!
  • 那些被忽略的 JavaScript 数组方法细节
  • 云栖大讲堂Java基础入门(三)- 阿里巴巴Java开发手册介绍
  • Prometheus VS InfluxDB
  • 关于Android全面屏虚拟导航栏的适配总结
  • 进程与线程(三)——进程/线程间通信
  • ​一帧图像的Android之旅 :应用的首个绘制请求
  • #!/usr/bin/python与#!/usr/bin/env python的区别
  • #Lua:Lua调用C++生成的DLL库
  • (1)虚拟机的安装与使用,linux系统安装
  • (免费领源码)python#django#mysql公交线路查询系统85021- 计算机毕业设计项目选题推荐
  • (转)Oracle 9i 数据库设计指引全集(1)
  • (轉貼) 寄發紅帖基本原則(教育部禮儀司頒布) (雜項)
  • .NET “底层”异步编程模式——异步编程模型(Asynchronous Programming Model,APM)...
  • .NET Core 成都线下面基会拉开序幕
  • .NET Core 控制台程序读 appsettings.json 、注依赖、配日志、设 IOptions
  • .NET MVC之AOP
  • .Net 中的反射(动态创建类型实例) - Part.4(转自http://www.tracefact.net/CLR-and-Framework/Reflection-Part4.aspx)...
  • .NET牛人应该知道些什么(2):中级.NET开发人员
  • .net下简单快捷的数值高低位切换
  • .NET中两种OCR方式对比
  • [ SNOI 2013 ] Quare
  • [ vulhub漏洞复现篇 ] Apache Flink目录遍历(CVE-2020-17519)
  • [ vulhub漏洞复现篇 ] Celery <4.0 Redis未授权访问+Pickle反序列化利用