How to draw a line in the simplest way in swift

前端 未结 10 2020
醉梦人生
醉梦人生 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 10:59

    Swift 3.1, inside a UIView:

    public override func draw(_ rect: CGRect) {
        let context = UIGraphicsGetCurrentContext()
        context!.setLineWidth(2.0)
        context!.setStrokeColor(UIColor.red.cgColor)
        context?.move(to: CGPoint(x: 0, y: self.frame.size.height))
        context?.addLine(to: CGPoint(x: self.frame.size.width, y: 0))
        context!.strokePath()
    }
    

    If you want to add a line to a UIViewController, just add a UIView with this function as a subview to the UIViewController, ei:

    let line = LineView(CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
    self.view.addSubview(line)
    

提交回复
热议问题