• Stars
    star
    2,960
  • Rank 15,309 (Top 0.4 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 9 years ago
  • Updated almost 4 years ago

Reviews

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

Repository Details

A Swift library to take the power of UIView.animateWithDuration(_:, animations:...) to a whole new level - layers, springs, chain-able animations and mixing view and layer animations together!

ver 2.0

NB! Breaking changes in 2.0 - due to a lot of requests EasyAnimation does NOT automatically install itself when imported. You need to enable it by calling EasyAnimation.enable() somewhere in your code (AppDelegate is a good idea).

The library doesn't use any private APIs - apps using it should be fine for release on the App Store.

Intro
Layer Animations
Spring Layer Animations
Chain Animations
Cancel Chain Animations
Installation
Credit
License
Version History

Intro

UIView.animateWithDuration:animations: is really easy to use and you're so familiar with its syntax that you often want it to do just a bit more for you automatically. But it doesn't and you need to import Bloated.framework by Beginner Ninja Coder in order to make a bit more advanced animations than what animateWithDuration:animations: allows you to.

EasyAnimation extends what UIKit offers in terms of animations and makes your life much easier because you can do much more without learning some perky new syntax.

In version 2.0 and higher to enable all EasyAnimation features you need to run once this code - AppDelegate is a good place since it'll enable EasyAnimation as soon as your app starts.

    EasyAnimation.enable()

In your other classes you don't need to import EasyAnimation or do anything else. Once you call enable() afterwards you use the normal UIKit APIs like usual.

Easy Layer Animations

EasyAnimation allows you to animate your layers straight from animate(duration:animations:...). No more CABasicAnimation code for you. Just adjust the properties of your layers from within the animations block and EasyAnimation will take care of the rest:

CoreAnimation (before)
    let anim = CABasicAnimation(keyPath: "position.x")
    anim.fromValue = 100.0
    anim.toValue = 200.0
    anim.duration = 2.0
    view.layer.addAnimation(anim, forKey: nil)
EasyAnimation (after)
    UIView.animate(duration: 2.0, animations: {
        self.view.layer.position.x = 200.0
    })

(OK, this example actually works fine also without EasyAnimation but I still keep it here for illustrative purpose)

Or if you need to specify delay, animation options and/or animation curve:

CoreAnimation (before)
    let anim = CABasicAnimation(keyPath: "position.x")
    anim.fromValue = 100.0
    anim.toValue = 200.0
    anim.duration = 2.0
    anim.fillMode = kCAFillModeBackwards
    anim.beginTime = CACurrentMediaTime() + 2.0
    anim.timingFunction = CAMediaTimingFunction(name: 
        kCAMediaTimingFunctionEaseOut)
    anim.repeatCount = Float.infinity
    anim.autoreverses = true
    view.layer.addAnimation(anim, forKey: nil)
EasyAnimation (after)
    UIView.animate(duration: 2.0, delay: 2.0, 
        options: [.repeat, .autoreverse, .curveEaseOut], 
        animations: {
        self.view.layer.position.x += 200.0
    // let's add more animations 
    // to make it more interesting!
    self.view.layer.cornerRadius = 20.0
    self.view.layer.borderWidth = 5.0
}, completion: nil)

And if you want to execute a piece of code after the animation is completed - good luck setting up your animation delegate and writing the delegate methods.

With EasyAnimation you just put your code as the completion parameter value and EasyAnimation executes it for you when your animation completes.

Spring Layer Animations

One thing I really missed since iOS9 when using CoreAnimation and CABasicAnimation was that there was no easy way to create spring animations. Luckily a handy library called RBBAnimation provides an excellent implementation of spring animations for layers - I translated the code to Swift and included RBBSpringAnimation into EasyAnimation.

Easy Animation takes care to use the new in iOS9 spring animation class CASpringAnimation when your app runs on iOS9 or higher and falls back to RBBSpringAnimation when your app runs on iOS8.

Here's how the code to create a spring animation for the layer position, transform and corner radius looks like:

EasyAnimation
    UIView.animate(duration: 2.0, delay: 0.0, 
      usingSpringWithDamping: 0.25, 
      initialSpringVelocity: 0.0, 
      options: [], 
      animations: {
        self.view.layer.position.x += 200.0
        self.view.layer.cornerRadius = 50.0
        self.view.layer.transform = CATransform3DMakeScale(1.2, 1.2, 1.0)
    }, completion: nil)

Sam Davies collaborated on the spring animations code. Thanks a ton - I couldn't have figured this one on my own!

Chain Animations

animate(duration:animations:..) is really handy but chaining one animation after another is a major pain (especially if we are talking about more than 2 animations).

EasyAnimation allows you to use a method to just chain two or more animations together. Call animateAndChain(duration:delay:options:animations:completion:) and then chain to it more animations. Use animate(duration:animations...) or any other method to create chained animations.

EasyAnimation
    UIView.animateAndChain(duration: 1.0, delay: 0.0, 
      options: [], animations: {
        self.view.center.y += 100
    }, completion: nil).animate(duration: 1.0, animations: {
        self.view.center.x += 100
    }).animate(duration: 1.0, animations: {
        self.view.center.y -= 100
    }).animate(duration: 1.0, animations: {
        self.view.center.x -= 100
    })

Yes - that works, give it a try in your project :]

