IOS-一个自定义进度条的小例子

    //  processDIY.m
    
    #import "processDIY.h"
    
    
    
    static const CGFloat kBorderWidth = 2.0f;
    
    
    
    @interface THProgressLayer : CAReplicatorLayer
    
    @property (nonatomic, strong) UIColor* progressTintColor;
    
    @property (nonatomic, strong) UIColor* borderTintColor;
    
    @property (nonatomic) CGFloat progresstemp;
    
    @end
    
    
    
    @implementation THProgressLayer
    
    
    
    @dynamic progressTintColor;
    
    @dynamic borderTintColor;
    
    
    
    + (BOOL)needsDisplayForKey:(NSString *)key
    
    {
        
        return [key isEqualToString:@"progresstemp"] ? YES : [super needsDisplayForKey:key];
        
    }
    
    
    
    - (void)drawInContext:(CGContextRef)context
    
    {
        
        [self isKindOfClass:[NSString class]];
        
        
        
        CGRect rect = CGRectInset(self.bounds, kBorderWidth, kBorderWidth);
        
        CGFloat radius = CGRectGetHeight(rect) / 2.0f;
        
        CGContextSetLineWidth(context, kBorderWidth);
        
        CGContextSetStrokeColorWithColor(context, self.borderTintColor.CGColor);
        
        
        
        [self drawRectangleInContext:context inRect:rect withRadius:radius];
        
        CGContextStrokePath(context);
        
        
        
        CGContextSetFillColorWithColor(context, self.progressTintColor.CGColor);
        
        CGRect progressRect = CGRectInset(rect, 2 * kBorderWidth, 2 * kBorderWidth);
        
        CGFloat progressRadius = CGRectGetHeight(progressRect) / 2.0f;
        
        progressRect.size.width = fmaxf(self.progresstemp * progressRect.size.width, 2.0f * progressRadius);
        
        [self drawRectangleInContext:context inRect:progressRect withRadius:progressRadius];
        
        CGContextFillPath(context);
        
    }
    
    - (void)drawRectangleInContext:(CGContextRef)context inRect:(CGRect)rect withRadius:(CGFloat)radius
    
    {
        
        CGContextMoveToPoint(context, rect.origin.x, rect.origin.y + radius);
        
        CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + rect.size.height - radius);
        
        CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + rect.size.height - radius, radius, M_PI, M_PI / 2, 1);
        
        CGContextAddLineToPoint(context, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height);
        
        CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height - radius, radius, M_PI / 2, 0.0f, 1);
        
        CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + radius);
        
        CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + radius, radius, 0.0f, -M_PI / 2, 1);
        
        CGContextAddLineToPoint(context, rect.origin.x + radius, rect.origin.y);
        
        CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + radius, radius, -M_PI / 2, M_PI, 1);
        
    }
    
    @end
    
    
    
    
    
    @implementation processDIY
    
    
    
    -(instancetype)initWithFrame:(CGRect)frame
    
    {
        
        self = [super initWithFrame:frame];
        
        if (self) {
            
            self.backgroundColor = [UIColor clearColor];
            
            UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
            
            tapGesture.numberOfTapsRequired=1;
            
            [self addGestureRecognizer:tapGesture];
            
        }
        
        return self;
        
    }
    
    -(IBAction)handleTapGesture:(UITapGestureRecognizer*)sender{
        
        CGPoint tapPoint = [sender locationInView:self];
        
        NSLog(@"x:%f,y:%f",tapPoint.x,tapPoint.y);
        
    }
    
    -(void)click
    
    {
        
        NSLog(@"click me");
        
    }
    
    + (Class)layerClass
    
    {
        
        return [THProgressLayer class];
        
    }
    
    - (void)didMoveToWindow
    
    {
        
        self.progressLayer.contentsScale = self.window.screen.scale;
        
    }
    
    - (THProgressLayer *)progressLayer
    
    {
        
        return (THProgressLayer *)self.layer;
        
    }
    
    -(CGFloat)progress
    
    {
        
        return self.progressLayer.progresstemp;
        
    }
    
    -(void)setProcessnow:(CGFloat)progress
    
    {
        
        [self.progressLayer removeAnimationForKey:@"progresstemp"];
        
        CGFloat pinnedProgress = MIN(MAX(progress, 0.0f), 1.0f);
        
        
        
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"progresstemp"];
        
        animation.fromValue = [NSNumber numberWithFloat:self.progress];
        
        animation.toValue = [NSNumber numberWithFloat:pinnedProgress];
        
        CGFloat time = fabs(self.progress-pinnedProgress)+0.3;
        
        animation.duration = time;
        
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        
        
        
        [self.progressLayer addAnimation:animation forKey:@"progresstemp"];
        
        self.progressLayer.progresstemp = pinnedProgress;
        
    }
    
    - (UIColor *)progressTintColor
    
    {
        
        return self.progressLayer.progressTintColor;
        
    }
    
    
    
    - (void)setProgressTintColor:(UIColor *)progressTintColor
    
    {
        
        self.progressLayer.progressTintColor = progressTintColor;
        
        [self.progressLayer setNeedsDisplay];
        
    } 
    
    - (UIColor *)borderTintColor
    
    {
        
        return self.progressLayer.borderTintColor;
        
    }
    
    - (void)setBorderTintColor:(UIColor *)borderTintColor
    
    {
        
        self.progressLayer.borderTintColor = borderTintColor;
        
        [self.progressLayer setNeedsDisplay];
        
    }
    
    @end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 忙里偷闲练习一下 Swift效果图: 效果分析: 其实这个效果就是就是类似一个 UISlider只是自定义了滑块,...
    云之君兮鹏阅读 6,182评论 3 5
  • ——夜策冷推书系列,第二期 《星空倒影》作者弦歌雅意 这本书很老了,05年的书。我可以很肯定地说绝无仅有。 ...
    夜策冷_31e6阅读 4,108评论 0 0
  • 突然想起高中的一节语文课 老师问:你的理想是什么 当时回答的人有很多,有人说朋友,有人说婚姻 ,有人说事业。 后来...
    哩噢阅读 2,586评论 0 1
  • 昨晚,室友在用贴纸装扮她的小床,我们这些“吃瓜群众们”,说她都快把属于她的小天地当成是婚房在布置了。她笑了笑,...
    我的世界日光倾城阅读 1,943评论 0 0
  • 喜欢认识你的时间 像留着手掌心的烙印 肉香弥漫还有痛感 喜欢你说话的样子 大大咧咧 没心没肺的呐喊 喜欢你噘嘴时的...
    南山有殇阅读 1,033评论 0 0