123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- //
- // QMUIPieProgressView+RQExtension.m
- // jiaPei
- //
- // Created by 张嵘 on 2022/7/26.
- // Copyright © 2022 JCZ. All rights reserved.
- //
- #import "QMUIPieProgressView+RQExtension.h"
- #import "QMUICore.h"
- @interface RQUIPieProgressLayer : CALayer
- @property(nonatomic, strong) UIColor *fillColor;
- @property(nonatomic, strong) UIColor *strokeColor;
- @property(nonatomic, assign) float progress;
- @property(nonatomic, assign) CFTimeInterval progressAnimationDuration;
- @property(nonatomic, assign) BOOL shouldChangeProgressWithAnimation; // default is YES
- @property(nonatomic, assign) CGFloat borderInset;
- @property(nonatomic, assign) CGFloat lineWidth;
- @property(nonatomic, assign) QMUIPieProgressViewShape shape;
- @end
- @implementation RQUIPieProgressLayer
- // 加dynamic才能让自定义的属性支持动画
- @dynamic fillColor;
- @dynamic strokeColor;
- @dynamic progress;
- @dynamic shape;
- @dynamic lineWidth;
- @dynamic borderInset;
- - (instancetype)init {
- if (self = [super init]) {
- self.shouldChangeProgressWithAnimation = YES;
- }
- return self;
- }
- + (BOOL)needsDisplayForKey:(NSString *)key {
- return [key isEqualToString:@"progress"] || [super needsDisplayForKey:key];
- }
- - (id<CAAction>)actionForKey:(NSString *)event {
- if ([event isEqualToString:@"progress"] && self.shouldChangeProgressWithAnimation) {
- CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:event];
- animation.fromValue = [self.presentationLayer valueForKey:event];
- animation.duration = self.progressAnimationDuration;
- return animation;
- }
- return [super actionForKey:event];
- }
- - (void)drawInContext:(CGContextRef)context {
- if (CGRectIsEmpty(self.bounds)) {
- return;
- }
-
- CGPoint center = CGPointGetCenterWithRect(self.bounds);
- CGFloat radius = MIN(center.x, center.y) - self.borderWidth - self.borderInset;
- CGFloat startAngle = -M_PI_2;
- CGFloat endAngle = M_PI * 2 * self.progress + startAngle;
-
- switch (self.shape) {
- case QMUIPieProgressViewShapeSector: {
- // 绘制扇形进度区域
-
- CGContextSetFillColorWithColor(context, self.fillColor.CGColor);
- CGContextMoveToPoint(context, center.x, center.y);
- CGContextAddArc(context, center.x, center.y, radius, startAngle, endAngle, 0);
- CGContextClosePath(context);
- CGContextFillPath(context);
- }
- break;
-
- case QMUIPieProgressViewShapeRing: {
- // 绘制环形进度区域
- radius -= self.lineWidth;
- CGContextSetLineWidth(context, self.lineWidth);
- CGContextSetLineCap(context, kCGLineCapRound);
- CGContextSetStrokeColorWithColor(context, self.strokeColor.CGColor);
- CGContextAddArc(context, center.x, center.y, radius, startAngle, endAngle, 0);
- CGContextStrokePath(context);
- CGContextClosePath(context);
- }
- break;
- }
-
- [super drawInContext:context];
- }
- - (void)setFrame:(CGRect)frame {
- [super setFrame:frame];
- self.cornerRadius = CGRectGetHeight(frame) / 2;
- }
- @end
- @implementation QMUIPieProgressView (RQExtension)
- @dynamic rq_borderColor;
- + (Class)layerClass {
- return [RQUIPieProgressLayer class];
- }
- - (void)tintColorDidChange {
- [super tintColorDidChange];
- self.progressLayer.fillColor = self.tintColor;
- self.progressLayer.strokeColor = self.tintColor;
- // self.progressLayer.borderColor = RGBA_COLOR(239, 145, 167, 0.2).CGColor;
- }
- - (void)setRq_borderColor:(UIColor *)rq_borderColor {
- self.progressLayer.borderColor = rq_borderColor.CGColor;
- }
- - (RQUIPieProgressLayer *)progressLayer {
- return (RQUIPieProgressLayer *)self.layer;
- }
- @end
|