簡體   English   中英

如何從 LineView 中只刪除一行?

[英]How to remove only one line from LineView?

通過我的代碼,我可以在視圖上一條一條地繪制多條直線。 現在我想要的是,單擊撤消按鈕時只刪除一行。 我已經寫了一行來刪除 undoButtonAction 中的最后一個繪制線,但它會刪除整個視圖。

我進行了很多搜索並嘗試了不同的解決方案
我能做些什么? 請提出一些提示。

#view 控制器

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)


    }


}

#線視圖

import Foundation
import UIKit



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

    var fpoint : CGPoint?

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

在您的lines數組中搜索要刪除的行, ie lines[2]並調用removeFromSuperView()

lines[2].removeFromSuperView()

首先在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

    }
}

現在,當您添加一行時,使用以下函數作為randomString(length: 16)提供隨機名稱

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

還更新以下方法

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)
}

所以每一層,你都有名稱標識符。

現在在撤消時,請執行以下操作

@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)
    }
    
}

請注意,這只是想法,而不是真正的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM