Git Product home page Git Product logo

vdanimation's Introduction

VDAnimation

CI Status Version License Platform

Description

This repository provides a new declarative way to describe animations

Example

Sequential {
  Parallel {
    someView.ca.frame.origin.y(100)
    someView.ca.backgroundColor(.red).duration(relative: 0.2)
  }
  Parallel {
    someView.ca.transform(CGAffineTransform(rotationAngle: CGFloat.pi / 3))
    someView.ca.backgroundColor(.white).duration(0.1)
    Sequential {
      someView.ca.backgroundColor(.blue)
      someView.ca.backgroundColor(.green)
    }
  }
  UIViewAnimate {
    self.imageHeightConstraint.constant = 50
    self.view.layoutIfNeeded()
  }
  TimerAnimation { progress in
  	someLabel.textColor = (UIColor.white...UIColor.red).at(progress)
  }
}
.curve(.easeInOut)
.duration(3)
.start()

Usage

UIViewAnimate

simple UIKit animation, it's initialized by closure

UIViewAnimate {
  ...
}
.duration(0.3)
.start()

Animate

simple SwiftUI animation, it's initialized by closure

struct ExampleView {
  @StateObject var animations = AnimationsStore()
  @State private var someValue: Value
		
  var example1: some View {
    VStack {
      Button(Text("Tap")) {
        Sequential {
	  Animate {
	    $someValue =~ newValue
	  }
	  .duration(0.3)
	  Animate { progress in
	    someValue = (from...to).at(progress)
	    //progress may be 0 or 1
	    //or any value in 0...1 if animation is interactive
	  }
	  .duration(0.3)
        }
	.store(animations)
        .start()
      }
    }
    .with(animations)
  }
		
  var example2: some View {
    VStack {
      Slider(value: $animations.progress, in: 0...1)
      Button("Play") {
	animations.play()
      }
      Button("Pause") {
	animations.pause()
      }
    }
    .with(animations) {
      Animate {
	$someValue =~ newValue
      }
      .duration(2)
    }
  }
}

Sequential

sequential animations running one after another

Sequential {
  Animate { ... }.duration(relative: 0.5)
  Interval(0.1)
  Parallel { ... }
}
.duration(1)
.start()

Parallel

parallel animations running simultaneously

Parallel {
  Animate { ... }.duration(relative: 0.5)
  Sequential { ... }	
}
.duration(1)
.start()

Interval

time interval

Interval(1)

Instant

any block of code, always zero duration

Instant {
  ...
}

TimerAnimation

CADisplayLink wrapper

TimerAnimation { progress in
  ...
}

Interactive

method .start() or .delegate() returns AnimationDelegateProtocol object

AnimationDelegateProtocol

  1. .isRunning: Bool { get }
  2. .position: AnimationPosition { get nonmutating set }
  3. .options: AnimationOptions { get }
  4. .play(with options: AnimationOptions)
  5. .pause()
  6. .stop(at position: AnimationPosition?)
  7. .add(completion: @escaping (Bool) -> Void)
  8. .cancel()

Modifiers

  1. .duration(TimeInterval) - sets the animation duration in seconds
  2. .duration(relative: Double) - sets the animation duration relative to the parent animation in 0...1
  3. .curve(BezierCurve) - sets the animation curve
  4. .spring(dampingRatio: CGFloat = 0.3) - sets spring animation curve (only for UIViewAnimate)
  5. .repeat(), .repeat(Int) - repeat animation
  6. .autoreverse(), .autoreverse(repeat: Int) - autoreverse animation
  7. .reversed() - reversed animation
  8. .ca - UIView, CALayer and View, Binding extension to describe an animation of properties
someView.ca.backgroundColor(.white).layer.cornerRadius(8).tintColor(.red).duration(0.3).start()

Transitions

VDAnimation provides easy way to describe UIViewController transitions. VDAnimation also supports transitions like Keynote's Magic Move or Hero. It checks the .transition.id property on all source and destination views. Every matched view pair is then automatically transitioned from its old state to its new state.

viewController.transition.isEnabled = true
viewController.transition.duration = 0.4
viewController.transition.curve = .easeIn
viewController.transition.modifier = .edge(.bottom)
viewController.transition.interactive.disappear = .swipe(to: .bottom)
present(viewController, animated: true)
fromVc.someView.transition.id = "source"
toVc.someView.transition.id = "source"
fromVc.someView2.transition.modifier = .scale.offset(10)
to.someView2.transition.modifier = .scale.offset(-10)
toVc.transition.isEnabled = true
viewController.transition.interactive.disappear = .swipe(to: .bottom)
present(toVc, animated: true)
toVc.transition = .pageSheet(from: .bottom)
present(toVc, animated: true)

Installation

  1. CocoaPods

Add the following line to your Podfile:

pod 'VDAnimation'

and run pod update from the podfile directory first.

  1. Swift Package Manager

Create a Package.swift file.

// swift-tools-version:5.0
import PackageDescription

let package = Package(
  name: "SomeProject",
  dependencies: [
    .package(url: "https://github.com/dankinsoid/VDAnimation.git", from: "1.51.0")
  ],
  targets: [
    .target(name: "SomeProject", dependencies: ["VDAnimation"])
  ]
)
$ swift build

Author

dankinsoid, [email protected]

License

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

vdanimation's People

Contributors

dankinsoid avatar

Stargazers

Wonyoung Ju avatar  avatar Romain Vincens avatar Jan Kodeš avatar yan_lu avatar Mark Villacampa avatar  avatar 东坡肘子 avatar  avatar Christopher Hickman avatar Tim Kersey avatar Asge Yohannes avatar Adrian Demetrescu avatar Ungjae Lee avatar Moi Gutierrez avatar  avatar  avatar

Watchers

James Cloos avatar  avatar

vdanimation's Issues

exmaple

need example project with preview

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.