views:

241

answers:

1

Okay, so, I'm all new to iPhone and stuff, but I'm not even at programming, I have many years of experience with ActionScript 2.0 and 3.0, I want to set up a view, that I can also pass variables to. The view is gonna draw everything with Quartz.

I tried to make another game where I tried to add a pointer to a NSMutableArray to the view class, but it didn't work cause I didn't know how to store an actual class.

I wanted to do like:

[myView setNeedsDisplay];

but I had to do

[(View*)self.view setNeedsDisplay];

didn't really work out in the end...

Okay, so now I got:

- (void) viewDidLoad {
    [super viewDidLoad];
 viewClass = [[View class] retain];
 gameView = [[[viewClass alloc] initWithFrame: CGRectZero] retain];
 [gameView setNeedsDisplay];
}

This is in my drawInContext:context, which is fired by drawRect:

Also, my drawRect does [self drawInContext: UIGraphicsGetCurrentContext()];

CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextSetLineWidth(context, 3.0);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGMutablePathRef aPath = CGPathCreateMutable();
CGPathMoveToPoint(aPath, nil, 5, 5);
CGPathAddLineToPoint(aPath, nil, 45, 43);
CGContextAddPath(context, aPath);
CGContextStrokePath(context);

Nothing happens.

Help?

Oh yeah, I get this error: : invalid context for each time I use those functions. :[

Thanks!

+2  A: 

You're overcomplicating it. Don't call drawInContext from drawRect. Instead just do this in your drawRect method:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
    CGContextSetLineWidth(context, 3.0);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineJoin(context, kCGLineJoinRound);
    CGContextMoveToPoint(context, 5, 5);
    CGContextAddLineToPoint(context, 45, 43);
    CGContextStrokePath(context);
}

There's a couple things you should note in that code. Whenever you do custom drawing in a UIView by overriding drawRect, use UIGraphicsGetCurrentContext to get a reference to the current context. Second, when using CGContext there is no need to create a new CGPath. CGContext has the same drawing functions as CGPath, see the docs here.

To create a view all you need to do is this:

gameView = [[YourView alloc] initWithFrame:CGRectZero];

Where YourView is your UIView subclass. You don't need to create a separate class reference (viewClass), and you don't need to call retain on the UIView object either, because the retain count is increased already when you create it. Also note that CGRectZero is a rectangle of size (0, 0). If you want your view to be visible then you need to supply an appropriately sized CGRect instead of CGRectZero.

Hope this clarifies things a bit.

macatomy
Ah, it works! Thanks!And I also found out I had forgotten to do a specific thing in the XIB file haha. Such as saying the UIView is View. >______>Well, thanks anyway!
Johannes Jensen