Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [84]

By Root 834 0
*)event

{

NSPoint p = [event locationInWindow];

currentPoint = [self convertPoint:p fromView:nil];

[self setNeedsDisplay:YES];

}

Add a method to calculate the rectangle based on the two points:

- (NSRect)currentRect

{

float minX = MIN(downPoint.x, currentPoint.x);

float maxX = MAX(downPoint.x, currentPoint.x);

float minY = MIN(downPoint.y, currentPoint.y);

float maxY = MAX(downPoint.y, currentPoint.y);

return NSMakeRect(minX, minY, maxX-minX, maxY-minY);

}

(For some reason, many people mistype that last method. Look at yours once more before going on. If you get it wrong, the results are disappointing.)

Declare the currentRect method in StretchView.h.

So that the user will see something even if he or she has not dragged, initialize downPoint and currentPoint in the setImage: method:

- (void)setImage:(NSImage *)newImage

{

image = newImage;

NSSize imageSize = [newImage size];

downPoint = NSZeroPoint;

currentPoint.x = downPoint.x + imageSize.width;

currentPoint.y = downPoint.y + imageSize.height;

[self setNeedsDisplay:YES];

}

In the drawRect: method, composite the image inside the rectangle:

- (void)drawRect:(NSRect)rect

{

NSRect bounds = [self bounds];

[[NSColor greenColor] set];

[NSBezierPath fillRect:bounds];

[[NSColor whiteColor] set];

[path stroke];

if (image) {

NSRect imageRect;

imageRect.origin = NSZeroPoint;

imageRect.size = [image size];

NSRect drawingRect = [self currentRect];

[image drawInRect:drawingRect

fromRect:imageRect

operation:NSCompositeSourceOver

fraction:opacity];

}

}

Build and run your application. Note that the view doesn’t scroll when you drag past the edge. It would be nice if the scroll view would move to allow users to see where they have dragged to, a technique known as autoscrolling. In the next section, you will add autoscrolling to your application.

Autoscrolling


To add autoscrolling to your application, you will send the message autoscroll: to the clip view when the user drags. You will include the event as an argument. Open StretchView.m and add the following line to the mouseDragged: method:

- (void)mouseDragged:(NSEvent *)event

{

NSPoint p = [event locationInWindow];

currentPoint = [self convertPoint:p fromView:nil];

[self autoscroll:event];

[self setNeedsDisplay:YES];

}

Build and run your application.

Note that autoscrolling happens only as you drag. For smoother autoscrolling, most developers will create a timer that sends the view the autoscroll: method periodically while the user is dragging. Timers are discussed in Chapter 24.

For the More Curious: NSImage


In most cases, it suffices to read in an image, resize it, and composite it onto a view, as you did in this exercise.

An NSImage object has an array of representations. For example, your image might be a drawing of a cow. That drawing can be in PDF, a color bitmap, and a black-and-white bitmap. Each of these versions is an instance of a subclass of NSImageRep. You can add representations to and remove representations from your image. When you sit down to rewrite Adobe Photoshop, you will be manipulating the image representations.

Here is a list of the subclasses of NSImageRep:

• NSBitmapImageRep

• NSEPSImageRep

• NSCachedImageRep

• NSCustomImageRep

• NSPDFImageRep

Although NSImageRep has only five subclasses, it is important to note that NSImage knows how to read dozens of image formats, including all the common formats: PNG, JPEG, PDF, GIF, BMP, TIFF and so on.

Challenge


Create a new document-based application that allows the user to draw ovals in arbitrary locations and sizes. NSBezierPath has the following method:

+ (NSBezierPath *)bezierPathWithOvalInRect:(NSRect)rect;

If you are feeling ambitious, add the ability to save and read files.

If you are feeling extra ambitious, add undo capabilities.

Chapter 19. Keyboard Events


When the user types, where are the corresponding events sent? First, the window manager gets the event and forwards it to the active application.

Return Main Page Previous Page Next Page

®Online Book Reader