iOS绘图API的使用
来源:互联网 发布:vscode spring boot 编辑:程序博客网 时间:2024/06/05 02:35
常用的绘图API
- UIGraphicsGetCurrentContext() 初始化
- CGContextMoveToPoint(c: CGContext, x: CGFloat, y: CGFloat) 移动到点
- CGContextAddLineToPoint(c: CGContext!, x: CGFloat, y: CGFloat)添加一条线
- CGContextAddRect(c: CGContext, rect: CGRect)添加一个矩形
- CGContextAddArc(c: CGContext!, x: CGFloat, y: CGFloat, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Int32) 添加弧形
- CGContextAddEllipseInRect(context: CGContext!, rect: CGRect) 添加椭圆
CGContextAddPath(context:CGContext!, path:CGPath!) 添加一个Path
- CGContextSetRGBStrokeColor(context:CGContext!, red:CGFloat, green:CGFloat, blue:CGFloat, alpha:CGFloat) 设置描边颜色
- CGContextSetRGBFillColor(context:CGContext!, red:CGFloat, green:CGFloat, blue:CGFloat, alpha:CGFloat) 设置填充颜色
- CGContextSetLineWidth(c:CGContext!, width:CGFloat) 设置线宽
- CGContextStrokePath(c:CGContext!) 对线条描边
- CGContextFillPath(c:CGContext!) 填充
- CGContextSaveGState(c:CGContext!) 保存改变前的状态
- CGContextRestoreGState(c:CGContext!)恢复改变前的状态
- CGContextTranslateCTM(c:CGContext!, tx:CGFloat, ty:CGFloat) 更改坐标系的原点
- CGContextScaleCTM(c:CGContext!, sx:CGFloat, sy:CGFloat) 反转Y轴
- CGContextDrawImage(c:CGContext!, rect:CGRect, image:CGImage!) 绘制图片
- CGRect(x: Int, y: Int, width: Int, height: Int)
绘制线条
import UIKitclass MyView: UIView { override init(frame: CGRect) { super.init(frame: frame) } required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override func drawRect(rect: CGRect) { var context = UIGraphicsGetCurrentContext() // 移动到点 CGContextMoveToPoint(context, 100, 200) // 添加线条 CGContextAddLineToPoint(context, 100, 200) CGContextAddLineToPoint(context, 200, 200) // 移动 CGContextMoveToPoint(context, 100, 300) CGContextAddLineToPoint(context, 100, 400) CGContextAddLineToPoint(context, 200, 500) // 设置颜色 CGContextSetRGBStrokeColor(context, 1, 0, 0, 1) // 设置线宽 CGContextSetLineWidth(context, 5) // 画线条 CGContextStrokePath(context) }}
绘制矩形
CGContextSetLineWidth(context, 5) // 设置线条宽度 CGContextSetRGBStrokeColor(context, 0, 1, 0, 1) //设置描边颜色 // 给矩形描边 CGContextStrokeRect(context, CGRect(x: 100, y: 100, width: 100, height: 100))
override func drawRect(rect: CGRect) { var context = UIGraphicsGetCurrentContext() // 添加矩形 CGContextAddRect(context, CGRect(x: 100, y: 100, width: 100, height: 100)) // 设置RGB填充颜色 CGContextSetRGBFillColor(context, 1, 0, 0, 1) CGContextFillPath(context) // 填充Path CGContextSetLineWidth(context, 5) // 设置线条宽度 CGContextSetRGBStrokeColor(context, 0, 1, 0, 1) //设置描边颜色 // 给矩形描边 CGContextStrokeRect(context, CGRect(x: 100, y: 100, width: 100, height: 100))
绘制圆形
override func drawRect(rect: CGRect) { var context = UIGraphicsGetCurrentContext() // 添加一个弧形 CGContextAddArc(context, 150, 200, 100, 0, 3.141592653*2, 0) CGContextSetLineWidth(context, 10) CGContextStrokePath(context) // 添加一个椭圆 CGContextAddEllipseInRect(context, CGRect(x: 50, y: 400, width: 200, height: 100)) CGContextStrokePath(context) }
绘制图片
override func drawRect(rect: CGRect) { var context = UIGraphicsGetCurrentContext() CGContextSaveGState(context) // 保存改变前的状态 CGContextTranslateCTM(context, 10, 400) // 更改坐标系的原点 CGContextScaleCTM(context, 1, -1) // 反转Y轴 CGContextDrawImage(context, CGRect(x: 0, y: 0, width: 200, height: 200), uiImage) CGContextRestoreGState(context) // 恢复改变前的状态 CGContextStrokeRect(context, CGRect(x: 50, y: 100, width: 100, height: 100)) }
简易画板
// 手指按下 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { // 获取按下的点的坐标 var p = touches.anyObject()!.locationInView(self) // 移动到点 CGPathMoveToPoint(path, nil, p.x, p.y) } // 手指移动 override func touchesMoved(touches: NSSet, withEvent event: UIEvent) { // 获取按下的点的坐标 var p = touches.anyObject()!.locationInView(self) CGPathAddLineToPoint(path, nil, p.x, p.y) setNeedsDisplay() } var path = CGPathCreateMutable() override func drawRect(rect: CGRect) { var context = UIGraphicsGetCurrentContext() // 将先前创建的Path添加到context中 CGContextAddPath(context, path) // 将Path描画出来 CGContextStrokePath(context) }
0 0
- iOS绘图API的使用
- iOS- 绘图API
- 使用的iOS绘图方法
- iOS 绘图之API总结
- iOS绘图-UIBezierPath的使用
- iOS-绘图(Quartz2D)的简单使用(原创)
- IOS绘图使用
- 使用绘图API自定义组件
- ios私有API的使用
- IOS私有API的使用
- IOS绘图API绘制直线几何图形
- iOS的绘图机制
- iOS的绘图机制
- iOS的绘图机制
- iOS 基本的绘图
- ios-绘图的方式
- iOS的绘图方法使用和参数的说明
- iOS 绘图时使用 UIGraphicsGetCurrentContext() 报错的原因分析
- 优化算法的比较,选择
- hive优化
- 【@property的特性】
- Hibernate单向关联1-N
- 【暑期基础2】O HDU 2040 亲和数
- iOS绘图API的使用
- spring配置中mappingResources和mappingLocations
- mysql左连和右连
- hdoj 5289 Assignment 【RMQ + 二分查找区间最优长度】
- mysql 连接url中useUnicode=true&characterEncoding=UTF-8 的作用
- 字典的常用方法(含可变字典)
- mysql 连接url中useUnicode=true&characterEncoding=UTF-8 的作用
- Android UI学习之ListView(使用BaseAdapter)
- Windows Git SSH keys: