欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Swift - 绘制背景线条

程序员文章站 2024-02-01 11:44:46
效果   // // backgroundlineview.swift // linebackgroundview // // created by youxianm...

效果

Swift - 绘制背景线条

 

//
//  backgroundlineview.swift
//  linebackgroundview
//
//  created by youxianming on 16/8/14.
//  copyright © 2016年 youxianming. all rights reserved.
//

import uikit

// mark: public class : backgroundlineview

class backgroundlineview: uiview {

    // mark: properties.
    
    /// line width, default is 5.
    var linewidth : cgfloat {
    
        get {return backgroundview.linewidth}
        
        set(newval) {
        
            backgroundview.linewidth = newval
            backgroundview.setneedsdisplay()
        }
    }
    
    /// line gap, default is 3.
    var linegap : cgfloat {
        
        get {return backgroundview.linegap}
        
        set(newval) {
            
            backgroundview.linegap = newval
            backgroundview.setneedsdisplay()
        }
    }
    
    /// line color, default is graycolor.
    var linecolor : uicolor {
        
        get {return backgroundview.linecolor}
        
        set(newval) {
            
            backgroundview.linecolor = newval
            backgroundview.setneedsdisplay()
        }
    }
    
    /// rotate value, default is 0.
    var rotate : cgfloat {
    
        get {return backgroundview.rotate}
        
        set(newval) {
        
            backgroundview.rotate = newval
            backgroundview.setneedsdisplay()
        }
    }
    
    convenience init(frame: cgrect, linewidth : cgfloat, linegap : cgfloat, linecolor : uicolor, rotate : cgfloat) {
        
        self.init(frame : frame)
        self.linewidth = linewidth
        self.linegap   = linegap
        self.linecolor = linecolor
        self.rotate    = rotate
    }
    
    // mark: override system method.
    
    override func layoutsubviews() {
        
        super.layoutsubviews()
        setupbackgroundview()
    }
    
    override init(frame: cgrect) {
        
        super.init(frame: frame)
        
        self.layer.maskstobounds = true
        self.addsubview(backgroundview)
    }
    
    required init?(coder adecoder: nscoder) {
        
        fatalerror("init(coder:) has not been implemented")
    }
    
    // mark: private value & func.
    
    private let backgroundview = linebackground(length: 0)
    
    private func setupbackgroundview() {
    
        let drawlength        = sqrt(self.bounds.size.width * self.bounds.size.width + self.bounds.size.height * self.bounds.size.height)
        backgroundview.frame  = cgrectmake(0, 0, drawlength, drawlength)
        backgroundview.center = cgpointmake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0)
        backgroundview.setneedsdisplay()
    }
}

// mark: private class : linebackground

private class linebackground : uiview {

    private var rotate    : cgfloat = 0
    private var linewidth : cgfloat = 5
    private var linegap   : cgfloat = 3
    private var linecolor : uicolor = uicolor.graycolor()
    
    override init(frame: cgrect) {
        
        super.init(frame: frame)
        self.backgroundcolor = uicolor.clearcolor()
    }
    
    required init?(coder adecoder: nscoder) {
        
        fatalerror("init(coder:) has not been implemented")
    }
    
    convenience init(length : cgfloat) {
        
        self.init(frame : cgrectmake(0, 0, length, length))
    }
    
    override func drawrect(rect: cgrect) {
        
        super.drawrect(rect)
        
        if self.bounds.size.width <= 0 || self.bounds.size.height <= 0 {
            
            return
        }
        
        let context      = uigraphicsgetcurrentcontext()
        let width        = self.bounds.size.width
        let height       = self.bounds.size.width
        let drawlength   = sqrt(width * width + height * height)
        let outerx       = (drawlength - width)  / 2.0
        let outery       = (drawlength - height) / 2.0
        let tmplinewidth = linewidth <= 0 ? 5 : linewidth
        let tmplinegap   = linegap   <= 0 ? 3 : linegap
        
        var red   : cgfloat = 0
        var green : cgfloat = 0
        var blue  : cgfloat = 0
        var alpha : cgfloat = 0
        
        cgcontexttranslatectm(context, 0.5 * drawlength, 0.5 * drawlength)
        cgcontextrotatectm(context, rotate)
        cgcontexttranslatectm(context, -0.5 * drawlength, -0.5 * drawlength)
        
        linecolor.getred(&red, green: &green, blue: &blue, alpha: &alpha)
        cgcontextsetrgbfillcolor(context, red, green, blue, alpha)
        
        var currentx = -outerx
        
        while currentx < drawlength {
            
            cgcontextaddrect(context, cgrectmake(currentx, -outery, tmplinewidth, drawlength))
            currentx += tmplinewidth + tmplinegap
        }
        
        cgcontextfillpath(context)
    }
}

 

使用

//
//  viewcontroller.swift
//  linebackgroundview
//
//  created by youxianming on 16/8/14.
//  copyright © 2016年 youxianming. all rights reserved.
//

import uikit

class viewcontroller: uiviewcontroller {

    let tmpview = backgroundlineview(frame: cgrectmake(0, 0, 300, 300), linewidth: 4, linegap: 4,
                                     linecolor: uicolor.blackcolor().colorwithalphacomponent(0.045), rotate: cgfloat(m_pi_4))
    
    override func viewdidload() {
        
        super.viewdidload()

        tmpview.center = self.view.center
        self.view.addsubview(tmpview)
    }
}