GithubHelp home page GithubHelp logo

rchatham / swiftyanimate Goto Github PK

View Code? Open in Web Editor NEW
194.0 5.0 17.0 363 KB

Composable animations in Swift

License: MIT License

Swift 98.87% Ruby 0.53% Objective-C 0.59%
swift-3 animation ios swift cocoapods carthage composable-animations animations tested

swiftyanimate's Introduction

Composable animations in Swift. Blog

Platform: iOS 8+ Language: Swift 3 License: MIT

Cocoapods compatible Carthage compatible SPM compatible

Docs Codecov Travis Code Climate

Installation

Cocoapods

The easiest way to get started is to use CocoaPods. Just add the following line to your Podfile:

pod 'SwiftyAnimate', '~> 1.3.0'

Carthage

github "rchatham/SwiftyAnimate"

Swift Package Manager

Add the following line to your Package.swift file.

.Package(url: "https://github.com/rchatham/SwiftyAnimate.git", majorVersion: 0) 

Usage

This library can be used to design composable animations while keeping animation code readable and maintainable.

Composing Animations

Compose animations and insert logic inbetween them using the then, do, and wait functions.

Then blocks

Add animations to the current instance using one of the implementations for this function. There are implemetations for spring and keyframe animations as well as chaining Animate objects together.

Animate(duration: 1.0) {
        // animation code goes here
    }
    .then(duration: 0.5) {
        // more animation code
    }
    .perform()

And blocks

Add animations to the current instance using one of the implementations for this function. There are implemetations for spring and keyframe animations as well as stacking Animate objects together. 'And' animations are performed in sync with the animation before it.

Animate(duration: 1.0) {
        // animation code goes here
    }
    .and(duration: 0.5) {
        // more animation code
    }
    .perform()

Do blocks

Add code that you don't intend on animating but would like to perform between animations here. Any code you put here will NOT be animated.

Animate(duration: 1.0) {
        // animation code goes here
    }
    .do {
        // logic you don't want to animate
    }
    .then(duration: 0.5) {
        // more animation code
    }
    .perform()

Wait blocks

Add code that you may want to pause an ongoing chain of animations for. Any code you put here will NOT be animated. You can pass in a timeout if you want to wait for a specific amount of time or if you don't want to wait longer to execute the code in the wait block.

Animate(duration: 1.0) {
        // animation code goes here
    }
    .wait(timeout: 5.0) { resume in
        // logic you want to pause an animation to complete
        resume()
    }
    .then(duration: 0.5) {
        // more animation code
    }
    .perform()

Performing Animations

There are two ways to perform animations finish and perform. Important: You must either call one of these two functions or decay on an animation instance or this will result in a memory leak!

Perform

This one is easy. Call this on an animation instance to perform it. Perform takes an optional closure which gets excecuted on completing the last animation block.

let animation = Animate(duration: 1.0) {
    // animations
}

animation.perform()

Finish

If you don't need to pass in a completion closure try calling finish on your animation instance. The animation passed in is enqueue'd and then perform is called on the instance. Finish has all of the same variations as the then function.

Animate(duration: 1.0) {
        // animations
    }
    .finish(duration: 1.0) {
        // animations
    }

Decay

If you would like to deallocate an animation instance without performing it call decay on it.

let animation = Animate(duration: 1.0) {
    // animations
}

animation.decay()

UIView Extensions

A number of animatable properties have extensions defined to make implementing them with this library even easier. Please check the docs!

Best Practices

The best way to take advantage of this library is define extensions for the views that you would like to animate. This compartmentailizes your code and keep all the animation logic tucked up within the view and out of your view controllers.

extension AnimatingView {
    func bounceAnimation() -> Animate {
        return Animate()
            .then(animation: scale(duration: 0.3, x: 1.3, y: 1.3))
            .then(animation: scale(duration: 0.3, x: 0.8, y: 0.8))
            .then(animation: scale(duration: 0.3, x: 1.1, y: 1.1))
            .then(animation: scale(duration: 0.3, x: 1.0, y: 1.0))
    }
}

Then when you go to perform an animation you just have to call perform() on the returned animation.

let animatingView = AnimatingView()

animatingView.bounceAnimation().perform()

And string them together with other animations for building up complex animation logic easily.

Animate()
    .then(animation: animatingView.bounceAnimation())
    .then(animation: animatingView.tiltAnimation())
    .then(animation: animatingView.bounceAnimation())
    .perform()

