如何在iOS /核心图形中构建撤消堆栈 [英] how to build an undo stack in iOS/core graphics

查看:96
本文介绍了如何在iOS /核心图形中构建撤消堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为一组触摸添加撤消/重做功能..

I'm trying to add an undo / redo capability to a set of touches..

我有这个代码用于touchesBegan并移动:

I have this code for touchesBegan and moved:

- (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;


}

我想如果我能算出怎么样填充撤消堆栈,我可以遍历堆栈撤消重做触摸..也许我都湿了。我肯定会感谢一些帮助......

I think that if I can figure how to populate the undo stack, that I can iterate through the stack to undo redo touches.. Maybe I'm all wet. I sure would appreciate some help...

谢谢

..之前我曾问过类似的问题,但是我以不同的形式重新启动了项目,因为最后一种方式并不令人满意。

..I have asked a similar question before, but I've restarted the project in a different form as the last way was not satisfactory.

推荐答案

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

I finally solved this by managing arrays.

对于每个笔画,还有一个缓冲区数组:

For each stroke, there is an addition to a buffer array:

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

然后Undo和Redo方法如下所示:

Then the Undo and Redo methods look like this:

-(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];

}

我希望这有助于其他人。

I hope this helps others.

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

UPDATE - This is in response to the question below: "What is currentColoredPath?"


  1. @property(强,非原子)DrawingPath * currentColoredPath;

这是指一个类DrawingPath,我写的如下:

This refers to a class DrawingPath, which I wrote as follows:

.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 );

}

这篇关于如何在iOS /核心图形中构建撤消堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