【iOS】CountDownView的封裝

CountDownView的封裝

今天在整理以前的代碼的時(shí)候,把以前的一個(gè)控件再次封裝了一次若治,這是一個(gè)倒計(jì)時(shí)的控件,可以用來作為一個(gè)進(jìn)度條來使用。

CountDownView的四個(gè)基本類型以及一個(gè)自定義類型:
CountDownViewTypeCircle,
CountDownViewTypeRect,
CountDownViewTypeLine,
CountDownViewTypePercent

使用的時(shí)候往弓,也是很方便的!
開源代碼 - Github

使用的時(shí)候的代碼

 CountDownView * Circle =[CountDownView countDownViewWithFrame:CGRectMake(10, 40, 140, 140) type:CountDownViewTypeCircle andFinishblock:^(UIView *countDownView, CADisplayLink *link) {
        NSLog(@"%s",__func__);
    }];
    [Circle showInKeyWindowTop];

    CountDownView * Rect =[CountDownView countDownViewWithFrame:CGRectMake(160, 40, 140, 140) type:CountDownViewTypeRect andFinishblock:^(UIView *countDownView, CADisplayLink *link) {
        NSLog(@"%s",__func__);
    }];
    [self.view addSubview:Rect];

    CountDownView * downV =[CountDownView countDownViewWithFrame:CGRectMake(20, 200, 280, 80) type:CountDownViewTypeLine andFinishblock:^(UIView *countDownView, CADisplayLink *link) {
        NSLog(@"%s",__func__);
    }];
    [downV showInKeyWindowTop];

    //CountDownViewTypePercent
    CountDownView * downV1 =[CountDownView countDownViewWithFrame:CGRectMake(20, 300, 280, 40) type:CountDownViewTypePercent andFinishblock:^(UIView *countDownView, CADisplayLink *link) {
        NSLog(@"%s",__func__);
    }];
    [self.view addSubview:downV1];
    downV1.lineWidth = 30;

源碼

//
//  CountDownView.h
//  倒計(jì)時(shí)
//
//  Created by zzz on 15/10/9.
//  Copyright (c) 2015年 zzz. All rights reserved.
//

#import <UIKit/UIKit.h>


typedef void(^FinishCountDownBlock)(UIView *countDownView,CADisplayLink *link);


typedef enum : NSUInteger {
    CountDownViewTypeCircle,
    CountDownViewTypeRect,
    CountDownViewTypeLine,
    CountDownViewTypePercent
} CountDownViewType;

@interface CountDownView : UIView

/**
 *  倒計(jì)時(shí)最大時(shí)間
 */
@property(nonatomic, assign) CGFloat count;                //默認(rèn)為: 10.00s

/**
 *  當(dāng)前時(shí)間點(diǎn)(可以用作進(jìn)度條)
 */
@property(nonatomic, assign) CGFloat currentCount;


@property(nonatomic, strong) UIColor * lineColor;           //默認(rèn)為: Red:0.4 green:0 blue:0.8 alpha:0.6
@property(nonatomic, strong) UIColor * textColor;           //默認(rèn)為: Red:0 green:0.4 blue:0.8 alpha:0.8
@property(nonatomic, strong) UIFont * textFont;             //默認(rèn)為: 高度的一半

@property(nonatomic, assign) CGFloat lineWidth;             //默認(rèn)為: 10.0

@property(nonatomic, assign) BOOL isTouchBegin;             //默認(rèn)為: YES
@property(nonatomic, assign) BOOL isTouchEnd;               //默認(rèn)為: YES
@property(nonatomic, assign) BOOL hiddenText;               //默認(rèn)為: NO

@property(nonatomic, assign) CountDownViewType type;        // 默認(rèn)為:CountDownViewTypeCircle
@property(nonatomic ,copy) FinishCountDownBlock finishblock;


+ (CountDownView *) countDownViewWithFrame:(CGRect)frame type:(CountDownViewType)type andFinishblock:(FinishCountDownBlock)block;


- (void)setFinishblock:(FinishCountDownBlock)finishblock;

- (void)beginCountDown;

/**
 *  此方法必須要有keyWindow蓄氧,才有用
 */
- (void)showInKeyWindowTop;

