Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #import "UIView+Bounce.h"
  2. @implementation UIView (Bounce)
  3. + (CAKeyframeAnimation *)dockBounceAnimationWithViewHeight:(CGFloat)viewHeight {
  4. NSUInteger const kNumFactors = 23;
  5. CGFloat const kFactorsPerSec = 120.0f;
  6. CGFloat const kFactorsMaxValue = 128.0f;
  7.  
  8. CGFloat factors[kNumFactors] = { 0, 83, 100, 114, 124, 138, 156, 184, 156, 138, 124, 114, 100, 83, 32, 0, 0, 18, 28, 32, 28, 18, 0 };
  9.  
  10. NSMutableArray *transforms = [NSMutableArray array];
  11.  
  12. for (NSUInteger i = 0; i < kNumFactors; i++) {
  13. CGFloat positionOffset = factors[i] / kFactorsMaxValue * viewHeight;
  14. CATransform3D transform = CATransform3DMakeTranslation(0.0f, -positionOffset, 0.0f);
  15.  
  16. [transforms addObject:[NSValue valueWithCATransform3D:transform]];
  17. }
  18.  
  19. CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
  20. animation.repeatCount = 1;
  21. animation.duration = kNumFactors * 1.0f / kFactorsPerSec;
  22. animation.fillMode = kCAFillModeForwards;
  23. animation.values = transforms;
  24. animation.removedOnCompletion = YES; // final stage is equal to starting stage
  25. animation.autoreverses = NO;
  26.  
  27. return animation;
  28. }
  29. - (void)bounce:(float)bounceFactor {
  30. CGFloat midHeight = self.frame.size.height * bounceFactor;
  31. CAKeyframeAnimation *animation = [[self class] dockBounceAnimationWithViewHeight:midHeight];
  32. [self.layer addAnimation:animation forKey:@"bouncing"];
  33. }
  34. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement