Monday, July 18, 2011

Implementing Fade-In Fade-Out animation in NSView on Mac OS X

It is easy to implement Fade-In animation in UIView on iOS. We just need to change alpha value in the animation.

theView.alpha = 0.0f;

[UIView beginAnimations:@"fadeIn" context:nil];

[UIView setAnimationDuration:1.0];

theView.alpha = 1.0f;

[UIView commitAnimations];

But the following code does not work in NSView on Mac Snow leopard.

[theView setAlphaValue:0.0f];

[[theView animator] setAlphaValue:0.8f];

I found addSubView and removeFromSuperview can have fade-in fade-out effects.

[[theSuperview animator] addSubview:theView];

There is a problem. We cannot change the duration time in the following code:

CABasicAnimation *animation = [CABasicAnimation animation];

animation.duration = 3.0f;

[theSuperview setAnimations:[NSDictionary dictionaryWithObjectsAndKeys:animation, @"FadeIn", nil]];

[[theSuperview animator] addSubview:theView];

Now I have the solution.

Fade-in animation:

[NSAnimationContext beginGrouping];

[[NSAnimationContext currentContext] setDuration:2.0];

[[theSuperview animator] addSubview:theView];

[NSAnimationContext endGrouping];

Fade-out animation:

[NSAnimationContext beginGrouping];

[[NSAnimationContext currentContext] setDuration:2.0];

[[theView animator] removeFromSuperview];

[NSAnimationContext endGrouping];

2 comments:

Artem Umanets said...

Hey, your solution have helped me to achieve what I wanted.

Here is the other approach to animate NSView and change the animation duration:


[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:5.0];
[[some_ns_view animator] setAlphaValue:0.0f];
[NSAnimationContext endGrouping];

Anonymous said...

This one works perfectly for me, not the add/remove superview version (but I don't know why, did I miss somerhing ?)