oc实现倒计时(带圆圈)

.h文件

#import <UIKit/UIKit.h>

//宽

#define LINEWIDTH 5

//总时间

//#define ALLTOTALTIME 61

//定时器时间

#define TIMER 1

typedef NS_ENUM(NSInteger,CircularProgressViewType)

{

    CircularProgressViewType_Label = 0,

    CircularProgressViewType_Button,

};

@protocol CircularProgressDelegate;

@interface CircularProgressView : UIView

@property (assign, nonatomic) id <CircularProgressDelegate> delegate;

- (id)initWithFrame:(CGRect)frame

          backColor:(UIColor *)backColor

      progressColor:(UIColor *)progressColor

          lineWidth:(CGFloat)lineWidth

          totalTime:(CGFloat)totalTime

               type:(CircularProgressViewType)type;

        

- (void)play;

- (void)pause;

- (void)revert;

@end

@protocol CircularProgressDelegate <NSObject>

- (void)didUpdateProgressView:(CGFloat)progress;

//- (void)stopEvent;

@end

.m文件

//

//  PPX_CircularProgressView.m

//  PPX_CircularProgress

//

//  Created by pipixia on 16/10/9.

//  Copyright © 2016年 pipixia. All rights reserved.

//

#import "CircularProgressView.h"

#import "CommonUI.h"

@interface CircularProgressView ()

{

    BOOL isStart;

}

@property (strong, nonatomic) UIColor *backColor;

@property (strong, nonatomic) UIColor *progressColor;

@property (assign, nonatomic) CGFloat lineWidth;

@property (assign, nonatomic) float progress;

@property (strong, nonatomic) NSTimer *timer;

@property (assign, nonatomic) NSInteger currentProgress;//目前的进展

@property (assign, nonatomic) NSInteger timerFloat;//总进展

@property (assign, nonatomic) NSInteger timeWhole;//总进展

@property (nonatomic, strong) UILabel *timeLabel;

@property (nonatomic, strong) UIButton *stopButton;

@end

@implementation CircularProgressView

- (id)initWithFrame:(CGRect)frame

          backColor:(UIColor *)backColor

      progressColor:(UIColor *)progressColor

          lineWidth:(CGFloat)lineWidth

          totalTime:(CGFloat)totalTime

               type:(CircularProgressViewType)type;

{

    CGSize size = [CommonUI screenDp];

    self = [super initWithFrame:frame];

    if (self)

    {

        self.backgroundColor = [UIColor clearColor];

        _backColor = backColor;

        _progressColor = progressColor;

        _lineWidth = lineWidth;

        _timerFloat = totalTime;

        _currentProgress = totalTime;

        _progress = 1;

        _timeWhole = totalTime;

        isStart = YES;

        

        if (type == CircularProgressViewType_Label)

        {

            _timeLabel = [[UILabel alloc]init];

            _timeLabel.font = [UIFont boldSystemFontOfSize:(size.width*0.1)];

            _timeLabel.textAlignment = NSTextAlignmentCenter;

            _timeLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);

            _timeLabel.center = CGPointMake(self.bounds.size.width/2,self.bounds.size.height*0.4);

            NSString *str_minute = [NSString stringWithFormat:@"%02ld", (_timeWhole % 3600) / 60];

            NSString *str_second = [NSString stringWithFormat:@"%02ld", _timeWhole % 60];

            NSString *format_time = [NSString stringWithFormat:@"%@ : %@", str_minute, str_second];

              // 修改倒计时标签及显示内容

              self.timeLabel.text = format_time;

            _timeLabel.textColor = [UIColor whiteColor];

            [self addSubview:_timeLabel];

            

            UILabel *warm = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.width*0.12)];

            warm.center = CGPointMake(self.bounds.size.width/2,self.bounds.size.height*0.65);

            warm.textAlignment = NSTextAlignmentCenter;

            warm.font = [UIFont systemFontOfSize:size.width*0.04];

            warm.text = @"Warm up";

            warm.textColor = [UIColor whiteColor];

            [self addSubview:warm];

            

        }

        else

        {

            _stopButton = [UIButton buttonWithType:UIButtonTypeCustom];

            _stopButton.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);

            [_stopButton setTitle:@"Stop" forState:UIControlStateNormal];

            [_stopButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

            [_stopButton addTarget:self action:@selector(stopClicked) forControlEvents:UIControlEventTouchUpInside];

            [self addSubview:_stopButton];

        }

    }

    return self;

}

- (void)drawRect:(CGRect)rect{

    //draw background circle

    UIBezierPath *backCircle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width / 2,self.bounds.size.height / 2)

                                                              radius:self.bounds.size.width / 2 - self.lineWidth / 2

                                                          startAngle:(CGFloat) - M_PI_2

                                                            endAngle:(CGFloat)(1.5 * M_PI)

                                                           clockwise:YES];

    [self.backColor setStroke];

    backCircle.lineWidth = self.lineWidth;

    [backCircle stroke];

    if (self.progress != 0){

        //draw progress circle

        UIBezierPath *progressCircle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width / 2,self.bounds.size.height / 2)

                                                                      radius:self.bounds.size.width / 2 - self.lineWidth / 2

                                                                  startAngle:(CGFloat) - M_PI_2

                                                                    endAngle:(CGFloat)(- M_PI_2 + self.progress * 2 * M_PI)

                                                                   clockwise:YES];

        [self.progressColor setStroke];

        progressCircle.lineWidth = self.lineWidth;

        [progressCircle stroke];

    }

}

