美文网首页
iOS图形绘制

iOS图形绘制

作者: 小小白衣 | 来源:发表于2017-09-28 17:23 被阅读0次

    -------------------简单线条、图形-------------------

    #import "MyShapeView.h"
    
    @implementation MyShapeView
    
    /*
     这个方法在绘制视图对象的时候调用
     如果需要调用这个方法里面的代码,我们不能直接调用drawRect方法
     只能调用-(void)setNeedsDisplay方法,系统会自动调用drawRect方法
     */
    -(void)drawRect:(CGRect)rect
    {
        //获取上下文
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        
        //1.直线
        //设置绘制直线的颜色
        /*
         第一个参数:上下文
         第二~五个参数:颜色值
         */
        //CGContextSetRGBStrokeColor(ctx, 220.0f/255.0f, 220.0f/255.0f, 220.0f/255.0f, 1.0f);
        //设置线宽度
        CGContextSetLineWidth(ctx, 10);
        
        //设置直线的端点
        const CGPoint points1 [] = {CGPointMake(30, 80),CGPointMake(140, 80)};
        //绘制直线
        /*
         第一个参数:上下文
         第二个参数:直线的端点
         第三个参数:数组的元素个数
         */
        //sizeof(points1)/sizeof(points1[0])
        CGContextStrokeLineSegments(ctx, points1, 2);
        
        //2.矩形
        //1)空心
        /*
         第一个参数:上下文
         第二个参数:颜色值
         */
        CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
        //设置线宽
        CGContextSetLineWidth(ctx, 2);
        
        //绘制
        /*
         第一个参数:上下文
         第二个参数:矩形的大小
         */
        CGContextStrokeRect(ctx, CGRectMake(40, 100, 80, 40));
        
        //2)实心矩形
        //设置颜色
        CGContextSetFillColorWithColor(ctx, [UIColor purpleColor].CGColor);
        //设置线宽
        CGContextSetLineWidth(ctx, 4);
        //绘制
        CGContextFillRect(ctx, CGRectMake(160, 100, 80, 40));
        
        //3.椭圆
        //1)空心
        //颜色
        CGContextSetStrokeColorWithColor(ctx, [UIColor greenColor].CGColor);
        //线宽
        CGContextSetLineWidth(ctx, 8);
        //绘制
        /*
         第一个参数:上下文
         第二个参数:椭圆是矩形的内切椭圆
         */
        CGContextStrokeEllipseInRect(ctx, CGRectMake(40, 200, 100, 80));
        
        //2)实心
        //颜色
        CGContextSetFillColorWithColor(ctx, [UIColor cyanColor].CGColor);
        //线宽
        CGContextSetLineWidth(ctx, 2);
        //绘制
        CGContextFillEllipseInRect(ctx, CGRectMake(160, 200, 120, 80));
        
        //4.虚线
        //颜色
        CGContextSetStrokeColorWithColor(ctx, [UIColor brownColor].CGColor);
        //线宽
        CGContextSetLineWidth(ctx, 2);
        
        //设置点线规则
        /*
         第一个参数:上下文
         第二个参数:相位
         第三个参数:点线规则
         第四个参数:数组元素的个数
         */
        const CGFloat dash1[] = {20, 20};
        
        CGContextSetLineDash(ctx, 0, dash1, 2);
        
        //绘制虚线
        const CGPoint points2 [] = {CGPointMake(40, 300),CGPointMake(240, 300)};
        CGContextStrokeLineSegments(ctx, points2, 2);
        
        //设置点线规则
        const CGFloat dash2 [] = {10, 20, 10};
        
        CGContextSetLineDash(ctx, 0, dash2, 3);
        
        //绘制虚线
        const CGPoint points3[] = {CGPointMake(40, 360),CGPointMake(240, 360)};
        CGContextStrokeLineSegments(ctx, points3, 2);
        
        //相位
        const CGFloat dash3 [] = {20, 20};
        CGContextSetLineDash(ctx, 10, dash3, 2);
        
        //绘制虚线
        const CGPoint points4[] = {CGPointMake(40, 420),CGPointMake(240, 420)};
        CGContextStrokeLineSegments(ctx, points4, 2);
    }
    
    

    绘制圆角,正多边形

    #import "ComplexView.h"
    
    @implementation ComplexView
    
    
    -(void)drawRect:(CGRect)rect
    {
        //获取上下文
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        
        [self geneShapex:30 y:80 w:120 h:80 r:20 ctx:ctx];
        
        //
        CGContextStrokePath(ctx);
        
        
        //绘制正多边形
        [self geneComplexShapex:160 y:300 r:80 num:10 ctx:ctx];
        
        CGContextStrokePath(ctx);
        
    }
    
    //绘制圆角矩形
    /*
     @param x:矩形左上角的x坐标
     @param y:矩形左上角的y坐标
     @param w:矩形的宽度
     @param h:矩形的高度
     @param r:弧线的半径
     @param ctx:上下文
     */
    - (void)geneShapex:(CGFloat)x y:(CGFloat)y w:(CGFloat)w h:(CGFloat)h r:(CGFloat)r ctx:(CGContextRef)ctx
    {
        
        //移到起始点
        CGContextMoveToPoint(ctx, x+r, y);
        
        //直线
        CGContextAddLineToPoint(ctx, x+w-r, y);
        //CGContextAddLineToPoint(<#CGContextRef  _Nullable c#>, <#CGFloat x#>, <#CGFloat y#>)
        //弧线
        /*
         第一个参数:上下文
         第二个参数:第一个控制点的横坐标
         第三个参数:第一个控制点的纵坐标
         第四个参数:第二个控制点的横坐标
         第五个参数:第二个控制点的纵坐标
         第六个参数:弧线的半径
         */
        CGContextAddArcToPoint(ctx, x+w, y, x+w, y+r, r);
        
        //直线
        CGContextAddLineToPoint(ctx, x+w, y+h-r);
        //弧线
        CGContextAddArcToPoint(ctx, x+w, y+h, x+w-r, y+h, r);
        
        //直线
        CGContextAddLineToPoint(ctx, x+r, y+h);
        //弧线
        CGContextAddArcToPoint(ctx, x, y+h, x, y+h-r, r);
        
        //直线
        CGContextAddLineToPoint(ctx, x, y+r);
        //弧线
        CGContextAddArcToPoint(ctx, x, y, x+r, y, r);
        
        
    }
    
    
    //绘制正多边形
    /*
     @param x0:多边形的中心点x坐标
     @param y0:多边形的中心点y坐标
     @param r:多边形的外接圆半径
     @param num:几边形
     @param ctx:上下文
     */
    - (void)geneComplexShapex:(CGFloat)x0 y:(CGFloat)y0 r:(CGFloat)r num:(int)num ctx:(CGContextRef)ctx
    {
        CGContextMoveToPoint(ctx, x0+r, y0);
        
        //绘制直线
        for (int i=1; i<=num; i++) {
            
            CGContextAddLineToPoint(ctx, x0+r*cos(2*M_PI*i/num), y0+r*sin(2*M_PI*i/num));
            
        }
    }
    
    
    

    -------------------绘制文字-------------------

    Snip20170918_1.png

    #import "MyView.h"
    
    @implementation MyView
    
    -(void)drawRect:(CGRect)rect
    {
        //绘制文字
        NSString *str = @"hello,text";
        
        CGPoint point1 = CGPointMake(30, 80);
        
        NSDictionary *dict = @{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:20]};
        
        /*
         第一个参数:文字开始绘制的位置
         第二个参数:文字的属性
         */
        [str drawAtPoint:point1 withAttributes:dict];
        
        //绘制空心文字
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        
        //kCGTextStroke表示绘制空心文字
        CGContextSetTextDrawingMode(ctx, kCGTextStroke);
        
        //绘制文字
        NSString *str2 = @"hello, shanghai";
        [str2 drawAtPoint:CGPointMake(30, 120) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:28],NSForegroundColorAttributeName:[UIColor cyanColor]}];
        
        //设置阴影
        /*
         第一个参数:上下文
         第二个参数:阴影的偏移量
         第三个参数:透明度
         第四个参数:阴影的颜色
         */
        CGContextSetShadowWithColor(ctx, CGSizeMake(2, 2), 1, [UIColor blackColor].CGColor);
    
        //绘制文字
        NSString *str3 = @"hello, objective-C";
        [str3 drawAtPoint:CGPointMake(30, 300) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:30],NSForegroundColorAttributeName:[UIColor redColor]}];
        
        
    }
    
    

    绘制UIImage

    - (UIImage *)generateImage
    {
        //开始在内存中绘制图片
        UIGraphicsBeginImageContext(CGSizeMake(300, 400));
          
        //获取上下文
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        
        //直线
        //线宽
        CGContextSetLineWidth(ctx, 2);
        //颜色
        CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
        //绘制
        const CGPoint points[] = {CGPointMake(40, 80),CGPointMake(240, 80)};
        CGContextStrokeLineSegments(ctx, points, 2);
        
        
        //显示资源图片
        UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];
        
        
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 100, 40, 40)];
        imgView.image = [UIImage imageNamed:@"mark"];
        //绘制
        [myView addSubview:imgView];
        
        [myView.layer renderInContext:ctx];
        
        
        //获取图片
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
        
        return image;
    }
    

    相关文章

      网友评论

          本文标题:iOS图形绘制

          本文链接:https://www.haomeiwen.com/subject/nfovsxtx.html