Search code examples
iphonebuttonuiviewcontrollerdrawingquartz-2d

Draw a shape in QuartzView with the press of a button in the iPhone simulator


I want to draw a simple shape in the iPhone simulator when a button is pressed. I can draw it on the screen using the QuartzView without generating an event for the button. However, I want to draw a shape with the touch of a button when an event is generated. I tried to write an IBAction method for the button in both QuartzView and UIViewController. However, I cannot generate an event. Can someone help?


Solution

  • write the following code inside the viewcontroller file. It will work:-

    -(IBAction) getLine:(id) sender{
        CGPoint currentPoint;
        currentPoint.x=45;
        currentPoint.y=458;
        lastPoint.x=445;
        lastPoint.y=534;
        UIGraphicsBeginImageContext(self.drawImage.frame.size);
        [drawImage.image drawInRect:CGRectMake(0, 0, self.drawImage.frame.size.width, self.drawImage.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.5, 0.6, 1.0);
        CGContextBeginPath(UIGraphicsGetCurrentContext());
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
    }
    

    Feel free to ask anything more.