绘画

工程目录

这里都使用Storyboard,方便而又快捷.

绘画的相关介绍

 /*1 CGContextMoveToPoint 开始画线2 CGContextAddLineToPoint 画直线3 CGContextAddEllipseInRect 画一椭圆4 CGContextSetLineCap 设置线条终点形状5 CGContextSetLineDash 画虚线6 CGContextAddRect 画一方框7 CGContextStrokeRect 指定矩形8 CGContextStrokeRectWithWidth 指定矩形线宽度9 CGContextStrokeLineSegments 一些直线10 CGContextAddArc 画已曲线 前俩店为中心 中间俩店为起始弧度 最后一数据为0则顺时针画 1则逆时针11 CGContextAddArcToPoint(context,0,0, 2, 9, 40);//先画俩条线从point 到 弟1点 , 从弟1点到弟2点的线  切割里面的圆12 CGContextSetShadowWithColor 设置阴影13 CGContextSetRGBFillColor 填充颜色14 CGContextSetRGBStrokeColor 画笔颜色设置15 CGContextSetFillColorSpace 颜色空间填充16 CGConextSetStrokeColorSpace 颜色空间画笔设置17 CGContextFillRect 补充当前填充颜色的rect18 CGContextSetAlaha 透明度19 CGContextTranslateCTM 改变画布位置20 CGContextSetLineWidth 设置线的宽度21 CGContextAddRects 画多个线22 CGContextAddQuadCurveToPoint 画曲线23 CGContextStrokePath 开始绘制图片24 CGContextDrawPath 设置绘制模式25 CGContextClosePath 封闭当前线路26 CGContextTranslateCTM(context, 0, rect.size.height);    CGContextScaleCTM(context, 1.0, -1.0);反转画布27 CGContextSetInterpolationQuality 背景内置颜色质量等级28 CGImageCreateWithImageInRect 从原图片中取小图*/

绘画线条

//
//  DrawLinesView.swift
//  Study_DrawShape
//
//  Created by 周文春 on 16/3/17.
//  Copyright © 2016年 周文春. All rights reserved.
//import UIKit

