Permalink
Browse files

SKBounceAnimation

  • Loading branch information...
zacaltman committed Dec 19, 2012
1 parent c139bf2 commit b85e7b8331db6c1f1949accc612a6e9f478661e9
Showing with 2,118 additions and 0 deletions.
  1. +15 −0 External/SKBounceAnimation/.gitignore
  2. +19 −0 External/SKBounceAnimation/LICENSE.md
  3. +51 −0 External/SKBounceAnimation/README.md
  4. +511 −0 External/SKBounceAnimation/SKBounceAnimation.xcodeproj/project.pbxproj
  5. +16 −0 External/SKBounceAnimation/SKBounceAnimation/ColorBounceController.h
  6. +69 −0 External/SKBounceAnimation/SKBounceAnimation/ColorBounceController.m
  7. +16 −0 External/SKBounceAnimation/SKBounceAnimation/OneAxisBounceController.h
  8. +67 −0 External/SKBounceAnimation/SKBounceAnimation/OneAxisBounceController.m
  9. +16 −0 External/SKBounceAnimation/SKBounceAnimation/RectBounceController.h
  10. +86 −0 External/SKBounceAnimation/SKBounceAnimation/RectBounceController.m
  11. +19 −0 External/SKBounceAnimation/SKBounceAnimation/SKAppDelegate.h
  12. +56 −0 External/SKBounceAnimation/SKBounceAnimation/SKAppDelegate.m
  13. +38 −0 External/SKBounceAnimation/SKBounceAnimation/SKBounceAnimation-Info.plist
  14. +14 −0 External/SKBounceAnimation/SKBounceAnimation/SKBounceAnimation-Prefix.pch
  15. +26 −0 External/SKBounceAnimation/SKBounceAnimation/SKBounceAnimation.h
  16. +381 −0 External/SKBounceAnimation/SKBounceAnimation/SKBounceAnimation.m
  17. +23 −0 External/SKBounceAnimation/SKBounceAnimation/SKRootViewController.h
  18. +85 −0 External/SKBounceAnimation/SKBounceAnimation/SKRootViewController.m
  19. +16 −0 External/SKBounceAnimation/SKBounceAnimation/ScaleBounceController.h
  20. +76 −0 External/SKBounceAnimation/SKBounceAnimation/ScaleBounceController.m
  21. +16 −0 External/SKBounceAnimation/SKBounceAnimation/ScaleRotateBounceController.h
  22. +79 −0 External/SKBounceAnimation/SKBounceAnimation/ScaleRotateBounceController.m
  23. +16 −0 External/SKBounceAnimation/SKBounceAnimation/SizeBounceController.h
  24. +68 −0 External/SKBounceAnimation/SKBounceAnimation/SizeBounceController.m
  25. +16 −0 External/SKBounceAnimation/SKBounceAnimation/TwoAxisBounceController.h
  26. +67 −0 External/SKBounceAnimation/SKBounceAnimation/TwoAxisBounceController.m
  27. +2 −0 External/SKBounceAnimation/SKBounceAnimation/en.lproj/InfoPlist.strings
  28. +167 −0 External/SKBounceAnimation/SKBounceAnimation/en.lproj/SKRootViewController.xib
  29. +18 −0 External/SKBounceAnimation/SKBounceAnimation/main.m
  30. +22 −0 External/SKBounceAnimation/SKBounceAnimationTests/SKBounceAnimationTests-Info.plist
  31. +13 −0 External/SKBounceAnimation/SKBounceAnimationTests/SKBounceAnimationTests.h
  32. +32 −0 External/SKBounceAnimation/SKBounceAnimationTests/SKBounceAnimationTests.m
  33. +2 −0 External/SKBounceAnimation/SKBounceAnimationTests/en.lproj/InfoPlist.strings
