用POP动画编写带富文本的自定义动画效果
2015-04-10 20:48
288 查看
用POP动画编写带富文本的自定义动画效果
【源码】
https://github.com/YouXianMing/UI-Component-Collection
【效果】
【特点】
* 支持富文本
* 可定制型强(继承父类重写父类的startAnimation方法即可)
* 支持动画的中断与持续
* 支持CAMediaTimingFunction
* 数据与UI隔离,便于你封装属于你的类
【核心】
【说明】
父类 POPNumberCount 的设计类似于设计模式中的策略模式,提供接口让子类重写来实现不一样的功能,父类基本不会修改,具体到某个子类,只需要修改子类中的特定实现即可。(父类中的协议类似于一个个策略,具体由子类来实现)
【源码】
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 的设计类似于设计模式中的策略模式,提供接口让子类重写来实现不一样的功能,父类基本不会修改,具体到某个子类,只需要修改子类中的特定实现即可。(父类中的协议类似于一个个策略,具体由子类来实现)
相关文章推荐
- 【iOS学习笔记 15-11-06】简单自定义navigationcontroller push和pop动画效果
- html代码编写:自定义hr标签的显示效果
- jQuery文本动画效果
- 实现自定义对话框的动画效果:
- 在Android动画基础上实现自定义的动画效果
- 编写一个项目,实现页面之间切换时的动画效果。上传代码并描述步骤
- 自定义动画效果(Iris动画修改)
- android学习笔记---56_activity切换动画与页面切换动画,自定义activity窗口切换动画效果的实现.
- android 自定义动画效果 一
- Extjs动画效果(自定义动画)
- 各种自定义动画效果
- jQuery文本动画效果
- 【转载】 iphone 半透明的自定义tabbar,带滑块动画效果
- Flex4之自定义动画效果,使用自定义Parallel类来实现
- iOS学习之自定义弹出UIPickerView或UIDatePicker(动画效果)
- jQuery实现图片与文本信息切换动画效果
- 如何编写Silverlight动画效果控件
- 在iframe里的页面编写js,实现在父窗口上创建动画效果展开和收缩的div(不变动iframe父窗口代码)
- 更改pushViewController和popViewController的动画效果
- setAnimationStyle实现的popwindow显示消失的动画效果