Implementing NSUndoManager

。_饼干妹妹 提交于 2019-12-03 09:15:48

The touchesMoved method will called multi times when you draw a line, so if you register undo action in the touchesMoved method you need to group them:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.undoManager beginUndoGrouping];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.undoManager endUndoGrouping];
}

here's a example.

Complete example in Swift 4.* and Xcode 9.3

import UIKit

class ViewController: UIViewController {


    @IBOutlet weak var colorsCollectionView: UICollectionView!

    @IBOutlet weak var colorView: UIView!

    var colorsArray = ["green","blue","red","black","yellow","brown","orange","purple","cyan","magenta"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func redoAction(_ sender: Any) {
        undoManager?.redo()
    }

    @IBAction func undoAction(_ sender: Any) {
        undoManager?.undo()
    }

    func changeColor(color: UIColor) {

        let oldColor = self.colorView.backgroundColor ?? UIColor.white
        undoManager?.registerUndo(withTarget: self, handler: { (targetSelf) in
            targetSelf.changeColor(color: oldColor)
        })
        colorView.backgroundColor = color
    }

}


extension ViewController: UICollectionViewDelegate,UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return colorsArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)

        let label:UILabel = cell.viewWithTag(101) as! UILabel
        label.text = colorsArray[indexPath.row]

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let colorName = colorsArray[indexPath.row]

        switch colorName {
        case "green":
            changeColor(color: UIColor.green)
        case "blue":
            changeColor(color: UIColor.blue)
        case "red":
            changeColor(color: UIColor.red)
        case "black":
            changeColor(color: UIColor.black)
        case "yellow":
            changeColor(color: UIColor.yellow)
        case "brown":
            changeColor(color: UIColor.brown)
        case "orange":
            changeColor(color: UIColor.orange)
        case "purple":
            changeColor(color: UIColor.purple)
        case "cyan":
            changeColor(color: UIColor.cyan)
        case "magenta":
            changeColor(color: UIColor.magenta)
        default:
            changeColor(color: UIColor.white)
        }
    }
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!