Skip to content

Instantly share code, notes, and snippets.

@neilang
Created December 4, 2011 04:52
Show Gist options
  • Save neilang/1429215 to your computer and use it in GitHub Desktop.
Save neilang/1429215 to your computer and use it in GitHub Desktop.
Spin Segue using CABasicAnimation
#import "NASpinSegue.h"
#import <QuartzCore/QuartzCore.h>
#define SPINS 3.0f
#define DURATION 0.5f
#define TRANSITION_OUT_KEY @"transition out"
#define TRANSITION_IN_KEY @"transition in"
#define TRANSITION_IDENT @"transition type"
@implementation NASpinSegue
-(CALayer *)sourceLayer{
UIViewController *sourceViewController = (UIViewController *)self.sourceViewController;
return sourceViewController.view.layer;
}
-(CALayer *)destinationLayer{
UIViewController *destinationViewController = (UIViewController *)self.destinationViewController;
return destinationViewController.view.layer;
}
- (void)perform{
CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotation.fromValue = [NSNumber numberWithFloat:0.0f];
rotation.toValue = [NSNumber numberWithFloat:M_PI * SPINS];
CABasicAnimation *scaleDown = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleDown.fromValue = [NSNumber numberWithFloat:1.0f];
scaleDown.toValue = [NSNumber numberWithFloat:0.0f];
CABasicAnimation *fadeOut = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeOut.fromValue = [NSNumber numberWithFloat:1.0f];
fadeOut.toValue = [NSNumber numberWithFloat:0.0f];
CAAnimationGroup *transitionOut = [CAAnimationGroup animation];
transitionOut.animations = [NSArray arrayWithObjects:rotation, scaleDown, fadeOut, nil];
transitionOut.duration = DURATION;
transitionOut.delegate = self;
transitionOut.removedOnCompletion = NO;
transitionOut.fillMode = kCAFillModeForwards;
[transitionOut setValue:TRANSITION_OUT_KEY forKey:TRANSITION_IDENT];
[self.sourceLayer addAnimation:transitionOut forKey:TRANSITION_OUT_KEY];
}
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{
NSString *type = [theAnimation valueForKey:TRANSITION_IDENT];
if ([type isEqualToString:TRANSITION_OUT_KEY]) {
CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotation.fromValue = [NSNumber numberWithFloat:0.0f];
rotation.toValue = [NSNumber numberWithFloat:M_PI * SPINS];
CABasicAnimation *scaleUp = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleUp.fromValue = [NSNumber numberWithFloat:0.0f];
scaleUp.toValue = [NSNumber numberWithFloat:1.0f];
CAAnimationGroup *transitionIn = [CAAnimationGroup animation];
transitionIn.animations = [NSArray arrayWithObjects:rotation, scaleUp, nil];
transitionIn.duration = DURATION;
transitionIn.delegate = self;
[transitionIn setValue:TRANSITION_IN_KEY forKey:TRANSITION_IDENT];
[self.sourceViewController presentModalViewController:self.destinationViewController animated:NO];
[self.destinationLayer addAnimation:transitionIn forKey:TRANSITION_IN_KEY];
return;
}
if ([type isEqualToString:TRANSITION_IN_KEY]) {
[self.sourceLayer removeAnimationForKey:TRANSITION_OUT_KEY];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment