画实线、虚线圆
.h
//
// HanDrawLineTools.h
// HT-Line
//
// Created by han on 2022/11/1.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface HanDrawLineTools : NSObject
/// 创建圆环线
/// @param superView 父视图
/// @param lineColor 线颜色
/// @param lineWidth 线宽
/// @param center 圆环中心点
/// @param radius 半径
/// @param startAngle 开始角度
/// @param endAngle 结束角度
/// @param isFull 是否是实线
-(void)createCircleLineWithSuperView:(UIView *)superView lineColor:(UIColor *)lineColor lineWidth:(CGFloat)lineWidth arcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle isFull:(BOOL)isFull;
+(void)createCircleLineWithSuperView:(UIView *)superView lineColor:(UIColor *)lineColor lineWidth:(CGFloat)lineWidth arcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle isFull:(BOOL)isFull;
@end
NS_ASSUME_NONNULL_END
.m
//
// HanDrawLineTools.m
// HT-Line
//
// Created by han on 2022/11/1.
//
#import "HanDrawLineTools.h"
@implementation HanDrawLineTools
/// 创建圆环线
-(void)createCircleLineWithSuperView:(UIView *)superView lineColor:(UIColor *)lineColor lineWidth:(CGFloat)lineWidth arcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle isFull:(BOOL)isFull{
[HanDrawLineTools createCircleLineWithSuperView:superView lineColor:lineColor lineWidth:lineWidth arcCenter:center radius:radius startAngle:startAngle endAngle:endAngle isFull:isFull];
}
/// 创建圆环线
+(void)createCircleLineWithSuperView:(UIView *)superView lineColor:(UIColor *)lineColor lineWidth:(CGFloat)lineWidth arcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle isFull:(BOOL)isFull{
struct CGColor *lineCGColor = lineColor.CGColor;
// 圆弧虚线
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
// 线宽
shapeLayer.lineWidth = lineWidth;
// 端点样式
shapeLayer.lineCap = kCALineCapButt;
// 终点处理
shapeLayer.lineJoin = kCALineCapButt;
// 线条的颜色
shapeLayer.strokeColor = lineCGColor;
// 背景填充色
shapeLayer.fillColor = [UIColor clearColor].CGColor;
if (!isFull) {
// 设置线宽、线间距(虚线)
[shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:10], [NSNumber numberWithInt:5], nil]];
}
UIBezierPath* path = [UIBezierPath bezierPathWithArcCenter:center
radius:radius
startAngle:startAngle
endAngle:endAngle
clockwise:YES];
[path stroke];
shapeLayer.path = [path CGPath];
[superView.layer addSublayer:shapeLayer];
}
@end
用法
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
CGFloat height = self.view.frame.size.height;
CGFloat radius = self.view.frame.size.width/2 ;
CGFloat lineWidth = 1.5;
UIColor *lineColor = [[UIColor alloc] initWithRed:0 green:0 blue:0 alpha:0.5];
CGPoint center = CGPointMake(self.view.frame.size.width/2, height/2);
CGFloat startAngle = M_PI;
CGFloat endAngle = M_PI * -0.75;
CGFloat startAngle2 = M_PI * -0.75;
CGFloat endAngle2 = 0;
CGFloat startAngle3 = 0;
CGFloat endAngle3 = M_PI * 0.5;
CGFloat startAngle4 = M_PI * 0.5;
CGFloat endAngle4 = M_PI;
[HanDrawLineTools createCircleLineWithSuperView:self.view lineColor:lineColor lineWidth:lineWidth arcCenter:center radius:radius startAngle:startAngle endAngle:endAngle isFull:YES];
[HanDrawLineTools createCircleLineWithSuperView:self.view lineColor:lineColor lineWidth:lineWidth arcCenter:center radius:radius startAngle:startAngle2 endAngle:endAngle2 isFull:NO];
[HanDrawLineTools createCircleLineWithSuperView:self.view lineColor:lineColor lineWidth:lineWidth arcCenter:center radius:radius startAngle:startAngle3 endAngle:endAngle3 isFull:YES];
[HanDrawLineTools createCircleLineWithSuperView:self.view lineColor:lineColor lineWidth:lineWidth arcCenter:center radius:radius startAngle:startAngle4 endAngle:endAngle4 isFull:NO];
}
效果图
效果图
网友评论