Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Starting with a View-based template, create a project named <strong>Drawer</strong>. Add a UIView class to your project. Name it <strong>SquareView</strong> (.h and .m).</p> <p>Double-click <strong>DrawerViewController.xib</strong> to open it in <em>Interface Builder</em>. Change the generic view there to <strong>SquareView</strong> in the Identity Inspector (command-4) using the <strong>Class</strong> popup menu. Save and go back to <strong>Xcode</strong>.</p> <p>Put this code in the drawRect: method of your <strong>SquareView.m</strong> file to draw a large, crooked, empty yellow rectangle and a small, green, transparent square:</p> <pre><code>- (void)drawRect:(CGRect)rect; { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetRGBStrokeColor(context, 1.0, 1.0, 0.0, 1.0); // yellow line CGContextBeginPath(context); CGContextMoveToPoint(context, 50.0, 50.0); //start point CGContextAddLineToPoint(context, 250.0, 100.0); CGContextAddLineToPoint(context, 250.0, 350.0); CGContextAddLineToPoint(context, 50.0, 350.0); // end path CGContextClosePath(context); // close path CGContextSetLineWidth(context, 8.0); // this is set from now on until you explicitly change it CGContextStrokePath(context); // do actual stroking CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 0.5); // green color, half transparent CGContextFillRect(context, CGRectMake(20.0, 250.0, 128.0, 128.0)); // a square at the bottom left-hand corner } </code></pre> <p>You don't have to call this method for the drawing to happen. Your view controller will tell the view to draw itself at least once when the program launches and the NIB files are activated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload