gpt4 book ai didi

ios - 重做不起作用(ios swift undoManager,调用)

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:54:09 28 4
gpt4 key购买 nike

使用 Swift 3.02 (Xcode 8.2) 我引用了答案中描述的撤消解决方案:https://stackoverflow.com/a/31450475/7448394 .

在下面的代码中,我使用此解决方案来测试绘图的撤消和重做。当我按下 randomDrawing 按钮时,它会绘制一条从零到随机点的红线。当我按下撤消按钮时,这行或最后一行消失。但是当我按下重做按钮时,imageView 没有任何反应。明明撤消是有效的,我也检查了 undoManager?.canRedo 撤消操作后变为 true,但为什么这个重做操作没有显示结果?

import UIKit

class ViewController: UIViewController {

@IBOutlet var imageView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func undoDrawing(_ sender: AnyObject) {
undoManager?.undo()
}
@IBAction func redoDrawing(_ sender: AnyObject) {
undoManager?.redo()
}
@IBAction func randomDrawing(_ sender: AnyObject) {
let undo: UIImageView = undoManager?.prepare(withInvocationTarget: imageView) as! UIImageView

UIGraphicsBeginImageContext(self.imageView.frame.size)
self.imageView.image?.draw(in: CGRect(x: 0, y: 0, width: self.imageView.frame.size.width, height: self.imageView.frame.size.height))

UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: 0, y: 0))
UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: Int(arc4random_uniform(100) + 100), y: Int(arc4random_uniform(100) + 100)))
UIGraphicsGetCurrentContext()?.setLineWidth(10.0)
UIGraphicsGetCurrentContext()?.setStrokeColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
UIGraphicsGetCurrentContext()?.strokePath()

undo.image = imageView.image
self.imageView.image = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()
}
}

最佳答案

  1. 为了采取好的措施,请检查您的重做按钮是否已在 Interface Builder 中正确设置,并且它会触发您的 redoDraving 方法。

  2. 我遇到了和您一样的问题(没有重做)。 Thisanswer帮助过我。

关于ios - 重做不起作用(ios swift undoManager,调用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41794991/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com