class DrawLinesView: UIView {override init(frame: CGRect) {super.init(frame: frame)}required init?(coder aDecoder: NSCoder) {super.init(coder: aDecoder)}override func drawRect(rect: CGRect) {let context = UIGraphicsGetCurrentContext()//表示从哪个起点开始画CGContextMoveToPoint(context, 100, 100)//向下开始画线(表示画直线)CGContextAddLineToPoint(context, 100, 200)//向右开始画线CGContextAddLineToPoint(context, 200, 200)CGContextMoveToPoint(context, 100, 300)CGContextAddLineToPoint(context, 100, 400)CGContextAddLineToPoint(context, 200, 500)//设置所画线条的颜色CGContextSetRGBStrokeColor(context, 1, 0, 1, 1)//设置线条的宽度是多少CGContextSetLineWidth(context, 3)//开始绘制图CGContextStrokePath(context)}
}

绘画矩形

//
//  DrawRectView.swift
//  Study_DrawShape
//
//  Created by 周文春 on 16/3/17.
//  Copyright © 2016年 周文春. All rights reserved.
//import UIKitclass DrawRectView: UIView {override init(frame: CGRect) {super.init(frame: frame)}required init?(coder aDecoder: NSCoder) {super.init(coder: aDecoder)}// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.override func drawRect(rect: CGRect) {// Drawing code//创建一个画布,用于将我们所画的东西在这个上面展示出来let context = UIGraphicsGetCurrentContext()//画一个矩形CGContextAddRect(context, CGRect(x: 100, y: 100, width: 100, height: 100))CGContextSetRGBFillColor(context, 1, 0, 0, 1)CGContextFillPath(context)//边框宽度CGContextSetLineWidth(context, 2)//边框颜色CGContextSetRGBStrokeColor(context, 0, 1, 0, 1)CGContextStrokeRect(context, CGRect(x: 100, y: 100, width: 100, height: 100))}}

绘画圆

//
//  DrawCircleView.swift
//  Study_DrawShape
//
//  Created by 周文春 on 16/3/17.
//  Copyright © 2016年 周文春. All rights reserved.
//import UIKitclass DrawCircleView: UIView {required init?(coder aDecoder: NSCoder) {super.init(coder: aDecoder)}// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.override func drawRect(rect: CGRect) {let context = UIGraphicsGetCurrentContext()/***  绘制圆形**  @param context 画布*  @param 150     弧心距离X轴的距离*  @param 300     弧心距离Y轴的距离*  @param 100     弧半径*  @param 0       起始角度*  @param 3.14    结束角度*  @param 0       顺时针还是逆时针;0表示顺时针,1表示逆时针**  @return <#return value description#>*///第一个参数表示我们我们所在的画布,第二和第三个参数表示开始的点在父视图的什么位置,第四个参数表示弧的半径有多大,第五个参数表示起始的角度,第六个参数表示结束的角度(3.14表示半圆),最后一个参数表示顺时针还是逆时针,0表示顺时针,1表示逆时针//填充色CGContextAddArc(context, 150, 200, 100, 0, 3.141592653*2, 0)CGContextSetRGBFillColor(context, 1, 0, 0, 1)CGContextFillPath(context)//第一种方式绘制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: 200))CGContextStrokePath(context)}}

绘画图片

//
//  DrawImageView.swift
//  Study_DrawShape
//
//  Created by 周文春 on 16/3/17.
//  Copyright © 2016年 周文春. All rights reserved.
//import UIKitclass DrawImageView: UIView {var uiImage: CGImageRef?required init?(coder aDecoder: NSCoder) {super.init(coder: aDecoder)uiImage = UIImage(named: "101.jpg")?.CGImage}// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.override func drawRect(rect: CGRect) {let context = UIGraphicsGetCurrentContext()//保存最初始的一个状态CGContextSaveGState(context)//移动到某个位置CGContextTranslateCTM(context, 10, 400)//调整缩放CGContextScaleCTM(context, 1, -1)//如果不调整坐标的话,我们会发现图片显示出来是反着的,因为CGImage和图片的坐标是反着的,上面两行为调整代码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))}}

不调整坐标的话效果如下:

不添加状态,再次绘其他图的话效果如下:

绘制画板

//
//  DrawingBoardView.swift
//  Study_DrawShape
//
//  Created by 周文春 on 16/3/17.
//  Copyright © 2016年 周文春. All rights reserved.
//import UIKit

class DrawingBoardView: UIView {required init?(coder aDecoder: NSCoder) {super.init(coder: aDecoder)}var path = CGPathCreateMutable()override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {for touch: AnyObject in touches {let p: UITouch = touch as! UITouchCGPathMoveToPoint(path, nil, p.locationInView(self).x, p.locationInView(self).y)print(p.locationInView(self).x, p.locationInView(self).y)}}override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {for touch: AnyObject in touches {let p: UITouch = touch as! UITouchCGPathAddLineToPoint(path, nil, p.locationInView(self).x, p.locationInView(self).y)//重绘print(p.locationInView(self).x, p.locationInView(self).y)setNeedsDisplay()}}// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.override func drawRect(rect: CGRect) {// Drawing codelet context = UIGraphicsGetCurrentContext()CGContextAddPath(context, path)CGContextStrokePath(context)}}

可以自己画图:

整体效果图如下:

Swift 实践之绘画相关推荐

  1. A站 的 Swift 实践 —— 上篇

    背景介绍 AcFun俗称为"A站",作为一款二次元内容社区产品,以"认真你就输了"为文化导向,倡导轻松欢快的亚文化.AcFun涵盖了中长视频,小视频,番剧,文章 ...

  2. Swift 首次调试断点慢的问题解法 | 优酷 Swift 实践

    作者:段继统 & 夏磊 调试断点是与开发体验关系最为密切点之一,优酷iOS团队在外部调研时候发现,大量国内的iOS APP研发团队也遇到了类似的问题.考虑到国内Swift如火如荼的现状,我们尽 ...

  3. Swift实践:使用CoreData存储多种数据类的通讯录

