5

I'm currently putting the final touches on a project.

A lot (if not all) of the UI logic currently relies on Cocoa Bindings. Some of the user interface elements (labels, buttons, etc.) have their "Hidden" bindings defined. When certain events are triggered, these elements visibility is toggled.

I'm trying to animate the visibility change (by animating the opacity and maybe even the scale). This could easily be accomplished in a number of ways, either by setting the relevant layer properties, adding the animations to the layer, etc. However, since I'm trying to totally rely on the bindings behavior I "can't" really do this directly.

I tried an implementation using Layer actions, by defining actions for the keys kCAOnOrderIn and kCAOnOrderOut on the relevant elements, but it really didn't work, as the setHidden: is most likely being triggered on the NSView instead of the CALayer -- which makes sense.

So, my question is: how would you animate setHidden: on a NSView, when setHidden: is being invoked by the Cocoa Bindings.

Thank you.

phluid
  • 96
  • 1
  • 6

3 Answers3

4

This will fade out an NSView...

[[someView animator] setAlphaValue:0.0f];
AlBeebe
  • 8,101
  • 3
  • 51
  • 65
0

Animating setHidden will have no visual effect since it's either on or off. If you want to animate visibility, use setAlpha (or setOpacity on the layer) instead. These take a value between 0.0 and 1.0. If you need the hidden flag to get set for the sake of state information, call -performSelector:withObject:afterDelay passing it a selector that sets the hidden value to whatever you need it to be after the animation has completed. Alternatively you can set up a delegate for explicit animation to be called back when the animation finishes and call setHidden then.

Matt Long
  • 24,438
  • 4
  • 73
  • 99
  • `setHidden:` on a CALayer *is* animatable. The default action for the order in/out keys actually use a CATransition causing the layers opacity to animate. – phluid Feb 02 '11 at 23:20
  • Yes. Hidden is animatable in the sense that you can change the values, however, there are only two possible values. Core Animation interpolates in-between values. With hidden, it's either YES or NO. But what it sounds like you want it to do is automatically trigger a fade animation when setHidden is called. Is that correct? If so, I don't think you can do that with only a binding. You'll probably have to write code. You can try to bind to the alpha property and set it to either 0 or 1. Seems that might give you want you're looking for, but I'm not sure how you have things connected. – Matt Long Feb 03 '11 at 16:17
0

I would suggest taking a look at NSViewAnimation. It takes any NSView and can animate the frame, size or visibility.

sosborn
  • 14,676
  • 2
  • 42
  • 46