views:

478

answers:

1

I want to use a focus ring animation as an indicator of incorrect data in field. So I'm sending becomeFirstResponder: to field and want focus ring to fade from red to default color.

I'm wrestling with Core Animation but still have not found any way to do it. Is it possible?

A: 

I'm not sure if this strategy follows the HIG, its often more common to do something like display a persistent icon indicating a field doesn't validate next to the field, but it shouldn't be too hard to get the effect you're seeking.

It might be easier to use a simple NSAnimation here instead of using Core Animation. The standard code for drawing a focus ring generally goes something like the following:

[NSGraphicsContext saveGraphicsState];
NSSetFocusRingStyle(NSFocusRingOnly);
[[NSColor clearColor] set];
[[NSBezierPath bezierPathWithRect:focusRect] fill];
[NSGraphicsContext restoreGraphicsState];

This code would be implemented in the drawRect: method in custom subclass of your control.

In order to draw a custom colored focus ring, you'll need to draw the rectangle yourself, and won't be able to benefit from the NSSetFocusRingStyle function. The color would be driven off of the NSAnimation, which would also set the control to repaint itself. Because you're not using Cocoa's facilities to draw the focus ring, you'll also probably need to inset the content of your view so you'll have space to draw the ring.

More information regarding NSAnimations is available in the Animation Programming Guide for Cocoa

Joey Hagedorn
Thanks Joey. Looks like the only way to do what I want is to subclass and draw manually as you said. I thought I can do that with some property animation but still no luck with that.
Anton
Hmm. This code draws a filled rect inside a control but not a focus ring. Focus ring invariably draws above this rect and doesn't change color.
Anton
I may have oversimplified this a bit in my original answer. I do think you'll be best off implementing custom drawing in the drawRect: method and an NSAnimation, but using NSSetFocusRingStyle() will only be useful in drawing a focus ring with normal appearance. You'll need to do more work to simulate the Cocoa focus ring style, and actually draw inside the bounds of your view; perhaps inset the rest of the content by the margin you'll need to draw the focus ring. I've edited my answer above to reflect reality a bit.
Joey Hagedorn