Dhiren
Dhiren

Reputation: 51

how to always show all draw lines?

in my code, I can draw a line by following user touch.

the issue is whenever I draw another line, the previous line is gone. how can I always show all lines? please guide me for the same. CanvasView.swift

import Foundation
import UIKit



class CanvasView: UIView {

//    var line = [CGPoint]()
    var points = [CGPoint]()
    var lines = [[CGPoint]]()

    var fpoint : CGPoint?




    //custom draw


//    override func draw(_ rect: CGRect) {
//        super.draw(rect)
//
//        guard let currentContext = UIGraphicsGetCurrentContext() else {
//            return
//        }
//
//        currentContext.setLineWidth(2)
//        currentContext.setLineCap(.butt)
////        let startPoint = CGPoint(x: 0, y: 10)
////        let endPoint = CGPoint(x: 100, y: 50)
////        //begin a line at specific  point
////        currentContext.move(to: startPoint)
////
////        //end of line at specific point
////        currentContext.addLine(to: endPoint)
//
//        lines.forEach { line in
//            for (i,p) in line.enumerated(){
//                if i == 0 {
//                    currentContext.move(to: p)
//                }else{
//                    currentContext.addLine(to: p)
//                }
//            }
//        }
//
////        for (i,p) in line.enumerated(){
////            if i == 0 {
////                currentContext.move(to: p)
////            }else{
////                currentContext.addLine(to: p)
////            }
////        }
//
//
//        //paint a line along with current path
//        currentContext.strokePath()
//    }

    func clearLine(){
        lines.append([CGPoint]())
    }
}

ViewController.swift

import UIKit

class Line {
    var startPoint : CGPoint?
    var endPoint : CGPoint?
}


class ViewController: UIViewController {

    var counter = 0
    var lastPosition : CGPoint?
    var firstPosition : CGPoint?
    let shapeLayer = CAShapeLayer()
    let path = UIBezierPath()
    let canvas = CanvasView()


    var lines = Line()

    func setup(){

        self.view.backgroundColor = UIColor.cyan
        shapeLayer.path = path.cgPath
        shapeLayer.strokeColor = UIColor.blue.cgColor
        shapeLayer.lineWidth = 2.0

        self.view.layer.addSublayer(shapeLayer)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.setup()
    }

    func drawLineFromPoint(start : CGPoint, toPoint end:CGPoint, ofColor lineColor: UIColor, inView view:UIView) {

        let path = UIBezierPath()
        path.move(to: start)
        path.addLine(to: end)

        let shapeLayer = CAShapeLayer()
        shapeLayer.path = path.cgPath
        shapeLayer.strokeColor = UIColor.red.cgColor
        shapeLayer.lineWidth = 2.0

        view.layer.addSublayer(shapeLayer)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        if let touch = touches.first{

            let position = touch.location(in: view)
            self.firstPosition = position
            self.lastPosition = position


            path.move(to: firstPosition!)
            path.addLine(to: lastPosition!)
            print(position)

        }

    }


    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

        if let touch = touches.first{

            let lastlocation = touch.location(in: view)

            path.removeAllPoints()
            lastPosition = lastlocation
//            if counter != 0 {
//                first lines.startPoint
////
////                for (i, pos) in lines.enumerated() {
////                    firstPosition = lines.
////                    lastPosition = lines.endPoint
////                }
//            }
//            else{
//                self.lines.startPoint = lastlocation
//            }
            path.move(to: firstPosition!)
            path.addLine(to: lastPosition!)
            shapeLayer.path  = path.cgPath
        }

    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {


//        for line in lines {
//
//            line.startPoint = firstPosition
//            line.endPoint = lastPosition
//        }
        lines.startPoint = firstPosition
        lines.endPoint = lastPosition

    }


}

please guide me for the same. if anything else you need let me know? let me know.

Upvotes: 0

Views: 69

Answers (0)

Related Questions