0

I found a question similar to my own here: Draw a line realtime with Swift 3.0

After piecing it together I have it all in my own program, but I am not sure where to make the call to drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) in my code so I can actually draw. The app runs, but I still can't draw. Code is below.

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    swiped = false
    if let touch = touches.first {
        lastPoint = touch.location(in: self.view)
    }
}

func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0)

    tempImageView.image?.draw(in: view.bounds)

    let context = UIGraphicsGetCurrentContext()

    context?.move(to: fromPoint)
    context?.addLine(to: toPoint)

    context?.setLineCap(CGLineCap.round)
    context?.setLineWidth(brushWidth)
    context?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
    context?.setBlendMode(CGBlendMode.normal)
    context?.strokePath()

    tempImageView.image = UIGraphicsGetImageFromCurrentImageContext()
    tempImageView.alpha = opacity
    UIGraphicsEndImageContext()
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    swiped = true
    if let touch = touches.first {
        let currentPoint = touch.location(in: view)
        drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint)

        lastPoint = currentPoint
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if !swiped {
        // draw a single point
        self.drawLineFrom(fromPoint: lastPoint, toPoint: lastPoint)
    }
}

let tempImageView = UIImageView()

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = UIColor(r: 21, g: 221, b: 125)

    //view.addSubview(titleLabel)
    //view.addSubview(learnButton)
    //view.addSubview(drawButton)
    //view.addSubview(mainImageView)
    view.addSubview(tempImageView)


    //setupTitleLabel()
    //setupLearnButton()
    //setupDrawButton()
    setupImageViews()

    self.navigationController?.isNavigationBarHidden = true
}

func setupImageViews() {
    tempImageView.widthAnchor.constraint(equalTo: view.widthAnchor)
    tempImageView.heightAnchor.constraint(equalTo: view.heightAnchor)
}
Community
  • 1
  • 1
xyphergrim
  • 31
  • 1
  • 4

0 Answers0