How to draw a line in the simplest way in swift

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

    Drawing in Swift 4.1

    override func viewDidLoad() {
        super.viewDidLoad()
         self.lastPoint = CGPoint.zero
         self.red = 0.0
         self.green = 0.0
         self.blue = 0.0
    
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    //MARK: Touch events
    override func touchesBegan(_ touches: Set, with event: UIEvent?) {
        isSwiping    = false
        if let touch = touches.first{
            lastPoint = touch.location(in: mainImageView)
        }
    }
    override func touchesMoved(_ touches: Set, with event: UIEvent?) {
    
        isSwiping = true;
        if let touch = touches.first{
            let currentPoint = touch.location(in: mainImageView)
            UIGraphicsBeginImageContext(self.tempImageView.frame.size)
            self.tempImageView.image?.draw(in: CGRect(x:0, y:0,width:self.tempImageView.frame.size.width, height:self.tempImageView.frame.size.height))
    
            UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: lastPoint.x, y: lastPoint.y))
            UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: currentPoint.x, y: currentPoint.y))
    
            UIGraphicsGetCurrentContext()?.setLineCap(CGLineCap.round)
            UIGraphicsGetCurrentContext()?.setLineWidth(9.0)
            UIGraphicsGetCurrentContext()?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
            UIGraphicsGetCurrentContext()?.strokePath()
    
    
            self.tempImageView.image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            lastPoint = currentPoint
        }
    }
    override func touchesEnded(_ touches: Set, with event: UIEvent?) {
        if(!isSwiping) {
            // This is a single touch, draw a point
            UIGraphicsBeginImageContext(self.tempImageView.frame.size)
            self.tempImageView.image?.draw(in: CGRect(x:0, y:0,width:self.tempImageView.frame.size.width, height:self.tempImageView.frame.size.height))
            UIGraphicsGetCurrentContext()?.setLineCap(CGLineCap.round)
            UIGraphicsGetCurrentContext()?.setLineWidth(9.0)
    
            UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: lastPoint.x, y: lastPoint.y))
            UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: lastPoint.x, y: lastPoint.y))
            UIGraphicsGetCurrentContext()?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
            UIGraphicsGetCurrentContext()?.strokePath()
            self.tempImageView.image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
        }
    }
    

提交回复
热议问题