zoukankan      html  css  js  c++  java
  • iOS画圆、画线

    UIView:

    - (void)drawRect:(CGRect)rect {
        [super drawRect:rect];
        
        CGRect frame = CGRectMake(50, 100, 100, 100);
        /*画填充圆
         */
        CGContextRef context = UIGraphicsGetCurrentContext();
        [[UIColor whiteColor] set];
        CGContextFillRect(context, rect);
        
        CGContextAddEllipseInRect(context, frame);
        [[UIColor orangeColor] set];
        CGContextFillPath(context);
        
        /*边框圆
         */
        CGContextSetRGBStrokeColor(context, 255/255.0, 106/255.0, 0/255.0, 1);
        CGContextSetLineWidth(context, 5);
        CGContextAddArc(context, 50, 70, 20, 0, 2*M_PI, 0);
        CGContextDrawPath(context, kCGPathStroke);
    
        /*画直线
         */
        CGRect markFrame = CGRectMake(100, 250, 200, 200);
        [[UIColor orangeColor] set];
        CGContextSetLineWidth(context, 4);
        CGPoint points[5];
        points[0] = CGPointMake(markFrame.origin.x,markFrame.origin.y);
        points[1] = CGPointMake(markFrame.origin.x+markFrame.size.width/4, markFrame.origin.y+markFrame.size.height/2);
        points[2] = CGPointMake(markFrame.origin.x+markFrame.size.width/2, markFrame.origin.y+5);
        points[3] = CGPointMake(markFrame.origin.x+markFrame.size.width/4*3,markFrame.origin.y+markFrame.size.height/2);
        points[4] = CGPointMake(markFrame.origin.x+markFrame.size.width, markFrame.origin.y);
        
        CGContextAddLines(context, points, 5);
        CGContextDrawPath(context, kCGPathStroke);
        //边框圆
        CGContextSetLineWidth(context, 5);
        CGContextAddArc(context, markFrame.origin.x+markFrame.size.width/2, markFrame.origin.y+markFrame.size.height/2, markFrame.size.height/2-2, 0, 2*M_PI, 0);
        CGContextDrawPath(context, kCGPathStroke);
        
        /*曲线
         */
        [[UIColor redColor] set];
        
        CGContextSetLineWidth(context, 4.0);
        CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor);
        CGContextMoveToPoint(context, 300, 370);
        CGContextAddCurveToPoint(context,  193, 320, 100, 370, 100, 370);
        CGContextStrokePath(context);
    }
    
    

    UIViewController:

    #import "DrawViewController.h"
    
    @interface DrawViewController ()
    
    @end
    
    @implementation DrawViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        /*
         *画虚线圆
         */
        CAShapeLayer *dotteLine =  [CAShapeLayer layer];
        CGMutablePathRef dottePath =  CGPathCreateMutable();
        dotteLine.lineWidth = 2.0f ;
        dotteLine.strokeColor = [UIColor orangeColor].CGColor;
        dotteLine.fillColor = [UIColor clearColor].CGColor;
        CGPathAddEllipseInRect(dottePath, nil, CGRectMake(50.0f,  50.0f, 200.0f, 200.0f));
        dotteLine.path = dottePath;
        NSArray *arr = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:10],[NSNumber numberWithInt:5], nil];
        dotteLine.lineDashPhase = 1.0;
        dotteLine.lineDashPattern = arr;
        CGPathRelease(dottePath);
        [self.view.layer addSublayer:dotteLine];
        
        /*
         *画实线圆
         */
        CAShapeLayer *solidLine =  [CAShapeLayer layer];
        CGMutablePathRef solidPath =  CGPathCreateMutable();
        solidLine.lineWidth = 2.0f ;
        solidLine.strokeColor = [UIColor orangeColor].CGColor;
        solidLine.fillColor = [UIColor clearColor].CGColor;
        CGPathAddEllipseInRect(solidPath, nil, CGRectMake(50.0f,  300.0f, 200.0f, 200.0f));
        solidLine.path = solidPath;
        CGPathRelease(solidPath);
        [self.view.layer addSublayer:solidLine];
        
        /*
         *画虚线
         */
        CAShapeLayer *dotteShapeLayer = [CAShapeLayer layer];
        CGMutablePathRef dotteShapePath =  CGPathCreateMutable();
        [dotteShapeLayer setFillColor:[[UIColor clearColor] CGColor]];
        [dotteShapeLayer setStrokeColor:[[UIColor orangeColor] CGColor]];
        dotteShapeLayer.lineWidth = 2.0f ;
        NSArray *dotteShapeArr = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:10],[NSNumber numberWithInt:5], nil];
        [dotteShapeLayer setLineDashPattern:dotteShapeArr];
        CGPathMoveToPoint(dotteShapePath, NULL, 20,500);
        CGPathAddLineToPoint(dotteShapePath, NULL, 20, 285);
        CGPathAddLineToPoint(dotteShapePath, NULL, 300,285);
        [dotteShapeLayer setPath:dotteShapePath];
        CGPathRelease(dotteShapePath);
        [self.view.layer addSublayer:dotteShapeLayer];
        
        /*
         *画实线
         */
        CAShapeLayer *solidShapeLayer = [CAShapeLayer layer];
        CGMutablePathRef solidShapePath =  CGPathCreateMutable();
        [solidShapeLayer setFillColor:[[UIColor clearColor] CGColor]];
        [solidShapeLayer setStrokeColor:[[UIColor orangeColor] CGColor]];
        solidShapeLayer.lineWidth = 2.0f ;
        CGPathMoveToPoint(solidShapePath, NULL, 20, 265);
        CGPathAddLineToPoint(solidShapePath, NULL, 300,265);
        CGPathAddLineToPoint(solidShapePath, NULL, 300,50);
        [solidShapeLayer setPath:solidShapePath];
        CGPathRelease(solidShapePath);
        [self.view.layer addSublayer:solidShapeLayer];
        
        
    }
    


    示图:

     
  • 相关阅读:
    zabbix server监控本机agent
    虚拟机添加配置双网卡linux
    linux改变默认运行级别
    linux重置root密码
    vi文本编辑器
    centos7 mysql安装和基本操作yum安装
    安装wordpress个人网站
    httpd安装与配置(centos7) 2
    httpd安装与配置(centos7)
    Mac升级Python3
  • 原文地址:https://www.cnblogs.com/wxios/p/4533106.html
Copyright © 2011-2022 走看看