圆形倒计时

来源:互联网 发布:藤井莉娜淘宝 编辑:程序博客网 时间:2024/05/16 09:02

圆形倒计时,自定义UIView ,使用UIView自带的drawRect:(CGRect)方法,动态绘图

 


@interface CXRoundView :UIView

@property (nonatomic,strong)NSTimer *circleTimer;

@property (nonatomic,assign)NSInteger timeCount;

@end


#import "CXRoundView.h"

#define WIDTH [[UIScreen mainScreen] bounds].size.width

#define HEIGHT [[UIScreen mainScreen] bounds].size.height


@implementation CXRoundView


- (instancetype)initWithFrame:(CGRect)frame

{

    self = [superinitWithFrame:frame];

    if (self) {

        self.backgroundColor = [UIColorwhiteColor];

        [selfrunningTimer];

    }

    return self;

}


- (void)runningTimer{

    self.circleTimer = [NSTimer  scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];

}


- (void)timerAction:(NSTimer *)timer{

    static NSInteger timeCount =600;

    timeCount--;

    self.timeCount = timeCount;

    if (self.timeCount ==0) {

        [self.circleTimer invalidate];

        self.circleTimer =nil;

        [self finishView];

    }

    [self setNeedsDisplay];

}



- (void)finishView{

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提醒"message:@"倒计时结束"delegate:self cancelButtonTitle:@"确定otherButtonTitles:nil];

    [alert show];

    [self removeFromSuperview];

}



- (void)drawRect:(CGRect)rect {

    //获取上下文对象

    CGContextRef context =UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context,3);

    CGContextSetRGBStrokeColor(context,1.0,69 / 255.0,0.5,1.0);

    //CGContextAddArc(上下文对象,圆心x,圆心y,曲线开始点,曲线结束点,半径)

    CGContextAddArc(context,self.frame.size.width /2.0, self.frame.size.height / 2.0, self.bounds.size.width / 2.0 - 3.0,0,self.timeCount /600.0 *2 *M_PI,0);

    CGContextStrokePath(context);

}

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

使用该自定义view时只需要引入头文件,创建CXRoundView对象,添加到控制器的view上即可

     CXRoundView *circle = [[CXRoundView alloc]initWithFrame:CGRectMake(WIDTH - 72, HEIGHT /4 -100,50,50)];


    [self.view addSubview:circle];


来源于 https://github.com/Easyzhan/CircleCountDown


0 0