https://github.com/robb/rbbanimation
Block-based animations made easy, comes with easing functions and a CASpringAnimation replacement.
https://github.com/robb/rbbanimation
Last synced: about 1 year ago
JSON representation
Block-based animations made easy, comes with easing functions and a CASpringAnimation replacement.
- Host: GitHub
- URL: https://github.com/robb/rbbanimation
- Owner: robb
- License: mit
- Created: 2013-10-11T16:49:57.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2022-06-24T14:03:15.000Z (almost 4 years ago)
- Last Synced: 2025-05-15T06:03:03.173Z (about 1 year ago)
- Language: Objective-C
- Homepage:
- Size: 114 KB
- Stars: 2,066
- Watchers: 63
- Forks: 182
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# RBBAnimation
`RBBAnimation` is a subclass of `CAKeyframeAnimation` that allows you to
declare your __animations using blocks__ instead of writing out all the
individual key-frames.
This gives you greater flexibility when specifying your animations while keeping
your code concise.
It comes out of the box with a [replacement for
CASpringAnimation](#rbbspringanimation), [support for custom easing functions
such as bouncing](#rbbtweenanimation) as well as hooks to allow your writing
your [own animations fully from scratch](#rbbcustomanimation).
## Installation
To install RBBAnimation, I recommend the excellent [CocoaPods]. Simply add this
to your Podfile
```ruby
pod 'RBBAnimation', '0.4.0'
```
and you are ready to go!
If you'd like to run the bundled test app, make sure to install its dependencies
by running
```sh
pod install
```
after cloning the repo.
## RBBCustomAnimation
Use `RBBCustomAnimation` to create arbitrary animations by passing in an
`RBBAnimationBlock`:
```objc
RBBCustomAnimation *rainbow = [RBBCustomAnimation animationWithKeyPath:@"backgroundColor"];
rainbow.animationBlock = ^(CGFloat elapsed, CGFloat duration) {
UIColor *color = [UIColor colorWithHue:elapsed / duration
saturation:1
brightness:1
alpha:1];
return (id)color.CGColor;
};
```
The arguments of the block are the current position of the animation as well as
its total duration.
Most of the time, you will probably want to use the higher-level
`RBBTweenAnimation`.
## RBBSpringAnimation
`RBBSpringAnimation` is a handy replacement for the private [CASpringAnimation].
Specify your spring's mass, damping, stiffness as well as its initial velocity
and watch it go:
```objc
RBBSpringAnimation *spring = [RBBSpringAnimation animationWithKeyPath:@"position.y"];
spring.fromValue = @(-100.0f);
spring.toValue = @(100.0f);
spring.velocity = 0;
spring.mass = 1;
spring.damping = 10;
spring.stiffness = 100;
spring.additive = YES;
spring.duration = [spring durationForEpsilon:0.01];
```
## RBBTweenAnimation
`RBBTweenAnimation` allows you to animate from one value to another, similar to
`CABasicAnimation` but with a greater flexibility in how the values should be
interpolated.
It supports the same cubic Bezier interpolation that you get from
`CAMediaTimingFunction` using the `RBBCubicBezier` helper function:
```objc
RBBTweenAnimation *easeInOutBack = [RBBTweenAnimation animationWithKeyPath:@"position.y"];
easeInOutBack.fromValue = @(-100.0f);
easeInOutBack.toValue = @(100.0f);
easeInOutBack.easing = RBBCubicBezier(0.68, -0.55, 0.265, 1.55);
easeInOutBack.additive = YES;
easeInOutBack.duration = 0.6;
```
However, `RBBTweenAnimation` also supports more complex easing functions such as
`RBBEasingFunctionEaseOutBounce`:
```objc
RBBTweenAnimation *bounce = [RBBTweenAnimation animationWithKeyPath:@"position.y"];
bounce.fromValue = @(-100);
bounce.toValue = @(100);
bounce.easing = RBBEasingFunctionEaseOutBounce;
bounce.additive = YES;
bounce.duration = 0.8;
```
You can also specify your own easing functions, from scratch:
```objc
RBBTweenAnimation *sinus = [RBBTweenAnimation animationWithKeyPath:@"position.y"];
sinus.fromValue = @(0);
sinus.toValue = @(100);
sinus.easing = ^CGFloat (CGFloat fraction) {
return sin((fraction) * 2 * M_PI);
};
sinus.additive = YES;
sinus.duration = 2;
```
## License
RBBAnimation was built by [Robert Böhnke][robb]. It is licensed under the MIT
License.
If you use RBBAnimation in one of your apps, I'd love to hear about it. Feel
free to follow me on Twitter where I'm [@ceterum_censeo][twitter].
[caspringanimation]: https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/QuartzCore.framework/CASpringAnimation.h
[robb]: http://robb.is
[twitter]: https://twitter.com/ceterum_censeo
[cocoapods]: http://cocoapods.org/