动画曲线看起来像 Mountain View。
Appetize's Demo
概述
它由用于可视化的 Mountain 和用于绘图的 MountainView 组成。
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
可攀登山峰(Climbingable: ClimbDownable (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()
登山者:ClimbFinishable
获取动画的进度。
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()
}
协议
遵循MIT版权许可。有关详细信息,请参阅LICENSE文件。