我在实现NSUndoManager时遇到了困难,我试着阅读了苹果的文档,但我不能理解它。这就是我到目前为止所尝试的。我已经创建了一个通过连接数组中的两个点来绘制线条的应用程序,我通过移除最后一个对象实现了一个撤销方法,但是我不知道如何实现重做,我偶然发现了NSUndoManager并开始阅读它的文档,但我不知道如何将它应用到我的问题中。这是我目前拥有的代码
-(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,我不理解的是事物的去向,“对象”是什么。我不是在声明一些我需要声明的东西吗。
谢谢,
发布于 2014-10-30 16:39:01
当您绘制直线时,touchesMoved方法将调用多次,因此,如果在touchesMoved方法中注册undo操作,则需要对它们进行分组:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.undoManager beginUndoGrouping];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.undoManager endUndoGrouping];
}这是一个example。
发布于 2018-07-12 14:48:40
Swift 4.*和Xcode9.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)
}
}
}

https://stackoverflow.com/questions/24439803
复制相似问题