Brandon
Brandon

Reputation: 127

Drawing a CGRect by touching

I was wondering how to draw a CGRect onto an UIImageView. Here is the code I've got but it doesn't seem to be working. Any suggestions? touch1 and touch2 are both CGPoints and point is a CGRect.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
touch1 = [touch locationInView:self];
touch2 = [touch locationInView:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

}    

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
touch2 = [touch locationInView:self];
point = CGRectMake(touch2.x, touch2.y, 50, 50);
[self setNeedsDisplay];
}

 - (void) drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetFillColorWithColor(context, pointColor.CGColor);
CGContextAddEllipseInRect(context, point);
CGContextDrawPath(context, kCGPathFillStroke);
}

Upvotes: 2

Views: 1878

Answers (3)

Naveen Thunga
Naveen Thunga

Reputation: 3675

I took your code only, its working for me. Just have look at it. And thanks for your code. In this sample i am drawing a rect.

- (void)drawRect:(CGRect)rect 
{
    CGContextRef context = UIGraphicsGetCurrentContext();  
    CGContextSetLineWidth(context, 2.0);  
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);  
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);  
    CGContextAddRect(context, rectFrame);  
    CGContextDrawPath(context, kCGPathFillStroke);  

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    startPoint = [touch locationInView:self];
    rectFrame.origin.x = startPoint.x;
    rectFrame.origin.y = startPoint.y;
}  
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    endPoint = [touch locationInView:self];
    rectFrame.size.width = endPoint.y - startPoint.x;
    rectFrame.size.height = endPoint.y - startPoint.x;

    [self setNeedsDisplay];
 }    

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];
    endPoint = [touch locationInView:self];
    rectFrame.size.width = endPoint.y - startPoint.x;
    rectFrame.size.height = endPoint.y - startPoint.x;

    [self setNeedsDisplay];
 }

Upvotes: 0

slf
slf

Reputation: 22767

Listing 3-1 Code that creates an ellipse by applying a transform to a circle

CGContextScaleCTM(context, 1,2);
CGContextBeginPath(context);
CGContextAddArc(context, 0, 0, 25, 0, 2*M_PI, false);
CGContextStrokePath(context);

Lots more example code in the Quartz2d Example

Upvotes: 0

Ben Zotto
Ben Zotto

Reputation: 71008

What do you see actually happening?

A few notes:

  1. You shouldn't be doing this with a UIImageView-- those are specifically intended to be containers for image files. You should just use a subclass of a regular UIView for this.
  2. In your touchesBegan, you know that touch1 and touch2 will always be set to the same thing, right? You don't seem to ever be using touch1.
  3. point is a misleading variable name for something that is a rect.
  4. Your drawRect is not unreasonable. What is pointColor? If you're drawing black-on-black that might be part of the problem.

Upvotes: 1

Related Questions