Posts Tagged ‘Cocoa’

Mouse tracking in NSView

Saturday, February 13th, 2010

Tracking the position of the mouse does seem like a trivial task. At a first glance it seemed like it would be a matter to override the -mouseMoved: selector in my NSView subclass and make sure the window accepts mouseMoved events by calling [[self window] setAcceptsMouseMovedEvents:YES]; when the view had loaded. Well, not exactly. I tried every conceivable combination of placements of these peices of code, including making the view the first responder, but I could never manage to intercept the mouseMoved event.

Then I found out about NSTrackingArea. Perhaps I was being overly ignorant, but from reading the event handling docs, it was not clear to me that I would need to use a tracking area to be able to recieve mouseMoved events.

Using NSTrackingArea is pretty straight forward, although at first I got a cryptic error message in the console:

trackingArea options 0x2 do not specify when the tracking area is active

As it turns out, NSTrackingArea requires a combination of several options to work properly. In this case I needed to supply “NSTrackingMouseMoved+NSTrackingActiveInKeyWindow” in the options: attribute.

Putting the following code in the -viewDidMoveToWindow method in my NSView subclass finally managed to get the view to recieve mouseMoved events:

NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:[self frame]
	options:NSTrackingMouseMoved+NSTrackingActiveInKeyWindow
	owner:self
	userInfo:nil];
[self addTrackingArea:trackingArea];
[self becomeFirstResponder];

ImageSplitter.app

Friday, January 29th, 2010

Did you ever feel the need to split a big image into a bunch of equally sized slices? Well, I do sometimes, so I decided to make a permanent solution to the problem. I prestent to you: ImageSplitter.app

ImageSplitter a very simple Cocoa application that does one thing. Can you guess it? Correct, it splits images :)

I needed a simple way to split large background images into smaller chunks for an iPhone game that I’m currently working on. Im sure there are a lot of apps out there that could do this for me, but none that I could find that did only that, and in the specific way that I needed.

Well, now there is!

Screen shot 2010-01-29 at 13.15.24

Simply drag the image onto the image well, set the slice size and format, and hit Split. The image will split up into a series of files named mySplittedImage_0.png, mySplittedImage_1.png etc.

Happy splitting!