子类继承父类实现父类的代理方法

简介:

子类继承父类实现父类的代理方法

父类提供虚函数(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

有以下几点需要注意:

目录
相关文章
|
8月前
|
C#
C# 继承类中(父类与子类)构造函数的调用顺序
C# 继承类中(父类与子类)构造函数的调用顺序
|
6月前
|
SQL 数据库
父类坑
父类坑
28 0
|
10月前
|
程序员
为什么子类会调用父类无参的构造函数
为什么子类会调用父类无参的构造函数
|
11月前
类的继承和重写方法
类的继承和重写方法
52 0
|
开发者 Python
子类重写父类方法|学习笔记
快速学习子类重写父类方法,先来介绍子类重写父类方法。多态其实就是一种子类重写父类的方法。
150 0
关于纯虚函数继承子类 ,父类指向子类后 将父类指针转为子指针,就可以调用子类其他接口
关于纯虚函数继承子类 ,父类指向子类后 将父类指针转为子指针,就可以调用子类其他接口
关于纯虚函数继承子类 ,父类指向子类后 将父类指针转为子指针,就可以调用子类其他接口
子类到底能不能继承父类的私有属性?
继承就像是我们现实生活中的父子关系,儿子可以遗传父亲的一些特性,在面向对象语言中,就是一个类可以继承另一个类的一些特性,从而可以代码重用,其实继承体现的是is-a关系,父类同子类在本质上还是一类实体;子类通过继承父类的属性的行为,我们称之为继承。Java只支持单继承,不支持多继承。因为多继承容易带来安全隐患:当多个父类定义相同的功能,当功能内容不同的时候,子类对象不确定要运行哪一个,在Java中用另一种形式体现出来,就是接口的多实现。
ES6—45:子类对父类方法的重写
ES6—45:子类对父类方法的重写
145 0
ES6—45:子类对父类方法的重写
|
Python
类的继承_子类继承父类
[root@blackfox zhouyuyao]# cat c5.py  #!/usr/bin/python #coding:utf-8 class People(object):     color = 'yellow'     def __init__(self,c):         print "Init.
838 0
|
Python
8.4 类的重写
子类除了继承父类的所有属性和方法,还可以自定义自己的属性和方法,增加了代码的复用性 class parent(object):     name='parent'     sex='F'     def __init__(self):         print('my name is {0}'.
628 0