I have to create undo and redo performance utilizing swift UndoManager to my Paint app. I did it undo with eradicating final oject from array. However i need to use UndoManager. And if there’s some advices, possibly i’m doing one thing improper. If somebody may also help me, pls.
remaining class WACanvas: UIView {
non-public var strains = [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 }
strains.forEach { line in
guard line.factors.rely > 1 else { return }
print(line.factors)
context.setStrokeColor(line.coloration.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?) {
strains.append(WALine(coloration: 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 = strains.popLast() else { return }
lastLine.factors.append(level)
strains.append(lastLine)
setNeedsDisplay()
}
// MARK: - Public strategies
public func undo() {
_ = strains.popLast()
setNeedsDisplay()
}
public func clear() {
strains.removeAll()
setNeedsDisplay()
}
public func setSTrokeColor(coloration: UIColor) {
self.strokeColor = coloration
}
public func setSTroke(width: Float) {
self.strokeWidth = width
}
}