iOS 扇形 以及其他绘图

  • 参考连接

  • 第一种圆形扇形


    iOS 扇形 以及其他绘图_第1张图片
  • PieView.h

@interface PieView : UIView

-(instancetype)initWithCenter:(CGPoint)center radius:(CGFloat)radius bgColor:(UIColor *)bgColor andstartAngle:(CGFloat)startAngle andendAngle:(CGFloat)endAngle;

@end
-(instancetype)initWithCenter:(CGPoint)center radius:(CGFloat)radius bgColor:(UIColor *)bgColor andstartAngle:(CGFloat)startAngle andendAngle:(CGFloat)endAngle {
    
    self = [super init];
    if (self) {
        //设置self的frame和center
        self.backgroundColor = bgColor;
        self.frame = CGRectMake(0, 0, radius * 2, radius * 2);
        self.center = center;
        
    
        //特别注意:贝塞尔曲线的radius必须为self高度的四分之一,CAShapeLayer的线宽必须为self高度的二分之一
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(radius, radius) radius:radius / 2 startAngle:startAngle endAngle:endAngle clockwise:YES];
        
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.path = path.CGPath;
        maskLayer.fillColor = [UIColor clearColor].CGColor;
        maskLayer.strokeColor = bgColor.CGColor;
        maskLayer.lineWidth = radius; //等于半径的2倍,以圆的边缘为中心,向圆内部伸展一个半径,向外伸展一个半径,所以看上去以为圆的半径是self高度的一半。
        self.layer.mask = maskLayer;
    }
    
    return self;
}
  • 实现
CGPoint point = CGPointMake(150, 150);
    PieView *redView = [[PieView alloc] initWithCenter:point radius:50 bgColor:[UIColor redColor] andstartAngle:-M_PI/2 andendAngle:0.0];
    [self.view addSubview:redView];
    
    PieView *blueView = [[PieView alloc] initWithCenter:point radius:70 bgColor:[UIColor blueColor] andstartAngle:0.0f andendAngle:M_PI*3/2];
    [self.view addSubview:blueView];
  • 第二种扇形圆形
#import "CustomView.h"

@implementation CustomView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        
    }
    return self;
}

-(void)setArray:(NSArray *)array
{
    _array = array;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    
    CGFloat x = self.frame.size.width / 2;
    CGFloat y = self.frame.size.height / 2;
    CGFloat radius = (self.frame.size.width / 2) - 20.0f;
    
    float startAng= 0;
    
    for(NSNumber * obj in _array)
    {
        //计算角度
        CGFloat Ang = [obj floatValue] * M_PI*2;
        //计算结束角度
        Ang = Ang + startAng;
        //路径
        UIBezierPath *path =[UIBezierPath bezierPathWithArcCenter:CGPointMake(x, y) radius:radius startAngle:startAng endAngle:Ang clockwise:YES];
        //画线
        [path addLineToPoint:CGPointMake(x, y)];
        //这里randomColor是封装的类方法,这里就不写了,具体作用就是生成随机色
        [[self setAndomColor] setFill];
        //渲染
        [path fill];
        startAng = Ang;
    }
    
}

-(UIColor *)setAndomColor
{
    CGFloat redValue = arc4random() %255;
    CGFloat greenValue = arc4random() %255;
    CGFloat blueValue = arc4random() %255;
    UIColor *andomColor = [UIColor colorWithRed: redValue/255.0f green:greenValue/255.0f blue: blueValue/255.0f alpha:1.0f];
    return andomColor;
}
CGFloat x = 60.0f;
    CGFloat y = 60.0f;
    CGFloat width = 100.0f;
    CGFloat height = 100.0f;

    CustomView *cView = [[CustomView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    cView.backgroundColor = [UIColor redColor];
    [self.view addSubview:cView];
    
    cView.array = @[@0.1,@0.2,@0.3,@0.4];
    
    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0/*延迟执行时间*/ * NSEC_PER_SEC));
    
    dispatch_after(delayTime, dispatch_get_main_queue(), ^{
        cView.array = @[@0.25,@0.25,@0.25,@0.25];
    });
iOS 扇形 以及其他绘图_第2张图片
  • UIBezierPath 画矩形直线
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(20.0f, 40.0f, 20.0f, 2.0f)];
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    layer.path = bezierPath.CGPath;
    layer.fillColor = [UIColor orangeColor].CGColor;
    [self.view.layer addSublayer:layer];

你可能感兴趣的:(iOS 扇形 以及其他绘图)