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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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];

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    NSLog(@"touch begin");

}

@end

以上是关于IOS-一个自定义进度条的小例子的主要内容,如果未能解决你的问题,请参考以下文章

android 怎么自定义绘制如下图中这种进度条

Inno Setup - 在自定义页面上复制带有进度条的文件

c_cpp 进度条的一个漂亮的小可自定义视图,具有可选的动画属性

关于文件上传下载以及其他进度条的实现

C# WinForm自定义进度条

带有进度条的按钮 android