ios 菜鸟之绘制线段以及矩形边框

140 阅读1分钟
class CusView : UIView {
    
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        //获取ctx
        let ctx = UIGraphicsGetCurrentContext()
        //设置线宽
        ctx?.setLineWidth(10)
        ctx?.setStrokeColor(UIColor.blue.cgColor)

//        let points  = [CGPoint.init(x: 10, y: 40),CGPoint.init(x: 10, y: 100),CGPoint.init(x: 10, y: 100),CGPoint.init(x: 100, y: 100)]
//
//        //这个绘制是一段一段的绘制 所以points应该是成对的数组
//        ctx?.strokeLineSegments(between: points)
        
        //设置拐角的形状
//        ctx?.setLineCap(.round)
//        let points1  = [CGPoint.init(x: 100, y: 40),CGPoint.init(x: 100, y: 100),CGPoint.init(x: 100, y: 100),CGPoint.init(x: 200, y: 50)]
//        ctx?.strokeLineSegments(between: points1)
        
        //lengths 代表的就是实线跟虚线交替出现的规律
        //phase 影响的是第一个实线开始的位置
//        ctx?.setLineDash(phase: 50, lengths: [50,40])
//
//        ctx?.setLineCap(.round)
//        let points1  = [CGPoint.init(x: 0, y: 100),CGPoint.init(x: 500, y: 100)]
//        ctx?.strokeLineSegments(between: points1)
        
        //绘制一个矩形
//        ctx?.setFillColor(UIColor.blue.cgColor)
//        ctx?.fill(CGRect.init(x: 100, y: 0, width: 100, height: 100))
        
        //绘制矩形边框 注意这里的Y不能为0 会出现展示不全的情况
        ctx?.setStrokeColor(UIColor.red.cgColor)
        ctx?.stroke(CGRect.init(x: 100, y: 100, width: 100, height: 100))
    }
}

加油.png