添加视图到我用于绘制的UIView(rect :)

时间:2017-06-26 18:10:12

标签: swift uiview

我试图制作自己的图表,所以我打开了我的游乐场并开始打字!

所以我已经学会了如何使用,绘制(),到目前为止很酷。但现在,我试图在其上添加一些文字。所以这是我的代码:

import UIKit

class drawGraphMonthly: UIView {

var arrayOfDataPerMonth: [Int]?
var months: [String]?
var goldenRatioForData: CGFloat?
var goldenRationForMonth: CGFloat?
var monthStackView: UIStackView?

init(frame: CGRect, arrayOfDataPerMonth: [Int]) {
    super.init(frame: frame)
    self.arrayOfDataPerMonth = arrayOfDataPerMonth
    self.goldenRatioForData = (frame.size.height / 2) / CGFloat((self.arrayOfDataPerMonth!.max())!)
    self.goldenRationForMonth = frame.size.width / CGFloat((self.arrayOfDataPerMonth?.count)!)
    self.months = ["J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"]
    self.monthStackView = {
        let sv = UIStackView()
        sv.translatesAutoresizingMaskIntoConstraints = false
        sv.backgroundColor = .red
        sv.distribution = .fillEqually
        sv.axis = .horizontal
        for month in self.months! {
            let monthLabel: UILabel = {
                let label = UILabel()
                label.text = month
                return label
            }()
            sv.addArrangedSubview(monthLabel)
        }

        return sv
    }()

    self.addSubview(monthStackView!)
    monthStackView?.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
    monthStackView?.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
    monthStackView?.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
    monthStackView?.heightAnchor.constraint(equalToConstant: 30).isActive = true
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func draw(_ rect: CGRect) {

    // On dessine le background
    let backgroundView = UIBezierPath(rect: CGRect(x: frame.size.width, y: frame.size.height, width: 1, height: 1))
    let secondPoint = CGPoint(x: 0, y: frame.size.height)
    let thirdPointData = getY(y: (arrayOfDataPerMonth?[(arrayOfDataPerMonth?.count)! - 1])!)
    let thirdPoint = CGPoint(x: 0, y: thirdPointData)

    backgroundView.addLine(to: secondPoint)
    backgroundView.addLine(to: thirdPoint)
    var countBG = 1

    while countBG <= (arrayOfDataPerMonth?.count)! {

        let x = CGFloat(countBG) * goldenRationForMonth!
        let y = getY(y: (arrayOfDataPerMonth?[countBG - 1])!)
        let point = CGPoint(x: x, y: y)
        backgroundView.addLine(to: point)
        countBG += 1
    }
    UIColor.white.setFill()
    backgroundView.fill()

    // On dessine le trait
    let line = UIBezierPath(rect: CGRect(x: 0, y: thirdPointData, width: 0, height: 0))
    var countLine = 1

    while countLine <= (arrayOfDataPerMonth?.count)! {

        let x = CGFloat(countLine) * goldenRationForMonth!
        let y = getY(y: (arrayOfDataPerMonth?[countLine - 1])!)
        let point = CGPoint(x: x, y: y)
        line.addLine(to: point)
        countLine += 1
    }
    UIColor.blue.setStroke()
    line.lineWidth = 4
    line.stroke()



}



func getY(y: Int) -> CGFloat {
    var yToReturn : CGFloat = 0
    yToReturn = (frame.size.height - (CGFloat(y) * self.goldenRatioForData!)) - 30
    return yToReturn
}



}
let array = [21, 34, 36, 33, 31, 27, 23, 19, 8, 5, 25, 14]
let view = drawGraphMonthly(frame: CGRect(x: 0, y: 0, width: 414, height: 250), arrayOfDataPerMonth: array)
view.backgroundColor = UIColor(red:0.20, green:0.20, blue:0.20, alpha:1.0)

如果将其复制/粘贴到Playground中,您应该拥有与我相同的结果。

所以我的问题是,为什么我的stackview没有显示?

0 个答案:

没有答案