views:

57

answers:

3

I want to place some "handles" on a UIView I have on-screen.

So, I'm creating more UIViews, one at each corner of the UIView's frame rectangle, and I simply want to draw a circle that "fills" the rectangle that is the frame of the "handle" UIView.

Here's what I mean:

How I create the "HandleView":

CGPoint upperLeft = CGPointMake([[self viewToMove] frame].origin.x - 5, [[self viewToMove] frame].origin.y - 5);
HandleView *uL = [[HandleView alloc] initWithFrame:CGRectMake(upperLeft.x, upperLeft.y, 10, 10)];
[self setUpperLeftHandle:uL];
[uL release];
[[self view] addSubview:[self upperLeftHandle]];

drawRect: for HandleView:

- (void)drawRect:(CGRect)rect {
   // Drawing code
   CGContextRef context = UIGraphicsGetCurrentContext();
   [[UIColor orangeColor] set];
   CGContextFillEllipseInRect(context, [self frame]);
}

What I'm getting is just a set of black rectangles where I place the HandleViews. I'm not sure why they're black, but I can change the color by changing the [HandleView backgroundColor] property. I cannot, though, get anything to DRAW on this view. Calling setNeedsDisplay doesn't seem to make any difference.

Also, the drawRect: method IS being called, so there's a problem with the code there, probably not anywhere else.

It's been a while since I've messed with custom drawing, but I don't remember it being this hard.

What am I missing?

Thanks!!!

Update:

Modified code to this:

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    [self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 100, 100, 100, 1.0);
    CGContextFillEllipseInRect(context, rect);
}

Now, I see the circle, but only where it overlays another view. Where it's just sitting on top of the "background" UIView, I get nothing I can see. Also, even though the color is (100, 100, 100), it shows up white/light-light-gray.

What's the deal?

+2  A: 

This might work:

- (void)drawRect:(CGRect)rect {
    // Drawing code
    [[UIColor orangeColor] setFill];
    [[UIBezierPath bezierPathWithOvalInRect:rect] fill];
}
aegzorz
Nice! Thanks! It works!
mbm30075
A: 

The problem is with the following line:

CGContextFillEllipseInRect(context, [self frame]);

You need to change this to the following:

CGContextFillEllipseInRect(context, [self bounds]);

Origin of frame is the left-top corner location of view in the parent. So, it can be any value within the limits of the size of parent view.

So, while drawing with frame, you will be offsetting Fillellipse rect location which happens to be beyond the visible rect of the handle view.

Hence, you were not able to see anything.

SegFault
A: 

As for the color showing up as white, it's because CGContextSetRGBFillColor() expects values from 0.0 to 1.0 for the color component values (not 0-255). So, by passing 100, you were effectively passing 1.0, which was creating a white color.

Kelan