使用CGLayer撤消重做问题

时间:2012-07-09 12:16:04

标签: ios cocoa-touch quartz-graphics quartz-2d cglayer

我正在使用CgLayer上的unod重做操作,我已经尝试了一些代码,但是无法让它工作,不知道,我哪里出错了,下面是我的代码,我写的

这是我的drawRect函数

- (void)drawRect:(CGRect)rect
{    
    m_backgroundImage = [UIImage imageNamed:@"bridge.jpg"];

    CGPoint drawingTargetPoint = CGPointMake(0,0);
    [m_backgroundImage drawAtPoint:drawingTargetPoint];


    switch(drawStep)
    {
          case DRAW:
          {
              CGContextRef context = UIGraphicsGetCurrentContext();

              if(myLayerRef == nil)
              {

                  myLayerRef = CGLayerCreateWithContext(context, self.bounds.size, NULL);
              }   

              CGContextDrawLayerAtPoint(context, CGPointZero, myLayerRef); 
              break;
          }           

         case UNDO:
         {            
              [curImage drawInRect:self.bounds];              
              break;
         }

        default:
            break;
    }      
}

在接触结束时,我将图层转换为NSValue并将其作为keyValue对存储到NSDictionary中,然后将字典对象添加到数组中。

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{        
    NSValue *layerCopy = [NSValue valueWithPointer:myLayerRef];


    NSDictionary *lineInfo = [NSDictionary dictionaryWithObjectsAndKeys:layerCopy, @"IMAGE",
                              nil];

    [m_pathArray addObject:lineInfo];    
    NSLog(@"%i",[m_pathArray count]);

}

下面是我的撤消功能

- (void)undoButtonClicked
{   
    if([m_pathArray count]>0)
    {
        NSMutableArray *_line=[m_pathArray lastObject];
        [m_bufferArray addObject:[_line copy]];
        [m_pathArray removeLastObject];
        drawStep = UNDO;
        [self redrawLine];
    } 
}

//Redraw functions

- (void)redrawLine
{
    NSDictionary *lineInfo = [m_pathArray lastObject];

    NSValue *val = [lineInfo valueForKey:@"IMAGE"];

    CGLayerRef  layerToShow = (CGLayerRef) [val pointerValue];

    CGContextRef context1 = CGLayerGetContext(layerToShow);
    CGContextDrawLayerAtPoint(context1, CGPointMake(00, 00),layerToShow);
    [self setNeedsDisplayInRect:self.bounds];
}

我认为这是我错误的地方。所以朋友们请帮帮我。

从下面的评论中,我添加了函数,其中它绘制到Cglayer中(这个函数我调用了touchesMovedEvent。

- (void) drawingOperations
{    
    CGContextRef context1 = CGLayerGetContext(myLayerRef);    

    CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2); 
    CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1);     

    CGContextMoveToPoint(context1, mid1.x, mid1.y);
    CGContextAddQuadCurveToPoint(context1, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y); 
    CGContextSetLineCap(context1, kCGLineCapRound);
    CGContextSetLineWidth(context1, self.lineWidth);
    CGContextSetStrokeColorWithColor(context1, self.lineColor.CGColor);           
    CGContextSetAllowsAntialiasing(context1, YES);
    CGContextSetInterpolationQuality(context1, kCGInterpolationHigh); 
    CGContextSetAlpha(context1, self.lineAlpha);
    CGContextStrokePath(context1);    
}

此致 兰吉特

2 个答案:

答案 0 :(得分:1)

实现撤消和重做的最佳方法是实现NSUndoManager作为它的简要描述,您不必保存要撤消的每个Object状态或重做NSUndoManager本身为您做这个...

达到此目标的步骤是:

1-初始化NSUndoManager。

2-注册具有指定函数调用的NSUndoManager对象中的对象状态将在稍后讨论此情况。

在NSUndoManager对象中使用3次撤消或重做或清除操作功能。

来自我的工作解决方案

-in .h文件

@property (nonatomic,retain) NSUndoManager *undoManager;
    .m文件中的
  • @synthesize undoManager;

  • “viewDidLoad”方法中的
  • - 初始化您的NSUndoManager

    undoManager = [[NSUndoManager alloc] init];

假设您的班级中有一个功能可以使用捏缩放/缩小,所以在你的“viewDidLoad”中你会有

UIPinchGestureRecognizer *pinchGesture =
    [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    pinchGesture.delegate = (id)self;
    [self.view addGestureRecognizer:pinchGesture];

因此捏合会放大/缩小 请注意,“MyImageView”是我们要放大/缩小的图像

- (void)pinch:(UIPinchGestureRecognizer*)recognizer{

    if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateChanged) {
        NSLog(@"gesture.scale = %f", recognizer.scale);

        CGFloat currentScale = self.MyImageView.frame.size.width / self.MyImageView.bounds.size.width;
        CGFloat newScale = currentScale * recognizer.scale;
        //Here is the line that register image to NSUndoManager before making and adjustments to the image "save current image before changing the transformation"
        //Add image function is function that fired when you Make undo action using NSUndoManager "and so we maintain only image transformation that changed when you zoom in/out"
         [[undoManager prepareWithInvocationTarget:self] AddImage:self.MyImageView.transform];

        if (newScale < 0.5) {
            newScale = 0.5;
        }
        if (newScale > 5) {
            newScale = 5;
        }

        CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale);
        self.MyImageView.transform = transform;
        recognizer.scale = 1;
    }
}

-AddImage函数将在NSUndoManager中保存当前图像转换状态。

-(void)AddImage:(CGAffineTransform)sender{
    CGAffineTransform transform = sender;
    self.MyImageView.transform = transform;
}

因此,如果您有按钮可以进行撤消操作

-(IBAction)OptionsBtn:(id)sender{
  if ([undoManager canUndo]) {
      [undoManager undo];
  }
}

因此,如果您想要取消所有操作,您可以双向使用

while ([undoManager canUndo]) {
   [undoManager undo];
}

OR

[undoManager removeAllActions];

答案 1 :(得分:0)

  • (无效)redrawLine { NSDictionary * lineInfo = [m_pathArray lastObject];

    NSValue * val = [lineInfo valueForKey:@“IMAGE”];

    CGContextRef context1 =(CGContextRef)[val pointerValue]; CGContextDrawLayerAtPoint(context1,CGPointMake(00,00),layerToShow); [self setNeedsDisplayInRect:self.bounds]; }

只需使用您的代码

更新此方法