iOS view problems

Status
Not open for further replies.
I'm trying out iOS programming and want to draw various shapes on screen and be able to drag them around, eventually to make a simple puzzle.

I've come to understand navigation with segues but I am finding views somewhat hard to understand. I have a view based on UIView, called PuzzleView, and want this view to contain a lot of shapes, created programmatically.

I override the drawRect method in puzzleView and call a method called drawPickerbox:

Code:
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    [self drawPickerbox:context];
}

That method:

Code:
-(void)drawPickerbox:(CGContextRef)context
{
    PuzzleObject *pz = [[PuzzleObject alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    [pz setOpaque:NO];
    [pz setBackgroundColor:[UIColor greenColor]];
    [self addSubview:pz];
    [pz drawCircle:_shapeSize ofColor:([UIColor redColor].CGColor) inContext:context];
    [pz setUserInteractionEnabled:YES];
    
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                                    action:@selector(panHandler:)];
    [panRecognizer setMaximumNumberOfTouches:2];
    [panRecognizer setDelegate:self];
    [pz addGestureRecognizer:panRecognizer];
}

So far so good, I get a rectangle with green background of correct size and am able to drag it.

But the PuzzleObject it self has a drawCircle method:

Code:
-(void)drawCircle:(NSNumber*)size ofColor:(CGColorRef)color inContext:(CGContextRef)context
{
    subContext = context;
    CGRect fr = [self frame];
    UIGraphicsPushContext(subContext);
    CGContextSetStrokeColorWithColor(subContext, color);
    CGContextSetFillColorWithColor(subContext, color);
    CGContextAddArc(subContext, fr.origin.x+([size floatValue])/2, fr.origin.y+([size floatValue])/2, ([size floatValue]), 0, 2*M_PI, 1);
    CGContextFillPath(subContext);
    CGContextStrokePath(subContext);
    UIGraphicsPopContext();
}

which indeed does draw a red circle, but that is below the green background and does not move with the green background.

Any ideas?
 

Ember

Ars Centurion
346
Subscriptor++
First of all, you misunderstands view hierarchy and drawing.
It seems PuzzleObject is a subclass of UIView. You should manage (add, remove, reorder and move) subviews outside of drawRect:, usually in -viewDidLoad of a view controller and event handlers.
Rename -drawPickerbox: to -setupPickerbox and call it from -viewDidLoad of the view controller that loads PuzzleView or from -intWithRect: of PuzzleView class.

-(void)drawCircle:eek:fColor:inContext: of PuzzleObject should be called by -drawRect: of PuzzleObject, not by PuzzleView.

Remember that each view must draw itself but not its subviews. Each subview's drawRect: will be called by the UIKit as needed. You just manage the hierarchy and position of subviews.
 
Status
Not open for further replies.