This code will animate the view along a rectangular path - first downwards, then to the right, then up, then to the initial point where the animation started.

What a perfect oportunity to repeat the animation and make the animation run continuosly! Add options parameter to the last animate(duration:... in the chain and turn on the .repeat option.

This will make the whole chain (e.g. the 4 animations) repeat continuously.

If you want to pause between any two animations in the chain - just use the delay parameter and it will all just work.

Note: animateAndChain does not create a serial queue to which you could add animations at any time. You schedule your animations once with one call like the example above and it runs on its own, you can't add or remove animations to and from the sequence.

Cancel Chain Animations

If you have a repeating (or a normal) chain animation on screen you can cancel it at any time. Just grab hold of the animation object and call cancelAnimationChain on it any time you want.

let chain = UIView.animateAndChain(duration: 1.0, delay: 0.0,
    options: [], animations: {
        self.square.center.y += 100
    }, completion: nil).animate(duration: 1.0, animations: {
  [... the rest of the animations in the chain]
chain.cancelAnimationChain()

If you want to do some cleanup after the animation chain is cancelled provide a block of code to the cancelAnimationChain method:

chain.cancelAnimationChain({
  self.myView.center = initialPosition
  //etc. etc.
})

The animation will not stop immediately but once it completes the current step of the chain it will stop and cancel all scheduled animations in this chain.

Installation

  • CocoaPods: Add to your project's Podfile:

pod 'EasyAnimation'

  • Carthage: If you can help with Cartage support let me know.

  • Source code: To install with the source code - clone this repo or download the source code as a zip file. Include all files within the EasyAnimation folder into your project.

Credit

Author: Marin Todorov

More about Marin:


iOS Animations by Tutorials, Author

iOS Animations by Emails Newsletter, Author

Includes parts of RBBAnimation by Robert BΓΆhnke. The code is translated from Objective-C to Swift by Marin Todorov.

Collaborator on the spring animation integration: Sam Davies.

License

EasyAnimation is available under the MIT license. See the LICENSE file for more info.

RBBAnimation license: https://github.com/robb/RBBAnimation/blob/master/LICENSE

To Do

  • .autoreverse for chain animations (if possible)
  • add support for keyframe animation along the path via a custom property

Version History

  • 2.0 - initialize() is deprecated so in need of manual initialization
  • 1.1 - Xcode 8
  • 1.0.5 - Xcode 7.3 compatibility
  • 1.0.4 - Swift 3 compatibility changes
  • 1.0.2 - Fixes openGL view crashes for everyone
  • 1.0.1 - Bug fixes
  • 1.0 - Swift 2.0 and iOS9
  • 0.7 - round of bug fixes and a number of improvements
  • 0.6 - first beta version

More Repositories

1

SwiftSpinner

A beautiful activity indicator and modal alert written in Swift (originally developed for my app DoodleDoodle) Using blur effects, translucency, flat and bold design - all iOS 8 latest and greatest
Swift
2,184
star
2

Timelane

Timelane
Swift
706
star
3

TaskQueue

A Task Queue Class developed in Swift (by Marin Todorov)
Swift
681
star
4

UIEffectDesignerView

A native Particle Systems effect view for iOS and OSX powered by QuartzCore
Objective-C
617
star
5

Retry

Haven't you wished for `try` to sometimes try a little harder? Meet `retry`
Swift
498
star
6

EventBlankApp

A free open source iOS app for events or conferences. Read more on the app's webpage:
Swift
291
star
7

MTLog

NSLog replacement for coders!
Objective-C
222
star
8

Breadcrumbs

Bugtracker working off source code
Swift
193
star
9

TimelaneCombine

Timelane + Combine
Swift
168
star
10

RxTimelane

Timelane + RxSwift
Swift
132
star
11

PowerUpYourAnimations

Sample code from talks on advanced animations
Swift
118
star
12

DoNilDisturbPlugin

A plugin for your Xcode project that stops you from working outside work hours
Swift
109
star
13

timeui

Profile apps from the command line β€” duration, cpu & memory usage.
Swift
107
star
14

OneShotLocationManager

A replacement class for CLLocationManager for when you want to easily fetch the current device location
Swift
101
star
15

MTPopupWindow

Popup-window style view for Objective-C, which loads contents of an HTML file. Easy one-line usage. Check the readme for example
Objective-C
84
star
16

TimelaneCore

Timelane + Core
Swift
66
star
17

OperationTimelane

Timelane + Operations
Swift
45
star
18

RxSwiftoniOS

Sample code from my dotSwift 2017 talk in Paris
Swift
43
star
19

PackageView

An app that displays Package.swift info
Swift
42
star
20

RealmGitHubSearchRxDemo

The demo app for RxRealm's post on realm.io
Swift
36
star
21

RealmMultiplatformDemo

Demo that shares its model layer across Apple's four platforms
Swift
36
star
22

Advanced-RSS-reader

An example of an RSS reader app for iPhone, full source and comments
Objective-C
34
star
23

MTTestSemaphore

A class to help you create unit tests that test asynchronous methods. You will need this to unit test any class that fetch data from the network, use location, camera, etc.
Objective-C
33
star
24

Unxippity

Unxips quickly Xcode downloads
Swift
32
star
25

Cancellor

Bind multiple cancellables to the lifetime of another object like a view controller.
Swift
32
star
26

RealmNotificationExample

The project for the post demonstrating fine grained notifications on realm.io
Swift
30
star
27

MarkWalkthrough

A SwiftUI package to quickly build app walkthroughs
Swift
27
star
28

HUD

Beautiful alert message/ progress hud component for iOS Objective-C
Objective-C
25
star
29

SafariDownload

Swift package to read Safari's download packages
Swift
8
star
30

CustomInstrument

A custom Xcode instrument based on Timelane
Swift
7
star
31

Fetch-and-parse-JSON

Fetch and parse JSON
Objective-C
7
star
32

ActorBench

Actor vs queue vs lock benchmark
Swift
6
star
33

TimelaneInstrument

Timelane Tools Instrument
5
star
34

StylesDemoApp

A little experiment on styling SwiftUI views
Swift
4
star
35

powerups

cli for dynamic XML includes
Swift
3
star
36

nsspain2020

Demo projects from my talk at NSSpain 2020
Swift
3
star
37

Tracker

Google Analytics iOS shortcut wrapper
3
star
38

TimerApp

Timer App v1
Swift
3
star
39

LogRider

Logs viewer app for mac
Swift
3
star
40

snippetty.io

snippetty.io
HTML
3
star
41

HTTPKit

Task based, promise like syntax, RESTful, HTTP library for iOS and OS X. Built off ConcurrentKit and NSURLSession.
Objective-C
2
star
42

react-native-console-oslog

React Native package to log to Apple's unified log
Objective-C++
2
star
43

rx-marin

rx blog:
HTML
2
star
44

combinebook.com

HTML
2
star
45

ios-animations-by-emails

iOS Animations by Emails newsletter archive
HTML
2
star
46

snippety.io

snippetty.io
1
star
47

swiftconcurrencybook

swiftconcurrencybook.com
CSS
1
star
48

AnagramsGameiPad

The completed source code to "How to create an awesome Anagrams game with UIKit"
Objective-C
1
star
49

Baffle

Ruby
1
star
50

www.timelane.tools

www.timelane.tools
1
star
51

macro-bad-access

Swift
1
star
52

tryCombine

tryCombine blog by Marin Todorov
HTML
1
star
53

Languages

RWDevCon tutorial code
Swift
1
star