Mountain 0.2.0

Mountain 0.2.0

Kyohei Ito 维护。



Mountain 0.2.0

  • 作者
  • Kyohei Ito

logo

Carthage compatible Version License Platform

动画曲线看起来像 Mountain View。

Appetize 的演示

概述

它由用于可视化动画的 Mountain 和用于绘图的 MountainView 组成。

graph

Mountain

您可以可视化执行动画的类型和缓动。例如,如果您执行如下操作,您可以在日志中输出生成进程。

let mountain = Mountain()
UIView.animate(withDuration: 0.3) {
    mountain.climb(retainSelf: true)
        .situation {
            print($0)
        }
}

执行日志。

begin(Mountain.Information(type: "CABasicAnimation", delay: 0.0, duration: 0.29999999999999999, reverse: false, repeatCount: 0.0, function: Optional(easeInEaseOut)))
on(progress: 0.0641036704182625, curve: 0.0977618917822838)
on(progress: 0.12283568829298, curve: 0.188721746206284)
on(progress: 0.182344779372215, curve: 0.276777744293213)
on(progress: 0.243786245584488, curve: 0.363606035709381)
on(progress: 0.303644210100174, curve: 0.444229960441589)
on(progress: 0.362437427043915, curve: 0.519574403762817)
on(progress: 0.420619368553162, curve: 0.59021383523941)
on(progress: 0.481439799070358, curve: 0.659674882888794)
on(progress: 0.54224693775177, curve: 0.724372625350952)
on(progress: 0.600623190402985, curve: 0.781640827655792)
on(progress: 0.659852802753448, curve: 0.834497272968292)
on(progress: 0.718149065971375, curve: 0.880844235420227)
on(progress: 0.77815580368042, curve: 0.922019064426422)
on(progress: 0.838306427001953, curve: 0.955803155899048)
on(progress: 0.899652481079102, curve: 0.981418073177338)
on(progress: 0.958390593528748, curve: 0.99620121717453)
on(progress: 1.0, curve: 1.0)

由于 Mountain 不依赖 MountainView,因此它可以独立使用,且轻量级。

MountainView

通过 Interface Builder 创建 MountainView,可以轻松绘制动画。

@IBOutlet weak var mountainView: MountainView!

UIView.animate(withDuration: 0.3) {
    self.mountainView.climb()
}

要求

  • Swift 3.0
  • iOS 9.0 或更高版本

如何安装

MountainView依赖于Mountain。

CocoaPods

将以下内容添加到您的Podfile

pod "Mountain"

或者

pod "MountainView"

Carthage

将以下内容添加到您的Cartfile

github "KyoheiG3/MountainView"

用法

Mountain

可攀爬:可下降的(Mountain)

开始和停止获取动画。

public func climb(retainSelf: Bool = default) -> Climber
public func climbDown()

获取可以由执行climbDown来中断。

let mountain = Mountain()
UIView.animate(withDuration: 0.3) {
    mountain.climb(retainSelf: true)
}
mountain.climbDown()

攀登者:可结束攀登

获取动画的进度。

public func situation(_ handler: @escaping (ClimbingSituation) -> Void) -> ClimbFinishable
public func filter(_ handler: @escaping (CGFloat, CGFloat) -> Bool) -> Self
public func map(_ handler: @escaping (CGFloat) -> CGFloat) -> Self
public func finish(_ handler: @escaping (ClimbingSituation) -> Void)

您还可以过滤和更改数据。

let mountain = Mountain()
UIView.animate(withDuration: 0.3) {
    mountain.climb(retainSelf: true)
        .filter { progress, curve -> Bool in
            progress > 0.5
        }
        .map { curve in
            ceil(curve * 100)
        }
        .situation {
            print($0)
        }
        .finish {
            print($0)
        }
}

执行日志。

begin(Mountain.Information(type: "CABasicAnimation", delay: 0.0, duration: 0.29999999999999999, reverse: false, repeatCount: 0.0, function: Optional(easeInEaseOut)))
on(progress: 0.543500006198883, curve: 73.0)
on(progress: 0.602446019649506, curve: 79.0)
on(progress: 0.661734223365784, curve: 84.0)
on(progress: 0.72049480676651, curve: 89.0)
on(progress: 0.779515981674194, curve: 93.0)
on(progress: 0.841407358646393, curve: 96.0)
on(progress: 0.899605870246887, curve: 99.0)
on(progress: 0.961408495903015, curve: 100.0)
on(progress: 1.0, curve: 100.0)
finished

爬山情况

一个枚举,用来知道动画的进度。

case begin(info: Information)
case on(progress: CGFloat, curve: CGFloat)
case cancelled
case finished

信息

这是关于动画的信息。

public static let `default`: Information
public let type: String
public let delay: CFTimeInterval
public let duration: CFTimeInterval
public let reverse: Bool
public let repeatCount: Float
public let function: CAMediaTimingFunction?

MountainLayerDelegate

也可以通过代理方法获取动画的进度。

public func climbingSituationDidChange(_ layer: MountainLayer, situation: ClimbingSituation)

MountainLayerHaving: 可攀登

UIView 想自己获取动画进度时使用它。

public func climb() -> Climber
public func climbDown()

需要按照以下方式准备 MountainLayer

class View: UIView, MountainLayerHaving {
    override class var layerClass: AnyClass {
        return MountainLayer.self
    }
    var mountainLayer: MountainLayer {
        return layer as! MountainLayer
    }
}

UIView 扩展

您可以从动画块中获取进度。

open class func animate(withDuration duration: TimeInterval, delay: TimeInterval = default, options: UIViewAnimationOptions = default, retain: Bool = default, animations: @escaping () -> Void = default, situation: @escaping (ClimbingSituation) -> Void, completion: ((Bool) -> Void)? = default) -> ClimbDownable?
open class func animate(withDuration duration: TimeInterval, delay: TimeInterval = default, usingSpringWithDamping dampingRatio: CGFloat, initialSpringVelocity velocity: CGFloat, options: UIViewAnimationOptions = default, retain: Bool = default, animations: @escaping () -> Void = default, situation: @escaping (ClimbingSituation) -> Void, completion: ((Bool) -> Void)? = default) -> ClimbDownable?

MountainView

它是 UIView 的子类,遵循 MountainLayerHaving

您可以使用 Interface Builder 创建 MountainView 并绘制动画的缓动曲线。

@IBOutlet weak var mountainView: MountainView!

UIView.animate(withDuration: 0.3) {
    self.mountainView.climb()
}

screen

LICENSE

基于MIT许可。详情请参见LICENSE文件。