zoukankan      html  css  js  c++  java
  • IOS SWIFT基本画图教程

    OS SWIFT基本画图教程

    其实这是以前做过的一个例子,方便自己参考的代码!希望对大家也有点参考.

    首先,建立一个Swift类,继承UIView这个类,然后重写

    func drawRect(rect: CGRect)

    其次,获取画笔的上下文

    var context:CGContextRef =  UIGraphicsGetCurrentContext();//获取画笔上下文
    
    CGContextSetAllowsAntialiasing(context, true) //抗锯齿设置


    下面我们就可以编写画图形的代码了

    1 画点

    //画点

    CGContextFillEllipseInRect(context, CGRectMake(75, 75, 50, 50))

        
    2 画直线
            

    //画直线
    CGContextSetLineWidth(context, 5) //设置画笔宽度
    CGContextMoveToPoint(context, 10, 20);        
    CGContextAddLineToPoint(context, 100, 100);        
    CGContextStrokePath(context)

    3 画圆

     //画圆
    CGContextAddEllipseInRect(context, CGRectMake(50,50,100,100)); //画圆
    CGContextStrokePath(context) //关闭路径
            
    //通过画弧画圆
    //弧度=角度乘以π后再除以180
    //角度=弧度除以π再乘以180
    CGContextSetStrokeColorWithColor(context, UIColor.redColor().CGColor) //设置画笔颜色
    CGContextAddArc(context, 100, 100, 50, 0, CGFloat(270*M_PI/180), 0) //画弧
    CGContextStrokePath(context)//关闭路径

            
            
    4 画字符串

    //画字符串
    var str:NSString = "我是吴统威";
    str.drawAtPoint(CGPointMake(100, 200), withAttributes: nil);

            
    5 画图片

     //画图片
    CGContextSetShadow(context, CGSizeMake(3, 3),10)
    var img:UIImage = UIImage(named: "8")!;
    img.drawAtPoint(CGPointMake(50, 250));
    CGContextSetShadow(context, CGSizeMake(0, 0), 0)

         

    6 使用PATH画图 

    //使用path画图
     let p1:CGMutablePathRef = CGPathCreateMutable();
     CGPathMoveToPoint(p1, nil, 50, 250)
     CGPathAddLineToPoint(p1, nil, 50, 350)
     CGContextAddPath(context, p1)
     CGContextStrokePath(context)//关闭路径

    这里主要是介绍基本的用法,其他复杂的图形,可以参照API文档,去调用相关的方法,画出自己想要的图形

    附完整代码:

    //
    //  DrawingView.swift
    //  study
    //
    //  Created by Tonway on 15/2/2.
    //  Copyright (c) 2015年 Tonway. All rights reserved.
    //
    
    import UIKit
    
    class DrawingView: UIView {
        
        
        // Only override drawRect: if you perform custom drawing.
        // An empty implementation adversely affects performance during animation.
        override func drawRect(rect: CGRect) {
            // Drawing code
            
            var context:CGContextRef =  UIGraphicsGetCurrentContext();//获取画笔上下文
            
            
            CGContextSetAllowsAntialiasing(context, true) //抗锯齿设置
            
            //画点
            //CGContextSetLineWidth(context, 50);
            CGContextFillEllipseInRect(context, CGRectMake(75, 75, 50, 50))
            
            CGContextSetLineWidth(context, 5) //设置画笔宽度
            
            //画直线
            CGContextMoveToPoint(context, 10, 20);
            CGContextAddLineToPoint(context, 100, 100);
            CGContextStrokePath(context)
            
            //画圆
            CGContextAddEllipseInRect(context, CGRectMake(50,50,100,100)); //画圆
            
            CGContextStrokePath(context) //关闭路径
            
            
            //通过画弧画圆
            //弧度=角度乘以π后再除以180
            //角度=弧度除以π再乘以180
            CGContextSetStrokeColorWithColor(context, UIColor.redColor().CGColor) //设置画笔颜色
            
            CGContextAddArc(context, 100, 100, 50, 0, CGFloat(270*M_PI/180), 0) //画弧
            
            
            CGContextStrokePath(context)//关闭路径
            
            
            
            //画字符串
            var str:NSString = "我是吴统威";
            
            str.drawAtPoint(CGPointMake(100, 200), withAttributes: nil);
            
            //画图片
            
            CGContextSetShadow(context, CGSizeMake(3, 3),10)
            
            var img:UIImage = UIImage(named: "8")!;
            
            img.drawAtPoint(CGPointMake(50, 250));
            
            
            // CGContextDrawImage(context, CGRectMake(100, 250, 100, 100),img.CGImage)
            
            CGContextSetShadow(context, CGSizeMake(0, 0), 0)
            
            //使用path画图
            let p1:CGMutablePathRef = CGPathCreateMutable();
            
            CGPathMoveToPoint(p1, nil, 50, 250)
            
            CGPathAddLineToPoint(p1, nil, 50, 350)
            
            CGContextAddPath(context, p1)
            CGContextStrokePath(context)//关闭路径
            
            
            
            
            
            
        }
        
        
    }

    效果图

    swift绘图教程

    本文属于吴统威的博客原创文章,转载时请注明出处及相应链接:http://www.wutongwei.com/front/infor_showone.tweb?id=85

  • 相关阅读:
    Java vs Python
    Compiled Language vs Scripting Language
    445. Add Two Numbers II
    213. House Robber II
    198. House Robber
    276. Paint Fence
    77. Combinations
    54. Spiral Matrix
    82. Remove Duplicates from Sorted List II
    80. Remove Duplicates from Sorted Array II
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4946305.html
Copyright © 2011-2022 走看看