您的位置:首页 > 移动开发 > Swift

图形的绘制(包括线段 圆形 矩形等) swift编写

2015-11-13 18:15 483 查看
今天看了一下视频 主要将图形的绘制,首先打开main.storyboard 在其中选择view 为其添加文件myview 继承自uiview 随后为view关连文件 因为是在main.storyboard中加载的所以必须有一下 代码 但好像最新的版本已经省略了



随后在drawrect中进行绘制 主代码如下

import UIKit

class myView: UIView {

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
//拿到上下文的绘制环境 类似于canvas中的context
var context = UIGraphicsGetCurrentContext()
//随后在里面画线段 进行绘制 先绘制 在填充或描边 就是stroke 移到这点 重新开始另一点的路径也是这个函数
CGContextMoveToPoint(context, 100, 100)
//再移动到这点
CGContextAddLineToPoint(context, 200, 200)
//设置属性 例如线段宽度 颜色等
CGContextSetLineWidth(context, 20.0)
CGContextSetRGBStrokeColor(context, 1, 0, 1, 1)
//绘制
CGContextStrokePath(context)
}

}
运行结果



绘制矩形

import UIKit

class rect: UIView {

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
var context = UIGraphicsGetCurrentContext()
//进行矩形的绘制 和填充
CGContextFillRect(context, CGRect(x: 100, y: 100, width: 20, height: 20))
//描边的矩形 在绘制之前都可以进行填充
CGContextStrokeRect(context, CGRect(x: 100, y: 100, width: 20, height: 20))

}

}
绘制圆形有两种方法

import UIKit

class myView: UIView {

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
//绘制圆形的第一种方法 5个参数 为上下文环境 x y坐标 半径 起始弧度 还有结束的弧度 还有0顺时针 1逆时针
var context = UIGraphicsGetCurrentContext()
CGContextSetRGBFillColor(context, 1, 0, 1, 1)
CGContextAddArc(context, 100, 100, 50, 0, 3.14*2, 0)
CGContextFillPath(context)
//第二种方法是画一个椭圆 若在正方形内 为圆 在长方形内 为椭圆
CGContextFillEllipseInRect(context, CGRect(x: 300, y: 300, width: 200, height: 100))
CGContextFillPath(context)
}

}



绘制图片 把图片放到文件夹中 来拷贝

import UIKit

class myView: UIView {
//加载图片 指定图片的路径 为CGimage图像
var image = UIImage(named: "2.jpg")

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {

var context = UIGraphicsGetCurrentContext()
//最好先进行保存当前的状态 否则当绘制其他东西时,会对当前的状态产生影响
CGContextSaveGState(context)
//因为是cgimage 所以先要进行坐标的变化 坐标的不同 因此若不变化的话 图像是反的
CGContextTranslateCTM(context, 10, 400)
CGContextScaleCTM(context, 1, -1)
//绘制图像 是在一个矩形中进行绘制的
CGContextDrawImage(context, CGRect(x: 0, y: 0, width: 200, height: 200),image?.CGImage)
CGContextRestoreGState(context)

}

}



简易的画板

还有另外一种绘制的方法 是通过path 来进行的 把所有要的图形都加载到path 中,随后绘制path即可 线段要用stroke

import UIKit

class myView: UIView {

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {

var context = UIGraphicsGetCurrentContext()
//创建path
var path = CGPathCreateMutable()
//将path移动到某个点 nil表示path没有经过变化
CGPathMoveToPoint(path, nil, 100, 100)
CGPathAddLineToPoint(path, nil, 200, 200)
//添加到context中
CGContextAddPath(context, path)
CGContextSetRGBStrokeColor(context, 1, 1, 0, 1)
CGContextStrokePath(context)

}

}


可以通过弄一个画板 在鼠标开始点击的时候 随后在鼠标移动的时候 绘制出路径 这个以后再说 语法不同 又错了 有 setNeedSDisplay 系统认为要重绘 则会执行drawRect
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: