Home > OS >  How to remove only one line from LineView?
How to remove only one line from LineView?

Time:05-30

by my code I can draw multiple straight lines one by one on a view. now what I want is, to remove only one line when click Undo button. I have write single line to remove last draw line in undoButtonAction, but it removes whole view.

i have search a lot and trying different solutions
what can I do? please suggest some hint.

#view Controller

import UIKit

class Line {
    var startPoint : CGPoint?
    var endPoint : CGPoint?
    init(startPoint : CGPoint?, endPoint : CGPoint?) {
        self.startPoint = startPoint
        self.endPoint = endPoint

    }
}


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)

        let undoButton = UIButton(frame: CGRect(x: self.view.center.x - 50, y: self.view.frame.maxY - 100, width: 100, height: 30))

        undoButton.setTitle("Undo", for: .normal)
        undoButton.backgroundColor = .systemYellow
        undoButton.addTarget(self, action: #selector(undoButtonAction), for: .touchUpInside)
        self.view.addSubview(undoButton)
    }

    @objc func undoButtonAction(){
        lines.popLast()
        self.view.layer.removeFromSuperlayer()
    }

    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 = lineColor.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
            path.move(to: firstPosition!)
            path.addLine(to: lastPosition!)
            shapeLayer.path  = path.cgPath
        }


    }

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

        let line = Line(startPoint: firstPosition, endPoint: lastPosition)
        lines.append(line)
        drawLineFromPoint(start: line.startPoint!, toPoint: line.endPoint!, ofColor: .systemBlue, inView: view)


    }


}

#LineView

import Foundation
import UIKit



class LineView: UIView {
    var points = [CGPoint]()
    var lines = [[CGPoint]]()

    var fpoint : CGPoint?

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

CodePudding user response:

Search your lines array for the line you want to remove i.e lines[2] and call removeFromSuperView()

lines[2].removeFromSuperView()

CodePudding user response:

First add new variable in Line

class Line {
    var startPoint : CGPoint?
    var endPoint : CGPoint?
    var name : String?
    init(startPoint : CGPoint?, endPoint : CGPoint?, name : String?) {
        self.startPoint = startPoint
        self.endPoint = endPoint
        self.name = name

    }
}

Now when you are adding a line, provide random name using below function as randomString(length: 16)

func randomString(length: Int) -> String {
    let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    return String((0..<length).map{ _ in letters.randomElement()! })
}

Also update below methods

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    
    var randomString : String = randomString(length: 16)

    let line = Line(startPoint: firstPosition, endPoint: lastPosition, randomString)
    lines.append(line)
    drawLineFromPoint(start: line.startPoint!, toPoint: line.endPoint!, ofColor: .systemBlue, inView: view, name : randomString)
}


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

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

    let shapeLayer = CAShapeLayer()
    shapeLayer.path = path.cgPath
    shapeLayer.strokeColor = lineColor.cgColor
    shapeLayer.name = name
    shapeLayer.lineWidth = 2.0

    view.layer.addSublayer(shapeLayer)
}

So each layer, you have name identifier.

Now while doing undo, do below

@objc func undoButtonAction(){
    var lastLineObject : Line = lines[lines.count-1]
    guard let sublayers = self.view.layer.sublayers else { return }
    if let tempLayer = sublayers.first(where: {$0.name == lastLineObject.name ?? ""}) {
        print("Found it: \(layer)")
        tempLayer.removeFromSuperlayer()
    }
    lines.remove(at : lines.count - 1)
}

Note this is just idea and not real code.

  • Related