如何从LineView中删除一行?
通过我的代码,我可以在视图上一一绘制多个直线。 现在,我想要的是,单击撤消按钮时仅删除一行。 我已经编写了单行以删除未燃烧性的最后一个拉动线,但是它删除了整个视图。
我已经搜索了很多,尝试了不同的解决方案
我能做些什么?请建议一些提示。
#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]())
}
}
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]())
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
搜索您的
行
数组中要删除的行ie行[2]
并调用remove> remove fromsuperview()
Search your
lines
array for the line you want to removei.e lines[2]
and callremoveFromSuperView()
首先在
line
中添加新变量,添加行时,使用以下函数为
随机串(长度:16)
提供随机名称。
现在 具有名称标识符。
现在进行撤消时,在 下进行操作
,请注意,这只是想法而不是真实的代码。
First add new variable in
Line
Now when you are adding a line, provide random name using below function as
randomString(length: 16)
Also update below methods
So each layer, you have name identifier.
Now while doing undo, do below
Note this is just idea and not real code.