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

前端之家收集整理的这篇文章主要介绍了图形的绘制(包括线段 圆形 矩形等) swift编写前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

  1. import UIKit
  2.  
  3. class myView: UIView {
  4.  
  5. // Only override drawRect: if you perform custom drawing.
  6. // An empty implementation adversely affects performance during animation.
  7. override func drawRect(rect: CGRect) {
  8. //拿到上下文的绘制环境 类似于canvas中的context
  9. var context = UIGraphicsGetCurrentContext()
  10. //随后在里面画线段 进行绘制 先绘制 在填充或描边 就是stroke 移到这点 重新开始另一点的路径也是这个函数
  11. CGContextMoveToPoint(context,100,100)
  12. //再移动到这点
  13. CGContextAddLineToPoint(context,200,200)
  14. //设置属性 例如线段宽度 颜色等
  15. CGContextSetLineWidth(context,20.0)
  16. CGContextSetRGBStrokeColor(context,1,1)
  17. //绘制
  18. CGContextStrokePath(context)
  19. }
  20.  
  21. }
运行结果

绘制矩形

  1. import UIKit
  2.  
  3. class rect: UIView {
  4.  
  5. // Only override drawRect: if you perform custom drawing.
  6. // An empty implementation adversely affects performance during animation.
  7. override func drawRect(rect: CGRect) {
  8. var context = UIGraphicsGetCurrentContext()
  9. //进行矩形的绘制 和填充
  10. CGContextFillRect(context,CGRect(x: 100,y: 100,width: 20,height: 20))
  11. //描边的矩形 在绘制之前都可以进行填充
  12. CGContextStrokeRect(context,height: 20))
  13. }
  14.  
  15. }
绘制圆形有两种方法

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

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

  1. import UIKit
  2.  
  3. class myView: UIView {
  4. //加载图片 指定图片的路径 为CGimage图像
  5. var image = UIImage(named: "2.jpg")
  6. // Only override drawRect: if you perform custom drawing.
  7. // An empty implementation adversely affects performance during animation.
  8. override func drawRect(rect: CGRect) {
  9. var context = UIGraphicsGetCurrentContext()
  10. //最好先进行保存当前的状态 否则当绘制其他东西时,会对当前的状态产生影响
  11. CGContextSaveGState(context)
  12. //因为是cgimage 所以先要进行坐标的变化 坐标的不同 因此若不变化的话 图像是反的
  13. CGContextTranslateCTM(context,10,400)
  14. CGContextScaleCTM(context,-1)
  15. //绘制图像 是在一个矩形中进行绘制的
  16. CGContextDrawImage(context,CGRect(x: 0,y: 0,height: 200),image?.CGImage)
  17. CGContextRestoreGState(context)
  18. }
  19.  
  20. }

简易的画板

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

  1. import UIKit
  2.  
  3. class myView: UIView {
  4.  
  5. // Only override drawRect: if you perform custom drawing.
  6. // An empty implementation adversely affects performance during animation.
  7. override func drawRect(rect: CGRect) {
  8. var context = UIGraphicsGetCurrentContext()
  9. //创建path
  10. var path = CGPathCreateMutable()
  11. //将path移动到某个点 nil表示path没有经过变化
  12. CGPathMoveToPoint(path,nil,100)
  13. CGPathAddLineToPoint(path,200)
  14. //添加到context中
  15. CGContextAddPath(context,path)
  16. CGContextSetRGBStrokeColor(context,1)
  17. CGContextStrokePath(context)
  18. }
  19.  
  20. }
可以通过弄一个画板 在鼠标开始点击的时候 随后在鼠标移动的时候 绘制出路径 这个以后再说 语法不同 又错了 有 setNeedSDisplay 系统认为要重绘 则会执行drawRect

猜你在找的Swift相关文章