@@ -0,0 +1,15 @@
+# Xcode
+build/*
+*.pbxuser
+!default.pbxuser
+*.mode1v3
+!default.mode1v3
+*.mode2v3
+!default.mode2v3
+*.perspectivev3
+!default.perspectivev3
+*.xcworkspace
+!default.xcworkspace
+xcuserdata
+profile
+*.moved-aside
@@ -0,0 +1,19 @@
+Copyright (c) 2012 Soroush Khanlou (http://khanlou.com/)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,51 @@
+# SKBounceAnimation
+
+`SKBounceAnimation` is a `CAKeyframeAnimation` subclass that creates an animation for you based on start and end values and a number of bounces. It’s based on the math and technology in this blogpost: [khanlou.com/2012/01/cakeyframeanimation-make-it-bounce/](http://khanlou.com/2012/01/cakeyframeanimation-make-it-bounce/) which in turn was based partially on Matt Gallagher’s work here: [cocoawithlove.com/2008/09/parametric-acceleration-curves-in-core.html](http://cocoawithlove.com/2008/09/parametric-acceleration-curves-in-core.html).
+
+## Usage
+
+Basic code is simple:
+
+
+
+ NSString *keyPath = @"position.y";
+ id finalValue = [NSNumber numberWithFloat:300];
+ [view.layer setValue:finalValue forKeyPath:keyPath];
+
+ SKBounceAnimation *bounceAnimation = [SKBounceAnimation animationWithKeyPath:keyPath];
+ bounceAnimation.fromValue = [NSNumber numberWithFloat:view.center.x];
+ bounceAnimation.toValue = finalValue;
+ bounceAnimation.duration = 0.5f;
+ bounceAnimation.numberOfBounces = 2;
+
+ [view.layer addAnimation:bounceAnimation forKey:@"someKey"];
+
+We set the value of our keypath to the final value, and then perform the animation. When the animation finishes, it is automatically removed from the layer, and the `finalValue` takes over. If you do not use `-(void)setValue:forKeyPath:`, the original value for the keyPath will take over and the animation will snap back to original location after the animation is over.
+
+## Math
+
+The math is simple. Check out the [blogpost](http://khanlou.com/2012/01/cakeyframeanimation-make-it-bounce/) and [the informational post](http://khanlou.com/2012/01/dampers-and-their-role-in-physical-models/) preceding it for exact details, but essentially the system behaves with oscillating exponential decay in the form of the equation: `x = Ae^(-αt)•cos(ωt) + B`.
+
+A is the difference between start and end values, B is the end value, α is determined by the number of frames required to get the exponential decay portion to close enough to 0, and ω is determined by the number of periods required to get the desired number of bounces.
+
+## Extras
+
+`shouldOvershoot` is a property that you can change. It defaults to `YES`; if you set it to `NO`, the animation will bounce as if it were hitting a wall, instead of overshooting the target value and bouncing back. It looks a lot like the Anvil effect in Keynote.
+
+`shake` is a property that controls the oscilliation function. Setting it to `YES` lets you shake the element instead of moving it. To use it, set the `fromValue` to the maximum amount you want it to go to and `toValue` to its current location. It uses a sine wave for the oscillation intead of cosine, since it starts at 0 (i.e., the current location.)
+
+## Demo app
+
+The demo app contains demos for several different animations that are supported by `SKBounceAnimation`.
+
+* One-axis animation: Using a keypath like `position.x`, we can animate along one axis.
+* Two-axis animation: Using a keypath like `position`, `SKBounceAnimation` will generate a path, and your layer will follow it.
+* Size: Using the `bounds` keypath, we can make the size increase. The center of the size increase is determined by `anchorPoint`, which can be moved. It defaults to the center of the layer
+* Color: I have no idea why anyone would want to bounce a color animation, but I was feeling whimsical, so I added support for this as well.
+* Scale: Using a `CATransform3D` struct and the `transform` keypath, we can scale objects. This is very useful to create an effect like UIAlerts bouncing in. The `anchorPoint` also judges how this effect happens.
+* Scale & Rotate: Using multiple CATransform3Ds on top of each other, we can do super weird effects like scale and rotating. They look really cool.
+* Rect: The last demo creates two `SKBounceAnimations` with two different `keyPath`s (`position` and `bounds`) but attaches them to the same layer. The effect looks like a `frame` animation.
+
+## Future work
+
+`SKBounceAnimation` doesn’t support the `byValue` property yet.
Oops, something went wrong.

0 comments on commit b85e7b8

Please sign in to comment.