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

本文介绍如何使用Swift在iOS应用中绘制基本图形,包括线条、矩形、圆形及图片,并提供了一个简易画板的实现思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天看了一下视频 主要将图形的绘制,首先打开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







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值