// 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
IOS-一個(gè)自定義進(jìn)度條的小例子
最后編輯于 :
?著作權(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ù)。
【社區(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只是自定義了滑塊,...