Online Book Reader

Home Category

Learn Objective-C on the Mac - Mark Dalrymple [133]

By Root 970 0
makes the outline and head stretch out to accommodate the new bounds, while the facial features are still stuck in their rigid little world.

Also, you’ll notice that the lines are the exact same thickness after resizing, which means that their relative thickness, compared to the overall size of the view, has changed. If you made this view really huge, the lines would seem extraordinarily thin.

Fortunately, there’s an easy way to remedy all of this. Remember earlier in this chapter, when we mentioned the distinction between a view’s frame and bounds rects? The frame, you may recall, defines the view’s position and size within its superview. The bounds, however, determines the extent and position of the coordinate system within the view. If we can configure things so that the view’s bounds rect never changes, then it will always draw the exact same content, but perfectly stretched to match the actual frame it’s drawing in!

To do this, we need to implement two simple steps: grab a copy of the original bounds rect when the view is created, and then manually set the bounds to that original rect every time the view is resized. So, let’s add an instance variable (for holding the original rect value) to SmileyView.h, complete the initWithFrame: method and implement the setFrameSize: method as shown here:

The setFrameSize: method is one that gets called during live dragging, while the user is resizing a window. What we’re doing here is resetting the view’s bounds rect every step of the way, so that when it comes time to draw, all the drawing will occur based on the original bounds. Now Build & Run, resize the window, and witness the magic (Figure 13-8).

Figure 13-8. A real stretch

As you can see, everything stretches perfectly faithfully, including the widths of the lines. Also, everything is rendered to match the actual display resolution. You can stretch that out as big as you want, and you’ll always see perfectly anti-aliased curves. Whatever geometry we specify for our bounds, will be adjusted to match the frame we’re sitting in. What you’re seeing here is, in fact, a two-dimensional transformation.

Apart from being a nice way to deal with resizing, this built-in transformation possibility means that you can do your drawing at whatever scale you’re comfortable with, just by setting the bounds rect. If you want to do your graphics at pixel resolution you can do so, but if you’re plotting details of a mathematical curve ranging from 0.0 to 0.1, you can set your bounds accordingly, and don’t have to multiply all your display values to make them match the screen coordinates.

LOLmaker


Now that you’ve got a basic understanding of drawing in an NSView and manipulating its geometry, let’s move on to a new project: LOLmaker. LOLmaker is a simple application that lets you create your own LOLcat-style imagery by just dragging in an image and typing the text you want it to contain. It’s not rocket science, but it will introduce you to a few more issues around drawing in an NSView.

A FEW WORDS ABOUT LOLCATS

In case you’ve missed out on the whole LOLcat meme, or are reading this book in a future world where LOLcats have been forgotten, a LOLcat is basically a picture of a cat (or other animal) with a humorous caption, typically written in a style mocking the “Internet slang” of the early 21st century.

We like LOLcats simply because they are pictures of cats that make us laugh out loud. Please don’t think that we’d stoop to the level of including LOLcats content in our book just to sell more copies! It’s a well-known fact that most LOLcats fans are also pirates, and will only read this book in the form of a stolen PDF.

First Steps to LOL


Start by creating a new basic Cocoa project in Xcode, no Document or Core Data support this time. Although both of those could be put to good use in this project, for now we’re going to focus on just the drawing aspects. This project won’t actually save anything. Name the project LOLmaker, and after creating it use the Inspector panel to turn on GC as usual.

If you

Return Main Page Previous Page Next Page

®Online Book Reader