13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 7: User Interfaces: The Application Kit 217// On OS X 10.7 and earlier:NSImage * myImage = [[NSImage alloc] initWithSize:NSMakeSize(100.0,100.0)];[myImage lockFocus];[[NSColor whiteColor] setFill];NSRectFill(rect);NSRect square = NSMakeRect(0.0,0.0,50.0,50.0);// draw a square in the lower-left quadrant[[NSColor lightGrayColor] setFill];NSRectFill(square);// draw a square in the upper-right quadrantsquare.origin.x = square.origin.y = 50.0;[[NSColor darkGrayColor] setFill];NSRectFill(square);[myImage unlockFocus];Tip The coordinate system in Cocoa places its origin (the 0,0 coordinate) in the lower-left, likethe strict Cartesian coordinate system. If you want to place it in the upper-left, as has becomecommon in other operating systems, you can make use of the “flipped” attribute to do this foryou. On custom NSView subclasses you can override -isFlipped to gain a top-left origin whendrawing in that view. When drawing to an NSImage, you can pass YES to the flipped: parameterof -imageWithSize:flipped:displayHandler: or you can call -lockFocusFlipped: inlieu of -lockFocus.Now that you have your image, you probably want to put it onscreen, right? Well, the best way todo this is to use NSImageView. This might sound strange; surely this is another level of abstractionand will thus result in slower throughput. Well, it happens that Apple’s engineers have put avast amount of time, effort, and ingenuity into optimizing their views, so using an image view(or a text field for text) actually gains you a whole lot of highly-optimized rendering code that youwould otherwise have to write yourself.But, for the sake of completeness, here’s how a view gets placed on the screen: you either call-drawAtPoint:fromRect:operation:fraction: or -drawInRect:fromRect:operation:fraction:.As interesting as these methods are, however, I strongly advise you to use NSImageView instead,so I’ll skip the details of these methods and let you figure them out yourself; if you genuinelyneed them, you’ll understand the extensive documentation provided by Apple.www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!