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

Swift 将视图添加到用于绘制的我的UIView(rect:),swift,uiview,Swift,Uiview,我正试着做我自己的图表,所以我打开我的操场开始打字 我已经学会了如何使用draw(),到目前为止还很酷。但是现在,我想在上面添加一些文字。这是我的代码: import UIKit class drawGraphMonthly: UIView { var arrayOfDataPerMonth: [Int]? var months: [String]? var goldenRatioForData: CGFloat? var goldenRationForMonth: CGFloat? var

我正试着做我自己的图表,所以我打开我的操场开始打字

我已经学会了如何使用draw(),到目前为止还很酷。但是现在,我想在上面添加一些文字。这是我的代码:

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)
导入UIKit
类drawGraphMonthly:UIView{
变量arrayOfDataPerMonth:[Int]?
变量月份:[字符串]?
var goldenRatioForData:CGFloat?
var GoldenrantionFormMonth:CGFloat?
var monthStackView:UIStackView?
init(帧: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={
设sv=UIStackView()
sv.translatesAutoresizingMaskIntoConstraints=false
sv.backgroundColor=.red
sv.distribution=.Fill
sv轴=水平
为了自己的月,为了自己的月{
let monthLabel:UILabel={
let label=UILabel()
label.text=月份
退货标签
}()
sv.addArrangedSubview(月标签)
}
返回sv
}()
self.addSubview(monthStackView!)
monthStackView?.bottomAnchor.constraint(等式:self.bottomAnchor).isActive=true
monthStackView?.leftAnchor.constraint(等式:self.leftAnchor).isActive=true
monthStackView?.rightAnchor.constraint(等式:self.rightAnchor).isActive=true
monthStackView?.heightAnchor.constraint(equalToConstant:30)。isActive=true
}
必需的初始化?(编码器aDecoder:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
重写函数绘图(rect:CGRect){
//论德辛·勒背景
let backgroundView=UIBezierPath(rect:CGRect(x:frame.size.width,y:frame.size.height,width:1,height:1))
设secondPoint=CGPoint(x:0,y:frame.size.height)
设thirdPointData=getY(y:(arrayOfDataPerMonth?[(arrayOfDataPerMonth?.count)!-1]))
设thirdPoint=CGPoint(x:0,y:thirdPointData)
backgroundView.addLine(收件人:secondPoint)
背景视图.添加行(至:第三点)
var countBG=1

而countBG你应该在
draw
方法中绘制文本。你的意思是使用addsubview并将stackview约束到draw()方法中?不。我的意思是不要使用堆栈视图或任何其他子视图。只需像绘制图表的其余部分一样绘制文本。这不是有点复杂吗?你的意思是告诉draw()一个“a”的每个点还有“D”的所有点等等?不。请查看
CGContext
的文档。有一整套绘制文本的方法。