@end


//
//  CountDownView.m
//  倒計(jì)時(shí)
//
//  Created by zzz on 15/10/9.
//  Copyright (c) 2015年 zzz. All rights reserved.
//

#import "CountDownView.h"


@interface CountDownView ()


@property (nonatomic, strong) CADisplayLink * link;
@property (nonatomic, strong) UILabel * label;

@property(nonatomic, strong) UITapGestureRecognizer * tap;
@property(nonatomic, strong) UITapGestureRecognizer * doubelTap;


@end

@implementation CountDownView


+ (CountDownView *)countDownViewWithFrame:(CGRect)frame type:(CountDownViewType)type andFinishblock:(FinishCountDownBlock)block{
    CountDownView * downV = [[CountDownView alloc] initWithFrame:frame];
    downV.type = type;
    downV.finishblock = block;
    return downV;
}
- (void)beginCountDown{
    [self.link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)showInKeyWindowTop{
    
    if ([UIApplication sharedApplication].keyWindow) {
        [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:self];
    }else{
        NSLog(@"keyWindow = %@",[UIApplication sharedApplication].keyWindow);
    }
    
}




#pragma mark - 復(fù)寫方法
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor colorWithRed:0.1 green:0.2 blue:0.6 alpha:0.4];
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(beginAndEndLink)];
        [self addGestureRecognizer:tap];
        
        self.lineWidth = 10;
        self.count = 10;
        self.lineColor = [UIColor colorWithRed:0 green:0.4 blue:0.8 alpha:0.8];
        self.isTouchBegin = YES;
        self.isTouchEnd = YES;
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.label.frame = self.bounds;
}





// 開始畫圖
- (void)drawRect:(CGRect)rect{
    
    
    if (self.type == CountDownViewTypePercent) {
        self.label.text = [NSString stringWithFormat:@"%0.2f%",(1.0 - (_currentCount / self.count)) * 100];
    }else{
        
        int index =  (self.count == _currentCount)?(int)_currentCount:(int)_currentCount +1;
        index = _currentCount <= 0 ? 0 : index;
        self.label.text = [NSString stringWithFormat:@"%d",index];
        
    }
    
    if (self.type == CountDownViewTypeLine || self.type == CountDownViewTypePercent) {
        
        // 1.開啟上下文
        CGContextRef context = UIGraphicsGetCurrentContext();

        
        CGFloat Width = CGRectGetWidth(self.bounds);
        CGFloat height = CGRectGetHeight(self.bounds);
        
        CGFloat Now = Width * (_currentCount / self.count);
        
        CGContextAddRect(context, CGRectMake(0, height - self.lineWidth -5,Width - Now, self.lineWidth));
        
        CGContextSetLineWidth(context, self.lineWidth);
        [(self.lineColor)?self.lineColor:[UIColor blackColor] set];
        
        CGContextSetLineCap(context, kCGLineCapRound);
        
        CGContextFillPath(context);
        
        return;
    }
    // 1.開啟上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGFloat centerX = CGRectGetMidX(self.bounds);
    CGFloat centerY = CGRectGetMidY(self.bounds);
    
    CGFloat radius = (CGRectGetWidth(self.bounds) / 2.0) - 10;
    
    CGFloat startAngle = M_PI * 1.5;
    
    CGFloat endAngle = startAngle + (M_PI * 2.0) * (_currentCount / self.count);
    
    // 0 表示順時(shí)針
    CGContextAddArc(context, centerX, centerY, radius, startAngle, endAngle, 0);
    
    CGContextSetLineWidth(context, self.lineWidth);
    [(self.lineColor)?self.lineColor:[UIColor blackColor] setStroke];

    CGContextSetLineCap(context, kCGLineCapRound);
    
    CGContextStrokePath(context);
}





#pragma mark - 私有方法
- (void)beginAndEndLink{
    if (_link && self.isTouchEnd) {
        
        [_link invalidate];
        _link = nil;
        
        return;
    
    }else if (_currentCount <= 0 || (!self.isTouchBegin)) {
        
        return;
    }
    
    [self.link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}




// 每秒調(diào)用60次
- (void) linkLoop{
    
    [self setNeedsDisplay];
    
    if (_currentCount <= 0) {
        [_link invalidate];
        
        if (self.finishblock) {
            self.finishblock(self,_link);
        }
        return;
    }
    
    _currentCount -= 1 / 60.000000;
}







- (CADisplayLink *)link{
    if (!_link) {
        _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(linkLoop)];
        
    }
    return _link;
}

