自由手绘应用程序中的撤消和重做功能

时间:2012-06-04 10:55:52

标签: ios uikit core-graphics

我正在绘制一个绘图应用程序,我可以用手指触摸,现在我正在尝试实现,清除,撤消和重做功能,在我的viewcontroller中,我有两个IBAction方法用于“clearAll”和“撤消“,我创建了一个名为drawing.h和.m的自定义类,其中我编写了处理触摸事件的函数,下面是我的函数..

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {
    UITouch *touch = [touches anyObject]; 

     tempArray = [[NSMutableArray alloc]init];

    if ([touch tapCount] == 2) 
    {
        self.image = nil;
    }
     return;
} 


 currentlocation.location = [touch locationInView:self];
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    PointLocation *currentLocation1 = [[PointLocation alloc] init];
    currentLocation1.Location = [touch locationInView:self]; 


    UIGraphicsBeginImageContext(self.frame.size); 
    CGContextRef ctx = UIGraphicsGetCurrentContext();   
    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetLineWidth(ctx, 2.0);
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(ctx);

    CGContextMoveToPoint(ctx, currentlocation.Location.x, currentlocation.Location.y);
    CGContextAddLineToPoint(ctx, currentLocation1.Location.x, currentLocation1.Location.y);
    CGContextStrokePath(ctx);

    CGContextSetFlatness(ctx, 0.1);

    CGContextSetAllowsAntialiasing(ctx, true);
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();  

    currentlocation = currentLocation1;   

    **[tempArray addObject:[NSArray arrayWithObjects:currentlocation,currentLocation1, nil]];  

    NSLog(@"%i",[tempArray count]);**      
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {   

    UITouch *touch = [touches anyObject]; 


    PointLocation *currentLocation1 = [[PointLocation alloc] init];
    currentLocation1.Location = [touch locationInView:self];

    UIGraphicsBeginImageContext(self.frame.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();   
    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetLineWidth(ctx, 2.0);
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, currentlocation.Location.x, currentlocation.Location.y);
    CGContextAddLineToPoint(ctx, currentLocation1.Location.x, currentLocation1.Location.y);
    CGContextStrokePath(ctx);
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    currentlocation = currentLocation1;    

    **[tempArray addObject:[NSArray arrayWithObjects:currentlocation,currentLocation1, nil]];    
    NSLog(@"%i",[tempArray count]);**  

}

在上面的代码中我将所有CGPoints添加到一个数组中,数组正在填充这些点,现在我该如何执行撤消功能呢?

所以朋友们请帮帮我..

此致 兰吉特

0 个答案:

没有答案