OGeek|极客世界-中国程序员成长平台

标题: ios - 重做不起作用(ios swift undoManager,调用) [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:40
标题: ios - 重做不起作用(ios swift undoManager,调用)

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



Best Answer-推荐答案


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

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

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






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4