Skip to content

Instantly share code, notes, and snippets.

@kenshin03
Created March 5, 2013 08:02
Show Gist options
  • Save kenshin03/5088712 to your computer and use it in GitHub Desktop.
Save kenshin03/5088712 to your computer and use it in GitHub Desktop.
A bouncy movein effect for image grids that I experimented with. Don't want to lose it. Demo: https://vimeo.com/61078361
- (void)pushInViewsWithBounce {
self.collectionView.hidden = NO;
for (NSInteger i=0; i<100; i++){
UIView * viewToAnimate = [self.view viewWithTag:i];
if ([viewToAnimate isKindOfClass:[AFCollectionViewCell class]]){
viewToAnimate.hidden = YES;
CALayer * layerToMove = viewToAnimate.layer;
CGPoint endPoint = CGPointMake(layerToMove.position.x, layerToMove.position.y);
CGPoint currentPoint = CGPointMake(layerToMove.position.x, layerToMove.position.y+1000.0f);
CAKeyframeAnimation* moveAnimation;
moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnimation.duration = .8f;
moveAnimation.removedOnCompletion = NO;
moveAnimation.fillMode = kCAFillModeBackwards;
moveAnimation.calculationMode = kCAAnimationLinear;
moveAnimation.values = @[[NSValue valueWithCGPoint:currentPoint],
[NSValue valueWithCGPoint:CGPointMake(endPoint.x, endPoint.y-10)],
[NSValue valueWithCGPoint:CGPointMake(endPoint.x, endPoint.y+10)],
[NSValue valueWithCGPoint:CGPointMake(endPoint.x, endPoint.y-5)],
[NSValue valueWithCGPoint:CGPointMake(endPoint.x, endPoint.y+5)],
[NSValue valueWithCGPoint:CGPointMake(endPoint.x, endPoint.y-1)],
[NSValue valueWithCGPoint:CGPointMake(endPoint.x, endPoint.y+1)],
[NSValue valueWithCGPoint:endPoint]];
[moveAnimation setTimingFunctions:@[
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
]];
double delayInSeconds = 0.15f * i;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
viewToAnimate.hidden = NO;
[viewToAnimate.layer addAnimation:moveAnimation forKey:@"moveIn"];
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment