2012-04-18 33 views
1

我试图撤销/重做功能添加到一组触摸..如何构建的iOS /核芯显卡的撤消堆栈

我对这个的touchesBegan代码和感动:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSLog(@"%s", __FUNCTION__); 
    mouseSwiped = NO; 
    UITouch *touch = [touches anyObject]; 

    if ([touch tapCount] == 2) { 
     [self eraseButtonTapped:self]; 
     return; 
    } 

    lastPoint = [touch locationInView:self.view]; 
    lastPoint.y -= 20; 

    [self.undoPath addObject:WHATGOESHERE]; 
    // Remove all paths from redo stack 
    [self.redoStack removeAllObjects]; 

} 


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    //NSLog(@"%s", __FUNCTION__); 
    mouseSwiped = YES; 

    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self.view]; 
    currentPoint.y -= 20; 



    UIGraphicsBeginImageContext(self.view.frame.size); 
    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetLineCap(context, kCGLineCapRound); 
    CGContextSetLineWidth(context, brush); 
    CGContextSetRGBStrokeColor(context, red, green, blue, 1.0); 
    CGContextBeginPath(context); 
    CGContextMoveToPoint(context, lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y); 
    CGContextStrokePath(context); 
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    [self.undoPath addObject:WHATGOESHERE]; 
    UIGraphicsEndImageContext(); 


    NSLog(@"Touches Moved undoPath contains %i objects", [self.undoPath count]); 
    // Remove all paths from redo stack 
    [self.redoPath removeAllObjects]; 
    lastPoint = currentPoint; 


} 

我认为,如果我可以计算如何填充撤消堆栈,那我可以遍历堆栈以撤销重做触摸。也许我都是湿的。我当然希望得到一些帮助...

感谢

..我以前也问过类似的问题,但我已经重新启动该项目以不同的形式作为最后的办法是不能令人满意的。

+2

iOS有一个内置的[撤消体系结构](http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/UndoArchitecture/UndoArchitecture.html)。看看那个? – 2012-04-18 05:48:37

+0

谢谢。我刚刚读了这个。这可能非常有用,但代码片段会很好地完成。我个人觉得从代码学习要比从文档学习要容易得多。 – 2012-04-18 06:19:21

+1

我花了数小时/天的时间来探索这一点。我还没有找到全面的答案。如果我收集一个,我会发布我的解决方案。唯一可以报答我的人的方法是帮助他人。 – 2012-04-18 13:28:12

回答

0

我终于通过管理数组解决了这个问题。

对于每个行程,有一个除了缓冲区数组:

[self.currentColoredPath.path moveToPoint:[touch locationInView:self]]; 
[self.currentArray addObject:self.currentColoredPath]; 
// Remove all paths from redo stack 
[self.redoStack removeAllObjects]; 

然后撤消和恢复方法是这样的:

-(void)undoButtonClicked 
{ 
    //NSLog(@"%s", __FUNCTION__); 
    if ([self.currentArray count] == 0) { 
     //nothing to undo 
     return; 
    } 

    DrawingPath *undonePath = [self.currentArray lastObject]; 
    [self.currentArray removeLastObject]; 
    [self.redoStack addObject:undonePath]; 
    [self setNeedsDisplay]; 

} 

-(void)redoButtonClicked 
{ 
    //NSLog(@"%s", __FUNCTION__); 

    if ([self.redoStack count] == 0) { 
     // nothing to redo 
     return; 
    } 

    DrawingPath *redonePath = [self.redoStack lastObject]; 
    [self.redoStack removeLastObject]; 
    [self.currentArray addObject:redonePath]; 
    [self setNeedsDisplay]; 

} 

我希望这可以帮助别人。

更新 - 这是对以下问题的回应:“什么是currentColoredPath?”

  1. @property (strong,nonatomic) DrawingPath *currentColoredPath;

  2. 这是指一类DrawingPath,其中我写如下:

.H

#import <Foundation/Foundation.h> 

@interface DrawingPath : NSObject { 
    NSString *brushSize; 
} 
@property (strong, nonatomic) NSString *brushSize; 
@property (strong,nonatomic) UIColor *color; 
@property (strong,nonatomic) UIBezierPath *path; 

- (void)draw; 
- (void)brushChange; 

的.m

#import "DrawingPath.h" 

@implementation DrawingPath 

@synthesize path = _path; 
@synthesize color = _color; 
@synthesize brushSize = _brushSize; 
float brush = 12; 

- (id)init { 
    //NSLog(@"%s", __FUNCTION__); 
    if (!(self = [super init])) 
     return nil; 
    brushSize = [[NSUserDefaults standardUserDefaults] objectForKey:@"brushKey"]; 
    [self brushChange]; 


    _path = [[UIBezierPath alloc] init]; 
    _path.lineCapStyle=kCGLineCapRound; 
    _path.lineJoinStyle=kCGLineJoinRound; 

    [_path setLineWidth:brush]; 





    return self; 
} 

- (void)draw { 
    //NSLog(@"%s", __FUNCTION__); 

    [self.color setStroke]; 
    [self.path stroke]; 
} 

- (void)brushChange { //from notification 
    //NSLog(@"%s", __FUNCTION__); 

    brushSize = [[NSUserDefaults standardUserDefaults] objectForKey:@"brushKey"]; 
    //NSLog(@"DrawingPath - brushSize is %@: ", brushSize); 


    //NSLog(@"DrawingPath - brush is %f: ", brush); 

} 
+0

here什么是currentColoredPath.path你能解释我吗 – banu 2013-01-08 05:10:47

+0

嗨Banu,我已经更新了我的答案。我希望它有帮助。 – 2013-01-08 15:25:39