How to cancel an operation in iOS to store a UIGraphicsContext?

I want to perform undo operations in a graphics context in a drawing application. When the click is canceled, I want to go to the previous context containing the old drawing.

Example:

I have a rectangle in context. When dragging, I move the rectangle to a new position and redraw it. Now, when I press the cancel button, I want to move the rectangle to the previous position. How can i do this?

I have only a basic understanding of NSUndoManager.

Please, help!

Thank.

+5
source share
4 answers

, , . -: " ()". , UIGraphicsPushContext UIGraphicsPopContext, , . "push" "pop", , : , , . - - /rect/ .. , - . "" - - . , , UIGraphicsContext, .

. , . , , "" -. - . , , , , , , , .. App Store, , , .

, , , . , ( ) , , . , , . (, "" ), , . , , - , , ..

"--", ( "move shape" ), . , , , . Shape .. , , Shape z- . , , Z. , , , NSUndoManager , .

:

[shapeArray insertObject: newShape atIndex: 5];

NSUndoManager:

[[undoManager prepareInvocationWithTarget: shapeArray] removeObjectAtIndex: 5];

, NSUndoManager , shapeArray . NSUndoManager. , . , . iOS, , , , , , . , , . , , Command Pattern " " , .

, , . -, UIGraphicsContext "". . , , , - StackOverflow. , . , , , ( - iOS.)

+5

UIGraphicsContext . , , , . NSUndoManager , , , , -drawRect:.

+2

undomanager .

NSUndoManager *undoObject;
undoObject =[[NSUndoManager alloc] init];

uigraphics .

[[undoObject prepareWithInvocationTarget:self] performUndoWithObject:currentContext withLastpoint:lastPoint andFirstPoint:firstPoint];

-(void)performUndoWithObject:(CGContextRef )context withLastpoint:(CGPoint)previousLastPoint andFirstPoint:(CGPoint)previousFirstPoint
{
    // necessary steps for undoing operation
}

.

-(void)undoButtonClicked
{
    if([undoObject canUndo])
    {
        [undoObject undo];
    }   
}
+1

How to perform a cancel and redo operation in object c to store an array of values ​​in iOS. and I created a small demo in Undo and Redo Button in a replaced string value. Create two NSMutableArray files in ViewController.h files

@interface ViewController : UIViewController<UITextFieldDelegate>
{
     NSMutableArray *arrUndo, *arrRedo;
}
@property (weak, nonatomic) IBOutlet UIButton *btnUndo;
@property (weak, nonatomic) IBOutlet UIButton *btnRedo;
@property (weak, nonatomic) IBOutlet UITextField *txtEnterValue;
@property (weak, nonatomic) IBOutlet UILabel *lblShowUndoOrRedoValue;
@property (weak, nonatomic) IBOutlet UIButton *btnOK;
- (IBAction)btnUndo:(id)sender;
- (IBAction)btnRedo:(id)sender;
- (IBAction)btnOK:(id)sender;
@end

Deploy the ViewController.m files to add two button action methods to undo or redo.

- (void)viewDidLoad {
    [super viewDidLoad];
    self.txtEnterValue.delegate = self;
    arrUndo = [[NSMutableArray alloc] init];
    arrRedo = [[NSMutableArray alloc] init];
}
-(BOOL)canBecomeFirstResponder {
    return YES;
}
- (IBAction)btnOK:(id)sender {

    NSString *str = [NSString stringWithFormat:@"%@",    [self.txtEnterValue.text description]];
    self.txtEnterValue.text = nil;
    self.lblShowUndoOrRedoValue.text = str;
    [arrUndo addObject:str];
    NSLog(@"Text Value : %@", [arrUndo description]);
}
- (IBAction)btnUndo:(id)sender {

    if ([arrUndo lastObject] == nil) {
        NSLog(@"EMPTY");
    }
    else
    {
    [arrRedo addObject:[arrUndo lastObject]];
    NSLog(@"ArrAdd %@", [arrUndo description]);

    [arrUndo removeLastObject];
    for (NSObject *obj in arrUndo) {
        if ([arrUndo lastObject] == obj) {

            self.lblShowUndoOrRedoValue.text = obj;
        }
    }
    NSLog(@"ArrText %@", [arrUndo description]);
    }
}
- (IBAction)btnRedo:(id)sender {

    if ([arrRedo lastObject] == nil) {
        NSLog(@"EMPTY");
    }
    else
    {
    [arrUndo addObject:[arrRedo lastObject]];
    for (NSObject *obj in arrRedo) {
        if ([arrRedo lastObject] == obj) {

            self.lblShowUndoOrRedoValue.text = obj;

        }
    }
    }
    [arrRedo removeLastObject];
}
+1
source

All Articles