Showing posts with label Animation. Show all posts
Showing posts with label Animation. Show all posts

Monday, July 18, 2011

The way to stop UIView animations

#import <QuartzCore/QuartzCore.h>

[self.view.layer removeAllAnimations];

Showing an animation when NSView loading

An UIView of one of my iOS app shows a fade-in animation. I implemented this animation in viewDidLoad. When I was porting this app onto Mac OS X, I found there is no viewDidLoad.

I tried adding the fade-in animation in awakeFromNib, but no animation appeared.

My solution: Put the animation code in a method and call it after a short delay.

[self performSelector:@selector(animationShowButton) withObject:nil afterDelay:0.2];

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];