    上一篇文章简单的实现了一个通讯录,说是通讯录实际上就只是一个简简单单的Name List.这次我们要往这个通讯录里面加入更多的元素,目的也是为了学习CoreData如何存储更多的数据类型. 完成后的效 ...

  4. 拥抱Swift!优酷Mac迁移Swift实践

    作者丨阿里文娱高级无线开发工程师 大斗 不管从"明里"还是"暗里"来看,苹果都在大力推荐使用 Swift 这一门语言.作为苹果的"亲儿子", ...

  5. Flutter有局限,拥抱Swift!优酷Mac迁移Swift实践

    Python实战社群 Java实战社群 长按识别下方二维码,按需求添加 扫码关注添加客服 进Python社群▲ 扫码关注添加客服 进Java社群▲ 作者丨阿里文娱高级无线开发工程师 大斗 来源 | I ...

  6. swift 实践- 10 -- UIProgressView

    import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa ...

  7. file input 移动端选择文件夹_免费 |《MNN For Swift》移动端机器学习实战课程

    自 2019 年 4 月在 Github 开源以来,淘系技术部-端智能团队自研的 MNN 推理引擎,因为其高性能.易用性以及优秀兼容性受到不少开发者的支持和喜爱.我们也把这份支持化作不断前进的动力,仅 ...

  8. 计算机课外活动小结,课外活动总结

    课外活动总结 课外活动总结(一): 本年度我校的课外活动兴趣小组,在全校师生的共同努力下开展得有声有色,成为孩子们眼中色彩斑斓的万花筒.第二课堂活动能够有效的增强学生的学习兴趣和欲望,增强学生的文化意 ...

  9. 5月 CSDN 创作者之夜:获奖名单公布

    本期获奖名单从多个角度进行评选,有新人奖.实力新星奖.月度贡献奖.月度文章精选奖等等,具体的奖项以及评选规则可参考下表: 奖项名称 奖项评选规则 数量 现金奖励 持之以恒奖 第三期创作打卡连续四周完成 ...

最新文章

  1. SCCM 2012 Part 2 部署前AD准备
  2. thinkphp笔记
  3. iOS性能优化 启动
  4. xml发生错误_WEB之web.xml详解
  5. Oracle 原理: 初步认识程序包
  6. 连载:阿里巴巴大数据实践—数据服务
  7. 2017年初随想——几个小目标
  8. jquery datatable搜索框添加按钮,改变keypress搜索为点击按钮搜索
  9. PyTorch | torch.full()使用方法 | torch.full()如何使用? torch.full()例子说明 | 通过torch.full创建全相同的张量
  10. linux 根分区分的太大了,linux根分区满了如何处理,查找大文件方法
  11. 【渝粤教育】国家开放大学2018年秋季 1018t国际公法 参考试题
  12. 大屏样式(全屏禁止滚动)
  13. 【转】:localStorage使用总结
  14. java windows 中文乱码问题_JAVA中文乱码之解决方案
  15. Hook技术看这篇就够了
  16. 小米手机miui12系统usb共享网络
  17. matlab怎么显示bfm模型的纹理模型,【计算机视觉基础】如何通过BFM模型得到人脸关键特征点的信息...
  18. Python3 post请求上传文件
  19. 杨森翔书法:立马越王台
  20. 计算机表格标题怎么做,做表必备!超实用的五个制作Excel表头的技巧,快速学起来...

热门文章

  1. 使用word文档插入代码方式
  2. 磁盘中压缩出可用空间
  3. 09 jQuery事件委托小米购物车
  4. OSS 阿里云存储操作实例以及错误案例
  5. 【视频】互联网寒冬,经历裁员,拿20W被迫去大厂
  6. 攻防世界-Misc-功夫再高也怕菜刀-菜狗决定用菜刀和菜鸡决一死战(思路清晰,好操作,详细)
  7. ubuntu文件夹映射至win10网络盘符
  8. 考研计算机科学与技术属于理学么,计算机科学与技术是属于工学类还是理学类?...
  9. 给联想Thinkpad E480 安装了Ubuntu 18.04 Wifi适配器不可用的处理方法
  10. C语言中函数的声明与数组的声明