菜鸟教程小白 发表于 2022-12-11 18:40:17

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


                                            <p><p>使用 Swift 3.02 (Xcode 8.2) 我指的是答案中描述的撤消解决方案:<a href="https://stackoverflow.com/a/31450475/7448394" rel="noreferrer noopener nofollow">https://stackoverflow.com/a/31450475/7448394</a> .</p>
<p>在下面的代码中,我使用此解决方案来测试绘图的撤消和重做。
当我按下 randomDrawing 按钮时,它会从零到随机点绘制一条红线。
当我按下撤消按钮时,这一行或最后一行消失。
但是当我按下重做按钮时,imageView 没有任何反应。
显然undo是有效的,我还检查了undoManager?.canRedo,它在undo-action之后变为true,但是为什么这个redo-action没有显示结果?</p>
<pre><code>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()
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><ol>
<li><p>要采取好的措施,请检查您的重做按钮是否在 Interface Builder 中正确设置,并且它会触发您的 <code>redoDraving</code> 方法。 </p></li>
<li><p>我遇到了和你一样的问题(没有重做)。 <a href="https://stackoverflow.com/a/36492619/5670505" rel="noreferrer noopener nofollow">This
answer</a>帮助过我。 </p></li>
</ol></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 重做不起作用(ios swift undoManager,调用),我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41794991/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41794991/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 重做不起作用(ios swift undoManager,调用)