VDAnimation

CI Status Version License Platform

描述

该仓库提供了一种新的声明式动画描述方式

示例

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()

用法

UIViewAnimate

简单的 UIKit 动画,通过闭包初始化

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

Animate

简单的 SwiftUI 动画,通过闭包初始化

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 {
  Animate { ... }.duration(relative: 0.5)
  Interval(0.1)
  Parallel { ... }
}
.duration(1)
.start()

Parallel

并行动画,同时运行

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

Interval

时间间隔

Interval(1)

Instant

任何代码块,持续时间始终为零

Instant {
  ...
}

TimerAnimation

CADisplayLink 封装

TimerAnimation { progress in
  ...
}

Interactive

方法 .start().delegate() 返回 AnimationDelegateProtocol 对象

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()

修饰符

  1. .duration(TimeInterval) - 设置动画持续时间,单位为秒
  2. .duration(relative: Double) - 设置动画持续时间,相对于父动画的比例,范围为 0...1
  3. .curve(BezierCurve) - 设置动画曲线
  4. .spring(dampingRatio: CGFloat = 0.3) - 设置弹簧动画曲线 (仅适用于 UIViewAnimate)
  5. .repeat(), .repeat(Int) - 重复动画
  6. .autoreverse(), .autoreverse(repeat: Int) - 自动反转动画
  7. .reversed() - 反转动画
  8. .ca - UIViewCALayerViewBinding 扩展,用于描述属性动画
someView.ca.backgroundColor(.white).layer.cornerRadius(8).tintColor(.red).duration(0.3).start()

转场

VDAnimation 提供了一种简单的方式来描述 UIViewController 转场。VDAnimation 还支持类似于 Keynote 的 Magic MoveHero 的转场。它检查所有源视图和目标视图上的 .transition.id 属性。 然后自动将每个匹配的视图对从旧状态转换为新状态。

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)

安装

  1. CocoaPods

将以下行添加到您的 Podfile

pod 'VDAnimation'

然后先从 podfile 目录运行 pod update

  1. Swift Package Manager

创建一个 Package.swift 文件。

// 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

作者

dankinsoid, voidilov@gmail.com

许可证

VDAnimation 在 MIT 许可证下可用。 有关更多信息,请参见 LICENSE 文件。