标签:
子类继承父类实现父类的代理方法
父类提供虚函数(OC中称为代理方法),子类继承后实现虚函数来实现特定的功能.
父类方法:
NumberCount.h 与 NumberCount.m
// // NumberCount.h // YXMWeather // // Created by XianMingYou on 15/2/18. // Copyright (c) 2015年 XianMingYou. All rights reserved. // #import <Foundation/Foundation.h> @class NumberCount; @protocol NumberCountDelegate <NSObject> @optional /** * 最原始的方法 * * @param numberCount 对象自己 * @param number 变化的值 */ - (void)numberCount:(NumberCount *)numberCount currentNumber:(NSNumber *)number; /** * 子类可以实现的方法 * * @param numberCount 对象自己 * @param string 子类返回的富文本 */ - (void)numberCount:(NumberCount *)numberCount currentSting:(NSAttributedString *)string; @end @interface NumberCount : NSObject /** * 代理 */ @property (nonatomic, weak) id<NumberCountDelegate> delegate; /** * 动画实体 */ @property (nonatomic, strong) POPBasicAnimation *conutAnimation; /** * 初始值 */ @property (nonatomic) CGFloat fromValue; /** * 结束值 */ @property (nonatomic) CGFloat toValue; /** * 动画持续时间 */ @property (nonatomic) CGFloat duration; /** * 开始动画 */ - (void)startAnimation; @end
// // NumberCount.m // YXMWeather // // Created by XianMingYou on 15/2/18. // Copyright (c) 2015年 XianMingYou. All rights reserved. // #import "NumberCount.h" #import "POP.h" @implementation NumberCount - (instancetype)init { self = [super init]; if (self) { self.conutAnimation = [POPBasicAnimation animation]; } return self; } - (void)startAnimation { // 初始化值 CGFloat fromeValue = self.fromValue; CGFloat toValue = self.toValue; CGFloat duration = (self.duration <= 0 ? 1.f : self.duration); // 设定动画 self.conutAnimation.fromValue = @(fromeValue); self.conutAnimation.toValue = @(toValue); self.conutAnimation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.69 :0.11 :0.32 :0.88]; self.conutAnimation.duration = duration; // 只有执行了代理才会执行计数引擎 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]); [_delegate numberCount:self currentNumber:number]; }; }]; // 添加动画 [self pop_addAnimation:self.conutAnimation forKey:nil]; } } @end
子类方法:
// // HumidityCount.h // YXMWeather // // Created by XianMingYou on 15/2/18. // Copyright (c) 2015年 XianMingYou. All rights reserved. // #import "NumberCount.h" @interface HumidityCount : NumberCount - (void)startAnimation; @end
// // HumidityCount.m // YXMWeather // // Created by XianMingYou on 15/2/18. // Copyright (c) 2015年 XianMingYou. All rights reserved. // #import "HumidityCount.h" #import "NSString+RichText.h" @implementation HumidityCount - (void)startAnimation { // 初始化值 CGFloat fromeValue = self.fromValue; CGFloat toValue = self.toValue; CGFloat duration = (self.duration <= 0 ? 1.f : self.duration); // 设定动画 self.conutAnimation.fromValue = @(fromeValue); self.conutAnimation.toValue = @(toValue); self.conutAnimation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.69 :0.11 :0.32 :0.88]; self.conutAnimation.duration = duration; // 只有执行了代理才会执行计数引擎 if (self.delegate && [self.delegate respondsToSelector:@selector(numberCount:currentSting:)]) { /* 将计算出来的值通过writeBlock动态给控件设定 */ self.conutAnimation.property = [POPMutableAnimatableProperty propertyWithName:@"conutAnimation" initializer:^(POPMutableAnimatableProperty *prop) { prop.writeBlock = ^(id obj, const CGFloat values[]) { NSNumber *number = @(values[0]); NSAttributedString *ats = [self accessNumber:number]; [self.delegate numberCount:self currentSting:ats]; }; }]; // 添加动画 [self pop_addAnimation:self.conutAnimation forKey:nil]; } } // 处理富文本 - (NSAttributedString *)accessNumber:(NSNumber *)number { NSInteger count = [number integerValue]; NSString *countStr = [NSString stringWithFormat:@"%02ld", (long)count]; NSString *totalStr = [NSString stringWithFormat:@"%@%%", countStr]; UIFont *font1 = [UIFont fontWithName:LATO_REGULAR size:40.f]; UIFont *font2 = [UIFont fontWithName:LATO_BOLD size:9.f]; NSRange totalRange = [totalStr range]; // 全局的区域 NSRange countRange = [countStr rangeFrom:totalStr]; // %的区域 return [totalStr createAttributedStringAndConfig:@[ // 全局设置 [ConfigAttributedString font:font2 range:totalRange], [ConfigAttributedString font:font1 range:countRange], // 局部设置 [ConfigAttributedString foregroundColor:[UIColor redColor] range:countRange], ]]; } @end
有以下几点需要注意:
标签:
原文地址:http://www.cnblogs.com/YouXianMing/p/4296222.html