• Stars
    star
    927
  • Rank 49,282 (Top 1.0 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created over 12 years ago
  • Updated almost 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A CAKeyframeAnimation subclass that lets you quickly and easily set a number of bounces, and start and end values, and creates an animation for you.

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/ which in turn was based partially on Matt Gallagher’s work here: 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

To learn more about the math, check out the blogpost and the informational post 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 oscillation 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 instead of cosine, since it starts at 0 (i.e., the current location.)

stiffness is a property that determines how stiff the "spring" component should be. Acceptable values are

  • SKBounceAnimationStiffnessLight
  • SKBounceAnimationStiffnessMedium
  • SKBounceAnimationStiffnessHeavy

The default is SKBounceAnimationStiffnessMedium.

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 keyPaths (position and bounds) but attaches them to the same layer. The effect looks like a frame animation.

Other notes

SKBounceAnimation doesn’t support the byValue property.

More Repositories

1

Promise

A Promise library for Swift, based partially on Javascript's A+ spec
Swift
622
star
2

Objective-Shorthand

Objective-Shorthand is a set of categories that make long things in Objective-C short. Additions welcome.
Objective-C
360
star
3

Meridian

Meridian is a web server written in Swift that lets you write your endpoints in a declarative way.
Swift
316
star
4

InstantCocoa

Instant Cocoa is a framework for making iOS apps.
Objective-C
250
star
5

SKInnerShadowLayer

SKInnerShadowLayer is a CAGradientLayer subclass that adds properties to create an inner shadow on a given layer.
Objective-C
109
star
6

NSArray-LongestCommonSubsequence

This is a category on NSArray that finds the indexes of the longest common subsequence with another array.
Objective-C
73
star
7

swift-sudoku

Swift
40
star
8

NonEmptyArray

An array in Swift that can't be empty
Swift
35
star
9

SchemaSwift

Generate Swift structs from PostgreSQL database schema
Swift
24
star
10

MandrillTransport-CakePHP

This enables using CakeEmail from CakePHP 2.0 with Mandrill.
PHP
19
star
11

SKTabBarController

A clone of UITabBarController to help understand UIViewController containment.
Objective-C
15
star
12

SKNavigationController

A clone of UINavigationController to help understand UIViewController containment.
Objective-C
15
star
13

pepin

JavaScript
12
star
14

SKStateMachine

A simple state machine, written in Objective-C, that uses metaprogramming to define transitions.
Objective-C
12
star
15

ColumnarGroupedTableView

Objective-C
11
star
16

SKReachability

SKReachability is a singleton that gives quick access to information about the connection.
Objective-C
5
star
17

Parser

A small library for parsing JSON in swift
Swift
4
star
18

BigInt

Swift
3
star
19

SwiftgreSQL

A synchronous blocking wrapper around libpq with no dependencies. Forked and modified from `vapor-community/postgresql`.
Swift
3
star
20

FTWStartupLaunch

A drop-in class that lets you set your program to launch at login.
Objective-C
2
star
21

Tablesum

Automatic table footers in pure JavaScript
JavaScript
1
star
22

FTWCache

Dead simple caching for Mac and iOS
Objective-C
1
star