Программируем под IOS
Anonymous
Как я могу реализовать функции Undomanager и сделать функции Undo (), Redo ()?
Сообщение
Anonymous » 06 авг 2025, 13:18
Мне нужно создать функциональность отмены и повторно, используя Swift Undomanager в моем приложении для краски. Я сделал это снятие с удалением последнего выъезда из массива. Но я хочу использовать Undomanager. И если есть некоторые советы, может быть, я делаю что -то не так. Если кто -то может мне помочь, пожалуйста. < /P>
Код: Выделить всё
final class WACanvas: UIView {
private var lines = [WALine]()
private var strokeColor = UIColor.black
private var strokeWidth: Float = 1
override func draw(_ rect: CGRect) {
super.draw(rect)
guard let context = UIGraphicsGetCurrentContext() else { return }
lines.forEach { line in
guard line.points.count > 1 else { return }
print(line.points)
context.setStrokeColor(line.color.cgColor)
context.setLineWidth(CGFloat(line.width))
context.setLineCap(.round)
context.move(to: line.points.first!)
context.addLines(between: line.points)
context.strokePath()
}
}
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
lines.append(WALine(color: strokeColor, width: strokeWidth, points: []))
}
override func touchesMoved(_ touches: Set, with event: UIEvent?) {
guard let point = touches.first?.location(in: self) else { return }
guard var lastLine = lines.popLast() else { return }
lastLine.points.append(point)
lines.append(lastLine)
setNeedsDisplay()
}
// MARK: - Public methods
public func undo() {
_ = lines.popLast()
setNeedsDisplay()
}
public func clear() {
lines.removeAll()
setNeedsDisplay()
}
public func setSTrokeColor(color: UIColor) {
self.strokeColor = color
}
public func setSTroke(width: Float) {
self.strokeWidth = width
}
}
Подробнее здесь:
https://stackoverflow.com/questions/797 ... -functions
1754475481
Anonymous
Мне нужно создать функциональность отмены и повторно, используя Swift Undomanager в моем приложении для краски. Я сделал это снятие с удалением последнего выъезда из массива. Но я хочу использовать Undomanager. И если есть некоторые советы, может быть, я делаю что -то не так. Если кто -то может мне помочь, пожалуйста. < /P> [code]final class WACanvas: UIView { private var lines = [WALine]() private var strokeColor = UIColor.black private var strokeWidth: Float = 1 override func draw(_ rect: CGRect) { super.draw(rect) guard let context = UIGraphicsGetCurrentContext() else { return } lines.forEach { line in guard line.points.count > 1 else { return } print(line.points) context.setStrokeColor(line.color.cgColor) context.setLineWidth(CGFloat(line.width)) context.setLineCap(.round) context.move(to: line.points.first!) context.addLines(between: line.points) context.strokePath() } } override func touchesBegan(_ touches: Set, with event: UIEvent?) { lines.append(WALine(color: strokeColor, width: strokeWidth, points: [])) } override func touchesMoved(_ touches: Set, with event: UIEvent?) { guard let point = touches.first?.location(in: self) else { return } guard var lastLine = lines.popLast() else { return } lastLine.points.append(point) lines.append(lastLine) setNeedsDisplay() } // MARK: - Public methods public func undo() { _ = lines.popLast() setNeedsDisplay() } public func clear() { lines.removeAll() setNeedsDisplay() } public func setSTrokeColor(color: UIColor) { self.strokeColor = color } public func setSTroke(width: Float) { self.strokeWidth = width } [/code] } Подробнее здесь: [url]https://stackoverflow.com/questions/79727146/how-can-i-implement-undomanager-and-make-undo-redo-functions[/url]