I must create undo and redo performance utilizing swift UndoManager to my Paint app. I did it undo with eradicating final oject from array. However i wish to use UndoManager. And if there may be some advices, perhaps i’m doing one thing fallacious. If somebody may help me, pls.
remaining class WACanvas: UIView {
non-public var traces = [WALine]()
non-public var strokeColor = UIColor.black
non-public var strokeWidth: Float = 1
override func draw(_ rect: CGRect) {
tremendous.draw(rect)
guard let context = UIGraphicsGetCurrentContext() else { return }
traces.forEach { line in
guard line.factors.depend > 1 else { return }
print(line.factors)
context.setStrokeColor(line.colour.cgColor)
context.setLineWidth(CGFloat(line.width))
context.setLineCap(.spherical)
context.transfer(to: line.factors.first!)
context.addLines(between: line.factors)
context.strokePath()
}
}
override func touchesBegan(_ touches: Set, with occasion: UIEvent?) {
traces.append(WALine(colour: strokeColor, width: strokeWidth, factors: []))
}
override func touchesMoved(_ touches: Set, with occasion: UIEvent?) {
guard let level = touches.first?.location(in: self) else { return }
guard var lastLine = traces.popLast() else { return }
lastLine.factors.append(level)
traces.append(lastLine)
setNeedsDisplay()
}
// MARK: - Public strategies
public func undo() {
_ = traces.popLast()
setNeedsDisplay()
}
public func clear() {
traces.removeAll()
setNeedsDisplay()
}
public func setSTrokeColor(colour: UIColor) {
self.strokeColor = colour
}
public func setSTroke(width: Float) {
self.strokeWidth = width
}
}