首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实现NSUndoManager

实现NSUndoManager
EN

Stack Overflow用户
提问于 2014-06-27 04:38:59
回答 2查看 2.1K关注 0票数 0

我在实现NSUndoManager时遇到了困难,我试着阅读了苹果的文档,但我不能理解它。这就是我到目前为止所尝试的。我已经创建了一个通过连接数组中的两个点来绘制线条的应用程序,我通过移除最后一个对象实现了一个撤销方法,但是我不知道如何实现重做,我偶然发现了NSUndoManager并开始阅读它的文档,但我不知道如何将它应用到我的问题中。这是我目前拥有的代码

代码语言:javascript
复制
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSUInteger taps = [[touches anyObject]tapCount];
    if(taps == 2) {
        [self setNeedsDisplay];
    }
    else {
        if([self.pointsArray count] == 0) {
            self.pointsArray = [[NSMutableArray alloc]init];
            UITouch *t = [touches anyObject];
            CGPoint startLoc = [t locationInView:self];
            [self.pointsArray addObject:[NSValue valueWithCGPoint:startLoc]];
        }
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *t = [touches anyObject];
    CGPoint currentLoc = [t locationInView:self];
    [self.pointsArray addObject:[NSValue valueWithCGPoint:currentLoc]];
    [self setNeedsDisplay];
}

#pragma mark - Undo/Redo Methods
-(void)undo:(id) object {
    [[undoManager prepareWithInvocationTarget:self]redo:object];
    [undoManager setActionName:@"undoLineSegment"];
    [self.pointsArray removeLastObject];
}

-(void)redo:(id)object {
    [self.pointsArray addObject:object];
    [[undoManager prepareWithInvocationTarget:self]undo:object];
    [undoManager setActionName:@"RedoUndoneLineSegment"];
}

- (IBAction)undoButton:(UIButton *)sender {
    [self.undoManager undo];
    [self setNeedsDisplay];
}

- (IBAction)redoButton:(UIButton *)sender {
    [self.undoManager redo];
    [self setNeedsDisplay];
}

我没有收到任何错误,但在运行时,当我点击按钮时,什么也没有发生。关于NSUndoManager,我不理解的是事物的去向,“对象”是什么。我不是在声明一些我需要声明的东西吗。

谢谢,

EN

回答 2

Stack Overflow用户

发布于 2014-10-30 16:39:01

当您绘制直线时,touchesMoved方法将调用多次,因此,如果在touchesMoved方法中注册undo操作,则需要对它们进行分组:

代码语言:javascript
复制
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.undoManager beginUndoGrouping];
}

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

这是一个example

票数 0
EN

Stack Overflow用户

发布于 2018-07-12 14:48:40

Swift 4.*和Xcode9.3中的完整示例

代码语言:javascript
复制
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)
        }
    }
}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24439803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档