Skip to content

Commit

Permalink
Merge branch 'release/1.0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
dokgi88 committed Jul 19, 2019
2 parents c534568 + 46908c5 commit 2ff1400
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 13 deletions.
20 changes: 18 additions & 2 deletions Example/FlashingLabel/ViewController.swift
Expand Up @@ -85,11 +85,23 @@ class FlashingContainerView: UIView {
private let flashLabelMultiple: FlashingLabel = {
let label = FlashingLabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "HELLO FLASHING LABEL"
label.font = .boldSystemFont(ofSize: 20)
label.baseColor = UIColor.black.withAlphaComponent(0.1)
label.flashingColors = [.pantone_roseQuartz(), .pantone_serenity(), .pantone_greenery(), .pantone_ultraViolet(), .pantone_livingCoral(), .pantone_roseQuartz()]
label.flashingTime = 0.17
label.startFlashing()
return label
}()
private let flashLabelInfinite: FlashingLabel = {
let label = FlashingLabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "HELLO FLASHING LABEL"
label.font = .boldSystemFont(ofSize: 20)
label.baseColor = UIColor.black.withAlphaComponent(0.1)
label.flashingColors = [.pantone_roseQuartz(), .pantone_serenity(), .pantone_greenery(), .pantone_ultraViolet(), .pantone_livingCoral(), .pantone_roseQuartz()]
label.flashingTime = 0.17
label.isInfinite = true
label.startFlashing()
return label
}()
Expand All @@ -100,6 +112,7 @@ class FlashingContainerView: UIView {
addSubview(flashLabelFirst)
addSubview(flashLabelSecond)
addSubview(flashLabelMultiple)
addSubview(flashLabelInfinite)
addSubview(flashLabelThird)
addSubview(flashLabelFourth)
layout()
Expand All @@ -117,9 +130,12 @@ class FlashingContainerView: UIView {
flashLabelSecond.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true

flashLabelMultiple.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
flashLabelMultiple.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
flashLabelMultiple.bottomAnchor.constraint(equalTo: centerYAnchor, constant: -4).isActive = true

flashLabelInfinite.topAnchor.constraint(equalTo: centerYAnchor, constant: 4).isActive = true
flashLabelInfinite.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true

flashLabelThird.topAnchor.constraint(equalTo: flashLabelMultiple.bottomAnchor, constant: 8).isActive = true
flashLabelThird.topAnchor.constraint(equalTo: flashLabelInfinite.bottomAnchor, constant: 8).isActive = true
flashLabelThird.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true

flashLabelFourth.topAnchor.constraint(equalTo: flashLabelThird.bottomAnchor, constant: 8).isActive = true
Expand Down
2 changes: 1 addition & 1 deletion FlashingLabel.podspec
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'FlashingLabel'
s.version = '1.0.3'
s.version = '1.0.4'
s.summary = 'very easy and simple flashing label'

# This description is used to generate tags and improve search results.
Expand Down
42 changes: 32 additions & 10 deletions FlashingLabel/Classes/FlashingLabel.swift
Expand Up @@ -12,10 +12,17 @@ public class FlashingLabel: UILabel {
public var baseColor: UIColor?
public var flashingColors: [UIColor]?
public var flashingTime: TimeInterval?
public var isInfinite: Bool = false

private var isDraw = false
private var flashingCount = 0
private var isFlashing = true
private var textCount: Int {
guard let text = text else {return 0}
return Array(text).count
}

private lazy var infiniteColors = [UIColor]()
private lazy var flashingTimer = Timer()

public override func draw(_ rect: CGRect) {
Expand All @@ -39,27 +46,42 @@ public class FlashingLabel: UILabel {
guard let baseColor = baseColor else {return}
textColor = baseColor

guard !flashingTimer.isValid, let flashingTime = flashingTime else {return}
flashingTimer = Timer.scheduledTimer(timeInterval: flashingTime, target: self, selector: #selector(repeatFlashing), userInfo: nil, repeats: true)
guard !flashingTimer.isValid, let flashingTime = flashingTime, let flashingColors = flashingColors else {return}

var tempColors = [UIColor]()

defer {
flashingTimer = Timer.scheduledTimer(timeInterval: flashingTime, target: self, selector: #selector(repeatFlashing(_:)), userInfo: tempColors, repeats: true)
}

guard isInfinite, textCount > flashingColors.count else {
let colorsLastIndex = (flashingColors.count > textCount) ? textCount-1:flashingColors.count-1
tempColors = flashingColors[0...colorsLastIndex].map{$0}
return
}

var colorCount = 0
while tempColors.count < textCount {
tempColors.append(flashingColors[colorCount])
colorCount = (colorCount+1) == flashingColors.count ? 0:colorCount+1
}
}

@objc private func repeatFlashing() {
guard let flashingColors = flashingColors, let text = text else {return}
let colorsLastIndex = (flashingColors.count > Array(text).count) ? Array(text).count-1:flashingColors.count-1
let sliceColors = flashingColors[0...colorsLastIndex].map{$0}
@objc private func repeatFlashing(_ timer: Timer) {
guard let colors = timer.userInfo as? [UIColor], let text = text else {return}

let attribute = NSMutableAttributedString(string: text)
for i in 0..<sliceColors.count {
for i in 0..<colors.count {
let increCount = flashingCount+i
let count = (Array(text).count <= increCount) ? increCount-Array(text).count:increCount
let color = sliceColors[i]
let count = (textCount <= increCount) ? increCount-textCount:increCount
let color = colors[i]
let range = NSMakeRange(count, 1)
attribute.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
}
attributedText = attribute

flashingCount += 1
flashingCount = (Array(text).count == flashingCount) ? 0:flashingCount
flashingCount = (textCount == flashingCount) ? 0:flashingCount
}

}
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -35,6 +35,14 @@ label.text = "HELLO"
label.startFlashing()
```

* infinite mode

![flashinglabel_infinite.gif](https://github.com/dokgi88/dokgi88.github.io/blob/master/_images/FlashingLabel/flashinglabel_infinite.gif?raw=true)
```swift
label.flashingColors = [.orange, .purple]
label.isInfinite = true
```

* stop flashing
```swift
label.stopFlashing()
Expand Down

0 comments on commit 2ff1400

Please sign in to comment.