Skip to content

Instantly share code, notes, and snippets.

@kylefox
Created February 8, 2012 21:44
Show Gist options
  • Save kylefox/1774128 to your computer and use it in GitHub Desktop.
Save kylefox/1774128 to your computer and use it in GitHub Desktop.
Fade in/out a view.
-(void) animateGridView:(BOOL)animateIn {
CAMediaTimingFunction *timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
scaleAnim.timingFunction = opacityAnim.timingFunction = timingFunction;
CATransform3D endTransform;
CATransform3D fullScale = CATransform3DMakeScale(1, 1, 1);
CATransform3D smallScale = CATransform3DMakeScale(0.75, 0.75, 1);
float endOpacity;
if(animateIn) {
scaleAnim.fromValue = [NSValue valueWithCATransform3D:smallScale];
endTransform = fullScale;
opacityAnim.fromValue = [NSNumber numberWithFloat:0];
endOpacity = 1.0;
} else {
scaleAnim.fromValue = [NSValue valueWithCATransform3D:fullScale];
endTransform = smallScale;
opacityAnim.fromValue = [NSNumber numberWithFloat:1];
endOpacity = 0.0;
}
opacityAnim.toValue = [NSNumber numberWithFloat:endOpacity];
scaleAnim.toValue = [NSValue valueWithCATransform3D:endTransform];
// Necessary to retain transformed properties after animation completes.
self.gridView.layer.transform = endTransform;
self.gridView.layer.opacity = endOpacity;
CAAnimationGroup *animGroup = [CAAnimationGroup animation];
animGroup.animations = [NSArray arrayWithObjects:scaleAnim, opacityAnim, nil];
animGroup.duration = 0.5;
[self.gridView.layer addAnimation:animGroup forKey:nil];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment