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:
That method:
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:
which indeed does draw a red circle, but that is below the green background and does not move with the green background.
Any ideas?
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?