Feeds

Cloud power harnessed to dump unwanted Mac apps

Five PNGs, one engine

Beginner's guide to SSL certificates

Mac Secrets One of the many cool things about the Mac is the attention to detail that's been put into the user interface.

For example, when you drag something out of the Dock, or customize a toolbar by removing an item from it, you'll see a little explosive "poof" animation, accompanied by an appropriately destructive sound.

Well, here's how to do same thing for yourself, customizing the animation as appropriate.

At this point, some folks will say that it's possible to actually do this in a documented manner thereby undermining what I'm about to say. From Panther onwards, the official way of creating a "poof animation" is to use the AppKit routine NSShowAnimationEffect.

This routine takes a number of arguments including an integer that indicates the wanted effect. This can take only two values - zero or 10. I wondered if it were possibly to use other integer values in order to get alternative animations, but such attempts are ignored. The poof animation is the only one that's present.

It seemed to me that it would be useful to make use of the underlying animation "engine", if only one could find a mechanism for supplying different images for the animation itself.

Poof cloud

The "poofing" code in the Dock application uses five separate frames within a single PNG

After some digging around, I discovered that NSShowAnimationEffect simply makes use of an undocumented class, NSToolbarPoofAnimator. As the name suggests, this class is used by the toolbar to implement poof animation effects. Incidentally, in case you're wondering, the Dock doesn't use this mechanism - it has its own built in "poofing" code.

The most relevant NSToolbarPoofAnimator method is this one:

+ (void) runPoofAtPoint: (NSPoint) point withSize: (NSSize) size
           callbackInfo: (NSDictionary *) callbackInfo;

This class method takes a point where you want the animation to be displayed, a size for the animation, and a call-back dictionary. This dictionary can be nil, but if it's used, it should contain the following keys and objects:

Key: AnimationEffectDelegate

Object: The delegate to be notified when the animation ends.

Key: AnimationEffectDidEndSelector

Object: A string representation of the delegate selector to call

Key: AnimationEffectContextInfo

Object: An NSValue object containing a pointer to optional contextInfo

If this doesn't make sense, carefully read Apple's documentation on NSShowAnimationEffect. You should be able to see how the above three keys enable the NSToolbarPoofAnimator code to notify your delegate when the animation is over.

NSToolbarPoofAnimator works by loading five images into the _poofAnimFrames array, one of the instance variables within the class. These images are named "NSToolbarPoof1", "NSToolbarPoof2" and so on. You can easily view them by just dropping an image view control into Interface Builder, and then setting the image name to the aforementioned strings.

The bad news is that these named images are built right into AppKit itself - they're not even visible as discrete resources within the framework. This means we can't replace them with our own images - right? Wrong.

It turns out that this is very easy to do. Initially, I tried simply creating a new Cocoa project and adding five image resources to the project, with the names given above. I figured that [NSImage imageNamed:] would look inside the application bundle before going to the AppKit framework. This approach didn't work - I suspect certain image names receive special treatment.

In the end, I sub-classed NSToolbarPoofAnimator and added a little extra functionality. My code relies on the fact that the various images get loaded inside the following method:

- (id) initAtPoint: (NSPoint) point withSize: (NSSize) 
 size callbackInfo: (NSDictionary *) callbackInfo;

Once this method has been called, the aforementioned _poofAnimFrames array has been populated with the standard poof images, and there's absolutely nothing to stop us replacing those images with our own.

To try this out, I added the following method to my subclass:

- (void) replaceImagesWithBaseName: (NSString *) baseName
{
        NSInteger idx;
        for (idx = 0; idx < 5; idx++)
        {
          [_poofAnimFrames [idx] release];
          NSString * imageName = [NSString stringWithFormat: 
                               @"%@%d", baseName, idx + 1];
          _poofAnimFrames [idx] = [[NSImage imageNamed: imageName] retain];
          if (_poofAnimFrames [idx] == nil) 
      NSLog (@"Warning: image %@ not found!", imageName);
        }
}

Given a specific base name (such as "Fred") this method locates the image resources "Fred1", "Fred2" and so on, and plugs them into the _poofAnimFrames array.

At this point, I should confess that I am severely challenged from an artistic perspective. Rather than trying to come up with an animation of my own, I borrowed Apple's Dock animation and used Photoshop to give a blue tinge to each "poof bubble".

You can see the result when you run the demo program. I'm not encouraging you to copy Apple's artwork - the intention here is to demonstrate how to animate your artwork using a subclass of the NSToolbarPoofAnimator class.

While I was at it, I thought it would be fun to provide a mechanism for reversing the direction of the animation. Depending on what custom animation you want to use in your own application, this could be useful to indicate that some previous operation has been undone.

You can see how this works by consulting the source code of the demo application, which can be downloaded from here. ®

Remote control for virtualized desktops

More from The Register

next story
PEAK APPLE: iOS 8 is least popular Cupertino mobile OS in all of HUMAN HISTORY
'Nerd release' finally staggers past 50 per cent adoption
Microsoft to bake Skype into IE, without plugins
Redmond thinks the Object Real-Time Communications API for WebRTC is ready to roll
Microsoft promises Windows 10 will mean two-factor auth for all
Sneak peek at security features Redmond's baking into new OS
Mozilla: Spidermonkey ATE Apple's JavaScriptCore, THRASHED Google V8
Moz man claims the win on rivals' own benchmarks
Yes, Virginia, there IS a W3C HTML5 standard – as of now, that is
You asked for it! You begged for it! Then you gave up! And now it's HERE!
FTDI yanks chip-bricking driver from Windows Update, vows to fight on
Next driver to battle fake chips with 'non-invasive' methods
DEATH by PowerPoint: Microsoft warns of 0-day attack hidden in slides
Might put out patch in update, might chuck it out sooner
Ubuntu 14.10 tries pulling a Steve Ballmer on cloudy offerings
Oi, Windows, centOS and openSUSE – behave, we're all friends here
prev story

Whitepapers

Cloud and hybrid-cloud data protection for VMware
Learn how quick and easy it is to configure backups and perform restores for VMware environments.
Getting started with customer-focused identity management
Learn why identity is a fundamental requirement to digital growth, and how without it there is no way to identify and engage customers in a meaningful way.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.
Top 5 reasons to deploy VMware with Tegile
Data demand and the rise of virtualization is challenging IT teams to deliver storage performance, scalability and capacity that can keep up, while maximizing efficiency.
Mitigating web security risk with SSL certificates
Web-based systems are essential tools for running business processes and delivering services to customers.