博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS SWIFT基本画图教程
阅读量:4510 次
发布时间:2019-06-08

本文共 3738 字,大约阅读时间需要 12 分钟。

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//角度=弧度除以π再乘以180CGContextSetStrokeColorWithColor(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 UIKitclass 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

转载于:https://www.cnblogs.com/Free-Thinker/p/4946305.html

你可能感兴趣的文章
ubuntu常见错误--Could not get lock /var/lib/dpkg/lock解决
查看>>
Java中BIO,NIO,AIO的理解
查看>>
浅谈Sql各种join的用法
查看>>
Durid数据库连接池配置(不使用框架)
查看>>
BarCode128B字符转换函数(PB,SQL)
查看>>
watir学习资料
查看>>
Jmeter属性和变量
查看>>
java并发编程:并发容器之CopyOnWriteArrayList(转)
查看>>
python基础——面向对象进阶下
查看>>
Linux vi 命令详解
查看>>
本地如何搭建IPv6环境测试你的APP
查看>>
oracle、mysql新增字段,字段存在则不处理
查看>>
CAP原理
查看>>
动态加载主题
查看>>
leetcode[125]Valid Palindrome
查看>>
访问一个绝对地址把一个整型数强制转换 (typecast)为一个指针是合法的
查看>>
iOS项目开发之Socket编程 (转)
查看>>
C++ NULL与nullptr的区别
查看>>
Discretized Streams, 离散化的流数据处理
查看>>
Spark源码分析 – SchedulerBackend
查看>>