Skip to content

Commit

Permalink
Fix error
Browse files Browse the repository at this point in the history
  • Loading branch information
EyreFree committed Jan 30, 2019
1 parent eb910bd commit 87777b3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
31 changes: 29 additions & 2 deletions EFCountingLabel/Classes/EFCountingButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,37 @@

import Foundation

public class EFCountingButtonLabel: EFCountingLabel {

fileprivate weak var parentButton: UIButton?

public override func setTextValue(_ value: CGFloat) {
if let button = parentButton {
if let tryAttributedFormatBlock = self.attributedFormatBlock {
button.setAttributedTitle(tryAttributedFormatBlock(value), for: UIControl.State.normal)
} else if let tryFormatBlock = self.formatBlock {
button.setTitle(tryFormatBlock(value), for: UIControl.State.normal)
} else {
// check if counting with ints - cast to int
if nil != self.format.range(of: "%(.*)d", options: String.CompareOptions.regularExpression, range: nil)
|| nil != self.format.range(of: "%(.*)i") {
button.setTitle(String(format: self.format, Int(value)), for: UIControl.State.normal)
} else {
button.setTitle(String(format: self.format, value), for: UIControl.State.normal)
}
}
} else {
super.setTextValue(value)
}
}
}

open class EFCountingButton: UIButton {

open var countingLabel: EFCountingLabel = {
return EFCountingLabel()
open lazy var countingLabel: EFCountingButtonLabel = {
let buttonLabel: EFCountingButtonLabel = EFCountingButtonLabel()
buttonLabel.parentButton = self
return buttonLabel
}()

override open var titleLabel: UILabel? {
Expand Down
14 changes: 5 additions & 9 deletions EFCountingLabel/Classes/EFCountingLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,9 @@ public class UILabelCounterEaseInBounce: UILabelCounter {
public func update(_ t: CGFloat) -> CGFloat {
if t < 4.0 / 11.0 {
return CGFloat(1.0 - (powf(11.0 / 4.0, 2) * powf(Float(t), 2))) - t
}
if t < 8.0 / 11.0 {
} else if t < 8.0 / 11.0 {
return CGFloat(1.0 - (3.0 / 4.0 + powf(11.0 / 4.0, 2) * powf(Float(t - 6.0 / 11.0), 2))) - t
}
if t < 10.0 / 11.0 {
} else if t < 10.0 / 11.0 {
return CGFloat(1.0 - (15.0 / 16.0 + powf(11.0 / 4.0, 2) * powf(Float(t - 9.0 / 11.0), 2))) - t
}
return CGFloat(1.0 - (63.0 / 64.0 + powf(11.0 / 4.0, 2) * powf(Float(t - 21.0 / 22.0), 2))) - t
Expand All @@ -90,11 +88,9 @@ public class UILabelCounterEaseOutBounce: UILabelCounter {
public func update(_ t: CGFloat) -> CGFloat {
if t < 4.0 / 11.0 {
return CGFloat(powf(11.0 / 4.0, 2) * powf(Float(t), 2))
}
if t < 8.0 / 11.0 {
} else if t < 8.0 / 11.0 {
return CGFloat(3.0 / 4.0 + powf(11.0 / 4.0, 2) * powf(Float(t - 6.0 / 11.0), 2))
}
if t < 10.0 / 11.0 {
} else if t < 10.0 / 11.0 {
return CGFloat(15.0 / 16.0 + powf(11.0 / 4.0, 2) * powf(Float(t - 9.0 / 11.0), 2))
}
return CGFloat(63.0 / 64.0 + powf(11.0 / 4.0, 2) * powf(Float(t - 21.0 / 22.0), 2))
Expand Down Expand Up @@ -219,7 +215,7 @@ open class EFCountingLabel: UILabel {
}
}

private func setTextValue(_ value: CGFloat) {
public func setTextValue(_ value: CGFloat) {
if let tryAttributedFormatBlock = self.attributedFormatBlock {
self.attributedText = tryAttributedFormatBlock(value)
} else if let tryFormatBlock = self.formatBlock {
Expand Down
20 changes: 19 additions & 1 deletion Example/EFCountingLabel/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ViewController: UIViewController {
var countPercentageLabel: EFCountingLabel!
var scoreLabel: EFCountingLabel!
var attributedLabel: EFCountingLabel!
var countingButton: EFCountingButton!

override func viewDidLoad() {
super.viewDidLoad()
Expand Down Expand Up @@ -90,13 +91,22 @@ class ViewController: UIViewController {
self.view.addSubview(attributedLabel)
self.attributedLabel = attributedLabel

//storyboard
// storyboard
self.label.method = .easeInOut
self.label.format = "%d%%"
self.label.completionBlock = {
[weak self] () in
self?.label.textColor = UIColor(red: 0, green: 0.5, blue: 0, alpha: 1)
}

// button
self.countingButton = EFCountingButton(frame: CGRect(x: 10, y: 170, width: 200, height: 40))
self.countingButton.setTitle("倒计时按钮", for: UIControl.State.normal)
self.countingButton.setTitleColor(UIColor.blue, for: UIControl.State.normal)
self.countingButton.addTarget(self, action: #selector(buttonClicked), for: UIControl.Event.touchUpInside)
self.countingButton.countingLabel.format = "%d"
self.countingButton.countingLabel.method = .linear
self.view.addSubview(countingButton)
}

func setupButton() {
Expand All @@ -122,4 +132,12 @@ class ViewController: UIViewController {
attributedLabel.countFrom(0, to: 100, withDuration: 2.5)
self.label.countFrom(0, to: 100)
}

@objc func buttonClicked(button: EFCountingButton) {
self.countingButton.countingLabel.completionBlock = { [weak self] in
guard let strongSelf = self else { return }
strongSelf.countingButton.setTitle("倒计时按钮", for: UIControl.State.normal)
}
self.countingButton.countingLabel.countFrom(60, to: 0, withDuration: 60)
}
}

0 comments on commit 87777b3

Please sign in to comment.