- (void)updateProgressCircle{

    self.currentProgress -= 1;

    self.timeWhole -= 1;

    //update progress value

    self.progress = (float) (_currentProgress * 1.0 / _timerFloat);

    //redraw back & progress circles

    [self setNeedsDisplay];

    

    if (self.delegate && [self.delegate conformsToProtocol:@protocol(CircularProgressDelegate)]){

        [self.delegate didUpdateProgressView:self.currentProgress];

    }

    

    NSString *str_minute = [NSString stringWithFormat:@"%02ld", (_timeWhole % 3600) / 60];

    NSString *str_second = [NSString stringWithFormat:@"%02ld", _timeWhole % 60];

    NSString *format_time = [NSString stringWithFormat:@"%@ : %@", str_minute, str_second];

    // 修改倒计时标签及显示内容

    

    self.timeLabel.text = format_time;

    

    

    if (self.progress <= 0.0f){

        //invalid timer

        [self.timer invalidate];

        self.currentProgress = _timeWhole;

        self.timeWhole = _timeWhole;

        //restore progress value

        //        self.progress = 0;

        //self redraw

        //        [self setNeedsDisplay];

    }

}

//- (void)stopClicked

//{

//    if (self.delegate && [self.delegate conformsToProtocol:@protocol(CircularProgressDelegate)])

//    {

//        [self.delegate stopEvent];

//    }

//    

//}

- (void)play{

    if (isStart){

        self.timer = [NSTimer scheduledTimerWithTimeInterval:TIMER target:self selector:@selector(updateProgressCircle) userInfo:nil repeats:YES];

        [self.timer fire];

        self->isStart = NO;

    }

}

- (void)pause{

    if (!isStart){

        [self.timer invalidate];

        self.timer = nil;

        isStart = YES;

    }

}

- (void)revert{

    isStart = YES;

    [self updateProgressCircle];

    self.progress = _timeWhole;

    self.currentProgress = _timeWhole;

    self.timeWhole = _timeWhole;

    NSString *str_minute = [NSString stringWithFormat:@"%02ld", (_timeWhole % 3600) / 60];

    NSString *str_second = [NSString stringWithFormat:@"%02ld", _timeWhole % 60];

    NSString *format_time = [NSString stringWithFormat:@"%@ : %@", str_minute, str_second];

// 修改倒计时标签及显示内容

    self.timeLabel.text = format_time;

    [self.timer invalidate];

    self.timer = nil;

}

@end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
红绿色盲的主要特征是对红色和绿色颜色的区分能力降低。因此,我们可以通过CIColorMatrix滤镜将图像的红色和绿色通道进行转换,从而实现对红绿色盲的过滤。下面是OC代码实现: ``` //获取原始图像 CIImage *inputImage = [CIImage imageWithCGImage:image.CGImage]; //创建颜色矩阵 const CGFloat colorMatrix[] = { 0.567, 0.433, 0, 0, 0, 0.558, 0.442, 0, 0, 0, 0, 0.242, 0.758, 0, 0, 0, 0, 0, 1, 0 }; CIVector *colorMatrixVector = [CIVector vectorWithValues:colorMatrix count:16]; //创建CIColorMatrix滤镜 CIFilter *colorMatrixFilter = [CIFilter filterWithName:@"CIColorMatrix"]; [colorMatrixFilter setValue:inputImage forKey:kCIInputImageKey]; [colorMatrixFilter setValue:colorMatrixVector forKey:@"inputRVector"]; [colorMatrixFilter setValue:colorMatrixVector forKey:@"inputGVector"]; [colorMatrixFilter setValue:colorMatrixVector forKey:@"inputBVector"]; [colorMatrixFilter setValue:colorMatrixVector forKey:@"inputAVector"]; //获取输出图像 CIImage *outputImage = [colorMatrixFilter outputImage]; //将CIImage转换成UIImage CIContext *context = [CIContext contextWithOptions:nil]; CGImageRef imageRef = [context createCGImage:outputImage fromRect:outputImage.extent]; UIImage *resultImage = [UIImage imageWithCGImage:imageRef]; //释放CGImageRef CGImageRelease(imageRef); //返回结果 return resultImage; ``` 以上代码实现了对原始图像进行红绿色盲过滤的效果。在颜色矩阵中,我们将红色通道的权值设置为0.567,将绿色通道的权值设置为0.442,从而实现了对红绿色的转换。你可以根据自己的需求,修改颜色矩阵,实现不同的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_41620230

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值