- (UILabel *)label{
    if (!_label) {
        _label = [[UILabel alloc] init];
        _label.textAlignment = NSTextAlignmentCenter;
        _label.font = [UIFont systemFontOfSize:self.frame.size.height / 2.0];
        [_label setTextColor:[UIColor colorWithRed:0.4 green:0 blue:0.8 alpha:0.6]];
        [self addSubview:_label];
    }
    return _label;
}



- (void)setTextColor:(UIColor *)textColor{
    _textColor = textColor;
    [self.label setTextColor:_textColor];
}



- (void)setCount:(CGFloat)count{
    _count = count;
    _currentCount = _count;
}
- (void)setCurrentCount:(CGFloat)currentCount{
    _currentCount = currentCount;
    [self setNeedsDisplay];
}


- (void)setTextFont:(UIFont *)textFont{
    _textFont = textFont;
    self.label.font = textFont;
}

- (void)setType:(CountDownViewType)type{
    _type = type;
    
    if (_type == CountDownViewTypeCircle) {
        self.clipsToBounds = YES;
        self.layer.cornerRadius = self.frame.size.width/2.0;

    } else if (_type == CountDownViewTypeRect){
        self.clipsToBounds = YES;
        self.layer.cornerRadius = 1;
        
    }else if (_type == CountDownViewTypeRect){
        
        
    }
}

- (void)setHiddenText:(BOOL)hiddenText{
    _hiddenText = hiddenText;
    self.label.hidden = _hiddenText;
    
}


@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末函似,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子喉童,更是在濱河造成了極大的恐慌撇寞,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件泄朴,死亡現(xiàn)場離奇詭異重抖,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)祖灰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進(jìn)店門钟沛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人局扶,你說我怎么就攤上這事恨统。” “怎么了三妈?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵畜埋,是天一觀的道長。 經(jīng)常有香客問我畴蒲,道長悠鞍,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任模燥,我火速辦了婚禮咖祭,結(jié)果婚禮上掩宜,老公的妹妹穿的比我還像新娘。我一直安慰自己么翰,他們只是感情好牺汤,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著浩嫌,像睡著了一般檐迟。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上码耐,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天追迟,我揣著相機(jī)與錄音,去河邊找鬼伐坏。 笑死怔匣,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的桦沉。 我是一名探鬼主播每瞒,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼纯露!你這毒婦竟也來了剿骨?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤埠褪,失蹤者是張志新(化名)和其女友劉穎浓利,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體钞速,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡贷掖,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了渴语。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片苹威。...
    茶點(diǎn)故事閱讀 39,992評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖驾凶,靈堂內(nèi)的尸體忽然破棺而出牙甫,到底是詐尸還是另有隱情,我是刑警寧澤调违,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布窟哺,位于F島的核電站,受9級特大地震影響技肩,放射性物質(zhì)發(fā)生泄漏且轨。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望旋奢。 院中可真熱鬧阿蝶,春花似錦、人聲如沸黄绩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽爽丹。三九已至,卻和暖如春辛蚊,著一層夾襖步出監(jiān)牢的瞬間粤蝎,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工袋马, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留初澎,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓虑凛,卻偏偏與公主長得像碑宴,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子桑谍,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,144評論 25 707
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫延柠、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,104評論 4 62
  • 早睡是為了身體锣披,早起是為了我們的內(nèi)心贞间。 今天在哪思考:內(nèi)蒙古呼倫貝爾莫力達(dá)瓦治自旗尼爾基水利風(fēng)景區(qū) 天氣 心情:還...
    沙漠中的魚在飛閱讀 113評論 0 0
  • 天氣炎熱的早晨,公交車上大家都安安靜靜享受空調(diào)下的舒服雹仿。拿起手機(jī)看看信息什么的增热,突然就聽到“啪”的一聲,像是手打到...
    兮南樓閱讀 283評論 0 1