Link to home
Start Free TrialLog in
Avatar of James Hancock
James HancockFlag for United States of America

asked on

Xcode How do I do Lines, circles and rectangles?

In a game I'd like to make,
I'd like to be able to draw filled solid rectangles and circles, in xcode for iphone
like in old GWBASIC


Is this possible?
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

1. Create new windows-based project in Xcode, add new Objective-C class derived from UIView, give a name to this class, for example DrawingView.
2. Launch Interface Builder by pressing on the  main xib-file. Find "custom view" in the Library, drag and drop it onto the application window. Set the view size - almost the whole window. In the Size Inspector (Cmd+3) set the autosizing options. In the Identity Inspector (Cmd+6) set the view name as DrawingView. Save the project (Ctrl+S).
3. Switch to Xcode and open DrawingView.m file. In -drawRect method we can add our drawing code. I attached a rectangle. The UIBezierPath class used in this code is, maybe, the most important class when you draw the graphical primitives. It allows a lot (including ellipse you asked about).



- (void)drawRect:(CGRect)rect {
    // Drawing code
	
	CGRect bounds = self.bounds;
	[background set];
	UIRectFill( bounds );
	
	CGRect imageRect;
	imageRect.origin.x = bounds.origin.x + 10;
	imageRect.origin.y = bounds.origin.y + 30;
	imageRect.size.width = bounds.size.width - 20;
	imageRect.size.height = bounds.size.height - 40;
	
	[image drawInRect: imageRect];
	
	UIBezierPath* path = [UIBezierPath bezierPathWithRect: imageRect];
	[border set];
	path.lineWidth = 4;
	[path stroke];	
}

Open in new window

Avatar of James Hancock

ASKER

are there any #includes to consider, because I get errors:


- request for member bounds is something not a structure or union
- background undeclared
-image undeclared
border undeclared

What members should my .h have, then?

Will I be able to have multiple rectangles simultaneously?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
give me a few days plz ( serious upgrades being done in shop on my MacBook )

By The Way....Important Question :)

Is programming an iPad app identical to iPhone app, but for differnt screen size?
mainly, yes. The SDK to compile with is different. The simulator is different.
This thread already contains the solution.
You may ask the related question. I'll be glad to answer.