其实这是以前做过的一个例子,方便自己参考的代码!希望对大家也有点参考.
首先,建立一个Swift类,继承UIView这个类,然后重写
1
|
func drawRect(rect: CGRect)
|
其次,获取画笔的上下文
1
2
|
var context:CGContextRef = UIGraphicsGetCurrentContext();
//获取画笔上下文
CGContextSetAllowsAntialiasing(context,
true
)
//抗锯齿设置
|
下面我们就可以编写画图形的代码了
1 画点
//画点
1
|
CGContextFillEllipseInRect(context, CGRectMake(75, 75, 50, 50))
|
2 画直线
1
2
3
4
5
|
//画直线
CGContextSetLineWidth(context, 5)
//设置画笔宽度
CGContextMoveToPoint(context, 10, 20);
CGContextAddLineToPoint(context, 100, 100);
CGContextStrokePath(context)
|
3 画圆
1
2
3
4
5
6
7
8
9
|
//画圆
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 画字符串
1
|
//画字符串var str:NSString = "我是吴统威";str.drawAtPoint(CGPointMake(100, 200), withAttributes: nil);
|
5 画图片
1
|
//画图片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画图
1
2
3
4
5
|
//使用path画图 let p1:CGMutablePathRef = CGPathCreateMutable();
CGPathMoveToPoint(p1, nil, 50, 250)
CGPathAddLineToPoint(p1, nil, 50, 350)
CGContextAddPath(context, p1)
CGContextStrokePath(context)
//关闭路径
|
这里主要是介绍基本的用法,其他复杂的图形,可以参照API文档,去调用相关的方法,画出自己想要的图形
附完整代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
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)
//关闭路径
}
}
|
效果图
本文属于吴统威的博客原创文章,转载时请注明出处及相应链接:http://www.wutongwei.com/front/infor_showone.tweb?id=85