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

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

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

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

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

import UIKitclass 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,1)    //绘制        CGContextstrokePath(context)    }    }
运行结果

绘制矩形

import UIKitclass 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,height: 20))            }    }
绘制圆形有两种方法

import UIKitclass 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)        CGContextAddArc(context,50,3.14*2,0)        CGContextFillPath(context)        //第二种方法是画一个椭圆 若在正方形内 为圆 在长方形内 为椭圆        CGContextFillEllipseInRect(context,CGRect(x: 300,y: 300,wIDth: 200,height: 100))         CGContextFillPath(context)    }    }

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

import UIKitclass 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)        //绘制图像 是在一个矩形中进行绘制的        CGContextDrawImage(context,CGRect(x: 0,y: 0,height: 200),image?.CGImage)        CGContextRestoreGState(context)            }    }

简易的画板

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

import UIKitclass 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)     CGPathAddlinetoPoint(path,200)        //添加到context中        CGContextAddpath(context,path)        CGContextSetRGBstrokecolor(context,1)        CGContextstrokePath(context)            }    }
可以通过弄一个画板 在鼠标开始点击的时候 随后在鼠标移动的时候 绘制出路径 这个以后再说 语法不同 又错了 有 setNeedSdisplay 系统认为要重绘 则会执行drawRect 总结

以上是内存溢出为你收集整理的图形的绘制(包括线段 圆形 矩形等) swift编写全部内容,希望文章能够帮你解决图形的绘制(包括线段 圆形 矩形等) swift编写所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1082792.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存