Volkodav Volkodav - 1 year ago 61
Swift Question

How to draw a line in the simplest way in swift

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 no idea on how to draw lines. I have looked on the web for the answer and can't figure it out. Can someone help.

Thanks Ahead

Answer Source

Try looking into UIBezierPath it will help you a lot for drawing lines. Documentation Here is an example:

    override func drawRect(rect: CGRect) {
        var aPath = UIBezierPath()

        aPath.moveToPoint(CGPoint(x:/*Put starting Location*/ y:/*Put starting Location*/))

        aPath.addLineToPoint(CGPoint(x:/*Put Next Location*/ y:/*Put Next Location*/))

        //Keep using the method addLineToPoint until you get to the one where about to close the path


//If you want to stroke it with a red color
//If you want to fill it as well 


Make sure you put this code in the drawRect. Like in the example above.

If you need to update the drawing just call setNeedsDisplay() to update.