【问题标题】:redo does not work (ios swift undoManager, Invocation)重做不起作用(ios swift undoManager,调用)
【发布时间】:2017-06-07 07:01:02
【问题描述】:

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

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

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()
    }
}

【问题讨论】:

    标签: ios swift undo undo-redo nsundomanager


    【解决方案1】:
    1. 要采取好的措施,请检查您的重做按钮是否在 Interface Builder 中正确设置,并且它会触发您的 redoDraving 方法。

    2. 我遇到了和你一样的问题(没有重做)。 This answer 帮助了我。

    【讨论】:

      猜你喜欢
      • 2019-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多