Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Paint app with undo functionality

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 445
    Comment on it

    One way to add undo functionality to a paint app is by storing state of the view and taking the view back to previous state on undoing. We can store state in arrays. And the state here will be all the lines being drawn and properties of the lines like stroke color, width, brush strokes etc. For this we will have to create a subclass of UIView and override its drawRect: method.

    - (void)drawRect:(CGRect)rect 
    {
        //Get Context
        CGContextRef context=UIGraphicsGetCurrentContext();
        //Set pen to take
        CGContextSetLineCap(context, kCGLineCapRound);
        //Set at the inflection point draw a line connecting smooth
        CGContextSetLineJoin(context, kCGLineJoinRound);
        //For first time
        static BOOL allline=NO;
        if (allline==NO)
        {
            myallline=[[NSMutableArray alloc] initWithCapacity:10];
            myallColor=[[NSMutableArray alloc] initWithCapacity:10];
            myallwidth=[[NSMutableArray alloc] initWithCapacity:10];
            allline=YES;
        }
        //Draw last state
        if ([myallline count]>0)
        {
            for (int i=0; i<[myallline count]; i++)
            {
                NSArray* tempArray=[NSArray arrayWithArray:[myallline objectAtIndex:i]];
                //----------------------------------------------------------------
                if ([myallColor count]>0)
                {
                    Intsegmentcolor=[[myallColor objectAtIndex:i] intValue];
                    Intsegmentwidth=[[myallwidth objectAtIndex:i]floatValue]+1;
                }
                //-----------------------------------------------------------------
                if ([tempArray count]>1)
                {
                    CGContextBeginPath(context);
                    CGPoint myStartPoint=[[tempArray objectAtIndex:0] CGPointValue];
                    CGContextMoveToPoint(context, myStartPoint.x, myStartPoint.y);
    
                    for (int j=0; j<[tempArray count]-1; j++)
                    {
                        CGPoint myEndPoint=[[tempArray objectAtIndex:j+1] CGPointValue];
                        //--------------------------------------------------------
                        CGContextAddLineToPoint(context, myEndPoint.x,myEndPoint.y);    
                    }
                    [self IntsegmentColor];
                    CGContextSetStrokeColorWithColor(context, segmentColor);
                    //-------------------------------------------------------
                    CGContextSetLineWidth(context, Intsegmentwidth);
                    CGContextStrokePath(context);
                }
            }
        }
        //Draw current line
        if ([myallpoint count]>1)
        {
            CGContextBeginPath(context);
            //-------------------------
            //Starting point
            //------------------------
            CGPoint myStartPoint=[[myallpoint objectAtIndex:0]   CGPointValue];
            CGContextMoveToPoint(context,    myStartPoint.x, myStartPoint.y);
            //All the move to join the array of points
            for (int i=0; i<[myallpoint count]-1; i++)
            {
                CGPoint myEndPoint=  [[myallpoint objectAtIndex:i+1] CGPointValue];
                CGContextAddLineToPoint(context, myEndPoint.x,   myEndPoint.y);
            }
            //In an array of colors and brush size which does not take the corresponding values
            Intsegmentcolor=[[myallColor lastObject] intValue];
            Intsegmentwidth=[[myallwidth lastObject]floatValue]+1;
    
            //-------------------------------------------
            //Drawing pen color
            [self IntsegmentColor];
            CGContextSetStrokeColorWithColor(context, segmentColor);
            CGContextSetFillColorWithColor (context,  segmentColor);
            //-------------------------------------------
            //Drawing pen width
            CGContextSetLineWidth(context, Intsegmentwidth);
            //All points inside the array drawn
            CGContextStrokePath(context);
        }
    }
    

    myallline is array that stores all the lines that are drawn on the view. myallpoint is array that stores all points of current line that is being drawn. It is saved in myallline after the line is completed. myallColor and myallwidth are arrays for storing all the colors and width for corresponding lines on an index.

    Values are stored into these array from the touchesBegan, touchesMoved and touchesEnded methods that have to be implemented in the ViewController using following methods :

    1 When touches begin, initialize myallpoint array that will store all points of the current line being drawn.

    -(void)Introductionpoint1
    {
        myallpoint=[[NSMutableArray alloc] initWithCapacity:10];
    }

    2 When touch is moved, store the points touched to myallpoint array. Also invoke the drawRect method of the view by calling setNeedsDisplay on the view object so that it draws last state.

    -(void)Introductionpoint3:(CGPoint)sender
    {
        NSValue* pointvalue=[NSValue valueWithCGPoint:sender];
        [myallpoint addObject:[pointvalue retain]];
        [pointvalue release];
    }

    3 When touch ends add the points array to myallline array

    -(void)Introductionpoint2
    {
        [myallline addObject:myallpoint];
    }

    The source code can be downloaded from here. Palette is the UIView subclass which implements the above code.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: