0

Code:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIColor *grayColor = [UIColor colorWithRed: 0.8980392157 green: 0.8980392157 blue: 0.8980392157 alpha: 1.0];

    [grayColor set];

    CGContextSetLineWidth(context, 2.0);
    CGContextStrokeEllipseInRect(context, CGRectMake(100, 190, 101, 101));
}

I did draw a circle.When user moves finger on it i want to draw line on it.I googled lot about this.But i could not find any solution.any help will be appreciated.thanks in advance.

Leandros
  • 16,805
  • 9
  • 69
  • 108

2 Answers2

0

You should look into these:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

These are the methods to track the touches of a UIView.

Leandros
  • 16,805
  • 9
  • 69
  • 108
0
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

        UITouch *touch = [touches anyObject];
        CGPoint tapLocation = [touch locationInView:self.superDrawingLayer];  
        lastPoint = tapLocation;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        UITouch *touch = [touches anyObject];
        CGPoint tapLocation = [touch locationInView:self.superDrawingLayer];
        currentPoint = tapLocation;

        UIGraphicsBeginImageContext(self.superDrawingLayer.frame.size);
        [self.superDrawingLayer.image drawInRect:CGRectMake(0, 0, self.superDrawingLayer.frame.size.width, self.superDrawingLayer.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), drwaingWidth);
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeColor);
        CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor colorWithRed:1.0f green:0.764705f blue:0 alpha:1.0] CGColor]);
        CGContextBeginPath(UIGraphicsGetCurrentContext());
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());


        self.superDrawingLayer.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        lastPoint = tapLocation;
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

}

//Here superDrawingLayer is the ImageView on which lines will be drawn.

Sandeep Singh
  • 268
  • 1
  • 9