Skip to content

Commit

Permalink
5.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
EyreFree committed Jul 27, 2019
1 parent 5a53b63 commit 64d66c4
Show file tree
Hide file tree
Showing 17 changed files with 395 additions and 350 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

-----

## [5.1.0](https://github.com/EFPrefix/EFCountingLabel/releases/tag/5.1.0) (2019-07-27)

- Refactoring.

---

## [5.0.0](https://github.com/EFPrefix/EFCountingLabel/releases/tag/5.0.0) (2019-03-30)

- Upgrade to Swift 5.0.
Expand Down
2 changes: 1 addition & 1 deletion EFCountingLabel.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'EFCountingLabel'
s.version = '5.0.0'
s.version = '5.1.0'
s.summary = 'A label which can show number change animated.'

s.description = <<-DESC
Expand Down
58 changes: 33 additions & 25 deletions EFCountingLabel/Classes/EFCount.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
//
// EFCount.swift
// EFCountingLabel
//
// Created by Kirow on 2019/05/14.
//
// Copyright (c) 2017 EyreFree <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
Expand Down Expand Up @@ -33,43 +41,43 @@ extension EFCount {
public func countFromZeroTo(_ endValue: CGFloat, withDuration duration: TimeInterval) {
countFrom(0, to: endValue, withDuration: duration)
}

public func countFrom(_ startValue: CGFloat, to endValue: CGFloat) {
countFrom(startValue, to: endValue, withDuration: 0)
}

public func countFromCurrentValueTo(_ endValue: CGFloat) {
countFromCurrentValueTo(endValue, withDuration: 0)
}

public func countFromZeroTo(_ endValue: CGFloat) {
countFromZeroTo(endValue, withDuration: 0)
}
}

public class EFCounter {
public var timingFunction: EFTiming = EFTimingFunction.linear

public var updateBlock: ((CGFloat) -> Void)?
public var completionBlock: (() -> Void)?

public private(set) var fromValue: CGFloat = 0
public private(set) var toValue: CGFloat = 1
private var currentDuration: TimeInterval = 0
public private(set) var totalDuration: TimeInterval = 1
private var lastUpdate: TimeInterval = 0

private var timer: CADisplayLink?

public var isCounting: Bool {
return timer != nil
}

public var progress: CGFloat {
guard totalDuration != 0 else { return 1 }
return CGFloat(currentDuration / totalDuration)
}

public var currentValue: CGFloat {
if currentDuration == 0 {
return 0
Expand All @@ -78,36 +86,36 @@ public class EFCounter {
}
return fromValue + timingFunction.update(progress) * (toValue - fromValue)
}

public init() {

}

// CADisplayLink callback
@objc public func updateValue(_ timer: Timer) {
let now = CACurrentMediaTime()
currentDuration += now - lastUpdate
lastUpdate = now

if currentDuration >= totalDuration {
invalidate()
currentDuration = totalDuration
}

updateBlock?(currentValue)

if currentDuration == totalDuration {
runCompletionBlock()
}
}

private func runCompletionBlock() {
if let tryCompletionBlock = completionBlock {
completionBlock = nil
tryCompletionBlock()
}
}

//set init values
public func reset() {
invalidate()
Expand All @@ -117,7 +125,7 @@ public class EFCounter {
lastUpdate = 0
totalDuration = 1
}

public func invalidate() {
timer?.invalidate()
timer = nil
Expand All @@ -128,25 +136,25 @@ extension EFCounter: EFCount {
public func countFromCurrentValueTo(_ endValue: CGFloat, withDuration duration: TimeInterval) {
countFrom(currentValue, to: endValue, withDuration: duration)
}

public func countFrom(_ startValue: CGFloat, to endValue: CGFloat, withDuration duration: TimeInterval) {
fromValue = startValue
toValue = endValue

// remove any (possible) old timers
invalidate()

if duration == 0.0 {
// No animation
updateBlock?(endValue)
runCompletionBlock()
return
}

currentDuration = 0
totalDuration = duration
lastUpdate = CACurrentMediaTime()

let timer = CADisplayLink(target: self, selector: #selector(updateValue(_:)))
if #available(iOS 10.0, *) {
timer.preferredFramesPerSecond = 30
Expand All @@ -157,9 +165,9 @@ extension EFCounter: EFCount {
timer.add(to: .main, forMode: .tracking)
self.timer = timer
}

public func stopCountAtCurrentValue() {
invalidate()
updateBlock?(currentValue)
}
}
}
22 changes: 15 additions & 7 deletions EFCountingLabel/Classes/EFCountAdapter.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
//
// EFCountAdapter.swift
// EFCountingLabel
//
// Created by Kirow on 2019/05/14.
//
// Copyright (c) 2017 EyreFree <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
Expand Down Expand Up @@ -33,7 +41,7 @@ extension EFCountAdapter {
counter.updateBlock = nil
}
}

public func setCompletionBlock(_ completion: ((_ sender: Self) -> Void)?) {
if let completion = completion {
counter.completionBlock = { [unowned self] in
Expand All @@ -43,15 +51,15 @@ extension EFCountAdapter {
counter.completionBlock = nil
}
}

public func countFrom(_ startValue: CGFloat, to endValue: CGFloat, withDuration duration: TimeInterval) {
counter.countFrom(startValue, to: endValue, withDuration: duration)
}

public func countFromCurrentValueTo(_ endValue: CGFloat, withDuration duration: TimeInterval) {
countFrom(counter.currentValue, to: endValue, withDuration: duration)
}

public func stopCountAtCurrentValue() {
counter.stopCountAtCurrentValue()
}
Expand Down Expand Up @@ -99,15 +107,15 @@ open class EFCountingButton: UIButton, EFCountAdapter {
return nil
}
}

deinit {
counter.invalidate()
}
}

open class EFCountingLabel: UILabel, EFCountAdapter {
public private(set) var counter = EFCounter()

open var formatBlock: ((CGFloat) -> String)? {
set {
if let formatBlock = newValue {
Expand Down Expand Up @@ -147,7 +155,7 @@ open class EFCountingLabel: UILabel, EFCountAdapter {
return nil
}
}

deinit {
counter.invalidate()
}
Expand Down
6 changes: 3 additions & 3 deletions EFCountingLabel/Classes/EFTimingMethod.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// EFTimingMethod.swift
// EFTimingMethod
// EFCountingLabel
//
// Created by Kirow on 2019/05/14.
//
Expand Down Expand Up @@ -31,7 +31,7 @@ public enum EFTimingFunction: EFTiming {
case easeInOut(easingRate: CGFloat)
case easeInBounce
case easeOutBounce

public func update(_ time: CGFloat) -> CGFloat {
switch self {
case .linear:
Expand Down Expand Up @@ -67,4 +67,4 @@ public enum EFTimingFunction: EFTiming {
return 63.0 / 64.0 + pow(11.0 / 4.0, 2) * pow(time - 21.0 / 22.0, 2)
}
}
}
}
7 changes: 3 additions & 4 deletions Example/EFCountingLabel/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.isStatusBarHidden = false
return true
}
Expand Down
Loading

0 comments on commit 64d66c4

Please sign in to comment.