How to draw a line in the simplest way in swift

前端 未结 10 1986
醉梦人生
醉梦人生 2020-12-01 10:18

I am fairly new to swift and Xcode and I am trying to make a tic tac toe game. I have everything figured out except how to draw a line through the three x\'s or o\'s. I have

10条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 11:08

    Simple SWIFT 4.1 Implementation:

    public override func draw(_ rect: CGRect) {
        guard let context = UIGraphicsGetCurrentContext() else { return }
        let lineWidth: CGFloat = 1.0
        context.setLineWidth(lineWidth)
        context.setStrokeColor(UIColor(style: .tertiaryBackground).cgColor)
        let startingPoint = CGPoint(x: 0, y: rect.size.height - lineWidth)
        let endingPoint = CGPoint(x: rect.size.width, y: rect.size.height - lineWidth)
        context.move(to: startingPoint )
        context.addLine(to: endingPoint )
        context.strokePath()
    }
    

    Obviously, you need to adjust the starting and ending point.

提交回复
热议问题