Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions ACLabelCounting/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ class ViewController: UIViewController {
// label.count(from: 0,
// to: 100,
// duration: 5,
// animationType: .EaseIn,
// dataType: .Double) { txt in
// animationType: .easeIn,
// dataType: .double) { txt in
// return "\(txt) %"
// }


label.count(from: 0,
to: 100,
duration: 5,
animationType: .EaseIn,
dataType: .Int) { text -> NSAttributedString in
let appandString = " / 100"
let string = "\(text)\(appandString)"
let range = (string as NSString).range(of: appandString)
animationType: .easeIn,
dataType: .int) { text -> NSAttributedString in
let appendString = " / 100"
let string = "\(text)\(appendString)"
let range = (string as NSString).range(of: appendString)

let attributedString = NSMutableAttributedString(string: string)
attributedString.addAttribute(NSAttributedString.Key.foregroundColor,
Expand Down
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,37 @@ pod 'ACLabelCounting'
``` swift
label.count(to: 100)
```
从 10 到 100 的计数动画,持续时间是 5 秒钟,动画类型是渐入效果,数据类型是 .Double 类型。
从 10 到 100 的计数动画,持续时间是 5 秒钟,动画类型是渐入效果,数据类型是 .double 类型。

``` swift
label.count(from: 10,
to: 100,
duration: 5,
animationType: .EaseIn,
dataType: .Double)
animationType: .easeIn,
dataType: .double)
```
从 0 到 100 的计数动画,持续时间是 5 秒钟,动画类型是渐出效果,数据类型是 .Int 类型。字符串格式化是在字符串后面增加一个 `%`。
从 0 到 100 的计数动画,持续时间是 5 秒钟,动画类型是渐出效果,数据类型是 .int 类型。字符串格式化是在字符串后面增加一个 `%`。

``` swift
label.count(from: 0,
to: 100,
duration: 5,
animationType: .EaseOut,
dataType: .Int) { txt in
animationType: .easeOut,
dataType: .int) { txt in
return "\(txt) %"
}
```
从 0 到 100 的计数动画,持续时间是 5 秒钟,动画类型是渐 入效果,数据类型是 .Int 类型。字符串格式化就是在字符串有面加上 `/ 100` ,它颜色是亮灰色的。
从 0 到 100 的计数动画,持续时间是 5 秒钟,动画类型是渐 入效果,数据类型是 .int 类型。字符串格式化就是在字符串有面加上 `/ 100` ,它颜色是亮灰色的。

``` swift
label.count(from: 0,
to: 100,
duration: 5,
animationType: .EaseIn,
dataType: .Int) { text -> NSAttributedString in
let appandString = " / 100"
let string = "\(text)\(appandString)"
let range = (string as NSString).range(of: appandString)
animationType: .easeIn,
dataType: .int) { text -> NSAttributedString in
let appendString = " / 100"
let string = "\(text)\(appendString)"
let range = (string as NSString).range(of: appendString)

let attributedString = NSMutableAttributedString(string: string)
attributedString.addAttribute(NSForegroundColorAttributeName,
Expand All @@ -66,19 +66,19 @@ label.count(from: 0,

``` swift
enum ACLabelCountingDataType {
case Int
case Double
case int
case double
}
```
数据类型是 Int 或者 Double。

``` swift
enum ACLabelCountingAnimationType {
case None
case Liner
case EaseIn
case EaseOut
case EaseInOut
case none
case linear
case easeIn
case easeOut
case easeInOut
}
```
计数动画效果的类型分别是:没有效果(和线性效果一样)、线性效果、渐入效果、渐出效果、渐入渐出效果。他们分别对应的函数是:
Expand Down
44 changes: 21 additions & 23 deletions Source/ACLabelCounting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ struct LabelCountingConst {
}

enum ACLabelCountingAnimationType {
case None
case Liner
case EaseIn
case EaseOut
case EaseInOut
case none
case linear
case easeIn
case easeOut
case easeInOut
}

enum ACLabelCountingDataType {
case Int
case Double
case int
case double
}

class ACLabelCounting: UILabel {

private var progress: Double = 0
private var dataType: ACLabelCountingDataType = .Int
private var animationType: ACLabelCountingAnimationType = .None
private var dataType: ACLabelCountingDataType = .int
private var animationType: ACLabelCountingAnimationType = .none
private var duration: TimeInterval = LabelCountingConst.defaultDuration
private var displayLink: CADisplayLink! = CADisplayLink()
private var lastUpdate: TimeInterval = Date.timeIntervalSinceReferenceDate
Expand Down Expand Up @@ -92,25 +92,23 @@ class ACLabelCounting: UILabel {

private func handleText(with: Double, type: ACLabelCountingDataType) -> String {
switch type {
case .Int:
case .int:
return String(format: "%.0lf", with)
default:
case .double:
return String(format: "%.2lf", with)
}
}

private func renderScale(type: ACLabelCountingAnimationType) -> Double {
switch type {
case .None:
return liner(progress: progress, totle: Double(duration))
case .EaseIn:
case .none, .linear:
return linear(progress: progress, totle: Double(duration))
case .easeIn:
return easeIn(progress: progress, totle: Double(duration))
case .EaseOut:
case .easeOut:
return easeOut(progress: progress, totle: Double(duration))
case .EaseInOut:
case .easeInOut:
return easeInOut(progress: progress, totle: Double(duration))
default:
return liner(progress: progress, totle: Double(duration))
}
}

Expand Down Expand Up @@ -158,8 +156,8 @@ class ACLabelCounting: UILabel {
func count(from fromValue: Double = 0.0,
to toValue: Double,
duration time: TimeInterval = LabelCountingConst.defaultDuration,
animationType: ACLabelCountingAnimationType = .None,
dataType: ACLabelCountingDataType = .Int,
animationType: ACLabelCountingAnimationType = .none,
dataType: ACLabelCountingDataType = .int,
formatTextClosure: @escaping (String) -> String = { text -> String in return text }) {

assert((fromValue >= 0 && toValue >= 0), "the fromValue or toValue must be not negative!")
Expand All @@ -178,8 +176,8 @@ class ACLabelCounting: UILabel {
func count(from fromValue: Double = 0.0,
to toValue: Double,
duration time: TimeInterval = LabelCountingConst.defaultDuration,
animationType: ACLabelCountingAnimationType = .None,
dataType: ACLabelCountingDataType = .Int,
animationType: ACLabelCountingAnimationType = .none,
dataType: ACLabelCountingDataType = .int,
attributedTextClosure: @escaping (String) -> NSAttributedString) {

self.attributedTextClosure = attributedTextClosure
Expand All @@ -192,7 +190,7 @@ class ACLabelCounting: UILabel {
}

extension ACLabelCounting {
func liner(progress: Double, totle: Double) -> Double {
func linear(progress: Double, totle: Double) -> Double {
return progress / totle
}

Expand Down