Contributing

I would love to see your ideas for improving this library! The best way to contribute is by submitting a pull request. I'll do my best to respond to your patch as soon as possible. You can also submit a new GitHub issue if you find bugs or have questions. ๐Ÿ™

Please make sure to follow our general coding style and add test coverage for new features!

swiftyanimate's People

Contributors

martnst avatar rchatham 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

swiftyanimate's Issues

Make the transform method actually change the frame of the view!

I first performed this animation:

var moveUp: Animate {
    return transform(duration: 0.5, transforms: [
        .move(x: 0, y: -100)
        ])
}

Then I performed this:

var moveRight: Animate {
    return transform(duration: SquareView.animDuration, transforms: [
        .move(x: 100, y: 0)
        ])
}

The view moves up, then to the bottom right. I have expected it to move the view up, then straight to the right i.e. equivalent to moving (x: 100, y: -100).

I think this happens because when I do the second animation, the view thinks it is still at its original position, before the first animation.

Can the transform method actually move the view?

How do I animate a rotation of 360 degrees or more?

Currently, .rotate(angle: 360) does not seem to rotate the view at all. I expected it to keep rotating the view clockwise until it reaches its original position. Is this not how it was designed? I have this workaround for now, but it's not nice:

transform(duration: 0.1, transforms: [
        .rotate(angle: 180)
    ]).then(animation: transform(duration: 0.1, transforms: [
        .rotate(angle: 180)
    ]))

On Xcode 9.3 error message Contextual type for closure argument list expects 1 argument, which cannot be implicitly ignored

I updated my Xcode to Xcode 9.3 and this error came out
BasicAnimation.swift file, line 48
Animate.swift file , line 494
and other problem => Missing argument for parameter #1 in call, swift file, line 48

/// The animation block that gets performed. public var animationBlock: AnimationBlock { switch self { case .cornerRadius(view: let view, duration: let duration, delay: _, radius: let radius, timing: let timing): return { let animation = CABasicAnimation(keyPath: "cornerRadius") animation.timingFunction = timing.coreAnimationCurve animation.fromValue = view.layer.cornerRadius animation.toValue = radius animation.duration = duration view.layer.add(animation, forKey: "corner") view.layer.cornerRadius = radius } } }

Why can't I chain translated and scaled animations?

    Animate()
            .then(animation: translated(duration: 1, x: 0, y: 300, options: [.curveEaseIn]))
            .and(animation: scaled(duration: 1, x: 0.01, y: 0.01, options: [.curveEaseIn]))

When I tried to perform the above animation, I only see the scale animation but not the translation animation.

I got an error about multiple commands

I got this error when I install the pod with Xcode 10:

Multiple commands produce '/Users/user/Library/Developer/Xcode/DerivedData/MyApp-bqlusyyuqwregbcljtzhcxmeziow/Build/Products/Debug-iphonesimulator/SwiftyAnimate/SwiftyAnimate.framework/Info.plist':

  1. Target 'SwiftyAnimate' (project 'Pods') has copy command from '/Users/user/Documents/MyApp/Pods/SwiftyAnimate/Sources/Info.plist' to '/Users/user/Library/Developer/Xcode/DerivedData/MyApp-bqlusyyuqwregbcljtzhcxmeziow/Build/Products/Debug-iphonesimulator/SwiftyAnimate/SwiftyAnimate.framework/Info.plist'
  1. Target 'SwiftyAnimate' (project 'Pods') has process command with output '/Users/user/Library/Developer/Xcode/DerivedData/MyApp-bqlusyyuqwregbcljtzhcxmeziow/Build/Products/Debug-iphonesimulator/SwiftyAnimate/SwiftyAnimate.framework/Info.plist'

How do I remove the view from super view after a disappear animation?

transform(duration: 0.5, transforms: [
        .scale(x: 0, y: 0)
        ]).do { [weak self] in
            self?.removeFromSuperview()
    }

I have the above animation. I was hoping that when performed, the view will shrink and after 0.5 seconds it will be removed from the super view. However, it actually just make the view disappear without the animation. I thought the do block will run code after the animations. Am i misunderstanding this? How can I remove the view from super view after animation finishes?

Swift 4.2 support?

I saw there is a swift-4 branch so I tried to install that, but it still gives me quite a few errors. Swift 4.2 changes a lot of the names from UIKit. Can you update the pod so that it supports Swift 4.2?

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.