IOS-一個(gè)自定義進(jìn)度條的小例子

    //  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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 忙里偷閑練習(xí)一下 Swift效果圖: 效果分析: 其實(shí)這個(gè)效果就是就是類似一個(gè) UISlider只是自定義了滑塊,...
    云之君兮鵬閱讀 2,103評(píng)論 3 5
  • ——夜策冷推書系列,第二期 《星空倒影》作者弦歌雅意 這本書很老了,05年的書。我可以很肯定地說(shuō)絕無(wú)僅有。 ...
    夜策冷_31e6閱讀 831評(píng)論 0 0
  • 突然想起高中的一節(jié)語(yǔ)文課 老師問:你的理想是什么 當(dāng)時(shí)回答的人有很多,有人說(shuō)朋友,有人說(shuō)婚姻 ,有人說(shuō)事業(yè)。 后來(lái)...
    哩噢閱讀 374評(píng)論 0 1
  • 昨晚,室友在用貼紙裝扮她的小床,我們這些“吃瓜群眾們”,說(shuō)她都快把屬于她的小天地當(dāng)成是婚房在布置了。她笑了笑,...
    我的世界日光傾城閱讀 306評(píng)論 0 0
  • 喜歡認(rèn)識(shí)你的時(shí)間 像留著手掌心的烙印 肉香彌漫還有痛感 喜歡你說(shuō)話的樣子 大大咧咧 沒心沒肺的吶喊 喜歡你噘嘴時(shí)的...
    南山有殤閱讀 165評(píng)論 0 0

友情鏈接更多精彩內(nèi)容