GithubHelp home page GithubHelp logo

robb / rbbanimation Goto Github PK

View Code? Open in Web Editor NEW
2.1K 64.0 182.0 117 KB

Block-based animations made easy, comes with easing functions and a CASpringAnimation replacement.

License: MIT License

Ruby 3.10% Objective-C 96.90%

rbbanimation's Introduction

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, support for custom easing functions such as bouncing as well as hooks to allow your writing your own animations fully from scratch.

Installation

To install RBBAnimation, I recommend the excellent CocoaPods. Simply add this to your Podfile

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

pod install

after cloning the repo.

RBBCustomAnimation

Use RBBCustomAnimation to create arbitrary animations by passing in an RBBAnimationBlock:

Rainbow

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:

Bouncing

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:

Ease In Out Back

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:

Bouncing

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:

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;

Sine Wave

License

RBBAnimation was built by Robert Böhnke. 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.

rbbanimation's People

Contributors

hepengzhang avatar mikeger avatar pfandrade avatar robb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rbbanimation's Issues

Allow User Interaction For Animations With Forwards Fill Mode

So I did a simple opacity animation on a table view, and I disabled removedOnCompletion, and set the fill mode to forwards so the animation doesn't reset when it ends. However, that disallows user interaction. The animation needs to be configured to have the UIViewAnimationOptionAllowUserInteraction flag, does RBBAnimation allow this flag to be set?

Here's my sample code:

RBBTweenAnimation *opacityAnimation = [RBBTweenAnimation animationWithKeyPath:@"opacity"];

opacityAnimation.fromValue = @(0.0f);
opacityAnimation.toValue = @(1.0f);
opacityAnimation.easing = RBBEasingFunctionLinear;
opacityAnimation.duration = 0.2f;
opacityAnimation.fillMode = @"forwards";

opacityAnimation.cumulative = false;
opacityAnimation.additive = false;
opacityAnimation.removedOnCompletion = false;

[self.tableView.layer addAnimation:opacityAnimation forKey:@"Animation"];

Implementation inconsistent with CASpringAnimation

Not sure whether RBBSpringAnimation is less performant than its Apple-authored counterpart (animated CATransform3D transformations appear noticeably slower), or if the implementation is different.

My setup:

animation.velocity = 0.0;
animation.mass = 3.0;
animation.stiffness = 1000.0;
animation.damping = 500.0;

(these values were reverse engineered and pulled straight out of iOS 7)

iOS6 Compatibility

Hello !

I tried to clone the repo and run the test on an old iPhone 3GS with v.6.1.3 without problems.

Is there any particular reason to set the .podspec to 7.0 ?

Great work by the way !

Please improve the documentation.

There is no documentation in the code. And the README is pretty lean.

It's hard to know what values to provide to the properties of the spring. Ie, what are the units of the values. Eg, how should I be determining velocity for the spring?

Double the RBBSpringAnimation "sampling rate" for smoother animations.

While using RBBSpringAnimation I noticed that movement was quite jerky in the initial phases of the spring animation.

To combat this I increased the "sampling rate" specified in RBBAnimation's values method from 60 to 120. Eg:

- (NSArray *)values {
    RBBAnimationBlock block = [self.animationBlock copy];

    CFTimeInterval duration = self.duration;

    return [RBBBlockBasedArray arrayWithCount:(NSUInteger)(duration * 120) block:^id(NSUInteger idx) {
        return block(idx / 120.0f, (CGFloat)duration);
    }];
}

The results seem to be smoother animations.

I'm not a math/signal processing whiz but I think this is an example of the Nyquist rate.

Perhaps this could be made a property on RBBSpringAnimation. As in some situations I think 60 would be fine but others a higher value is going to produce smoother results.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.