当前位置: 移动技术网 > IT编程>移动开发>IOS > Swift - 绘制背景线条

Swift - 绘制背景线条

2018年09月17日  | 移动技术网IT编程  | 我要评论

红白机游戏,湖南湘潭,易建联再得一子

效果

\

 

//
//  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)
    }
}

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网