undo redo issues with CGLayer

后端 未结 2 1415
余生分开走
余生分开走 2020-12-11 12:39

I am working with unod redo operations on CgLayer, I have tried some code, but not able to get it working, dont know , where I am getting wrong, below is my code, which i ha

相关标签:
2条回答
  • 2020-12-11 13:09
    - (void)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];
    }
    

    just update this method with ur code

    0 讨论(0)
  • 2020-12-11 13:18

    The best way to implement Undo and Redo is to implement NSUndoManager as a brief description of it you don't have to save each state of Object that you want to undo or redo the NSUndoManager itself make this for you ...

    Steps for reaching this is:

    1- Initialize NSUndoManager.

    2- Register Object state in the NSUndoManager object with specified function call will discuss this for you case later.

    3-use undo or redo or clear actions function in NSUndoManager Object.

    Ex from my working solution

    -in .h file

    @property (nonatomic,retain) NSUndoManager *undoManager;
    
    • in .m file

      @synthesize undoManager;

    • in "viewDidLoad" method -- initialize your NSUndoManager

      undoManager = [[NSUndoManager alloc] init];

    Suppose that you have a function in your class that zoom in/out using pinch so in your "viewDidLoad" you will have

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

    So pinch will zoom in/out such Note that "MyImageView" is the image that we want to zoom in/out

    - (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 Function will save current image transformation state in NSUndoManager.

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

    So if you have button that make Undo action

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

    So if you want to cancel all action you have both ways

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

    OR

    [undoManager removeAllActions];
    
    0 讨论(0)
提交回复
热议问题