bar indicator add
This commit is contained in:
parent
4bfa87a901
commit
6ede310cd0
@ -0,0 +1,141 @@
|
||||
//
|
||||
// BarIndicatorView.swift
|
||||
// MVMCoreUI
|
||||
//
|
||||
// Created by Kevin Christiano on 2/3/20.
|
||||
// Copyright © 2020 Verizon Wireless. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
|
||||
open class BarsIndicatorView: View, IndicatorViewProtocol {
|
||||
//--------------------------------------------------
|
||||
// MARK: - Properties
|
||||
//--------------------------------------------------
|
||||
|
||||
let stackView: StackView = {
|
||||
let stackView = StackView()
|
||||
stackView.axis = .horizontal
|
||||
stackView.distribution = .equalSpacing
|
||||
stackView.spacing = 6
|
||||
return stackView
|
||||
}()
|
||||
|
||||
public var barsReference: [(view: View, constraint: NSLayoutConstraint)] = []
|
||||
|
||||
// Dimensions are based on InVision Design Guidelines.
|
||||
public static let indicatorBarWidth: CGFloat = 24
|
||||
public static let indicatorBarHeight: (selected: CGFloat, unselected: CGFloat) = (selected: 4, unselected: 1)
|
||||
|
||||
public var enabledColor: UIColor {
|
||||
return (superview as? CarouselIndicator)?.indicatorTintColor ?? .black
|
||||
}
|
||||
|
||||
public var currentIndexColor: UIColor {
|
||||
return (superview as? CarouselIndicator)?.currentIndicatorColor ?? .black
|
||||
}
|
||||
|
||||
public var disabledColor: UIColor {
|
||||
return (superview as? CarouselIndicator)?.disabledIndicatorColor ?? .mvmCoolGray3
|
||||
}
|
||||
|
||||
/// Returns the numberOfPages count from its parent CarouselIndicator.
|
||||
public var numberOfPages: Int? {
|
||||
return (superview as? CarouselIndicator)?.numberOfPages
|
||||
}
|
||||
|
||||
/// Returns the numberOfPages count from its parent CarouselIndicator.
|
||||
public var currentIndex: Int? {
|
||||
return (superview as? CarouselIndicator)?.currentIndex
|
||||
}
|
||||
|
||||
open var isEnabled: Bool = true {
|
||||
didSet {
|
||||
barsReference.forEach { view, heightConstraint in
|
||||
view.backgroundColor = isEnabled ? enabledColor : disabledColor
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Initializers
|
||||
//--------------------------------------------------
|
||||
|
||||
public init() {
|
||||
super.init(frame: .zero)
|
||||
}
|
||||
|
||||
public required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Setup
|
||||
//--------------------------------------------------
|
||||
|
||||
open override func setupView() {
|
||||
super.setupView()
|
||||
|
||||
guard subviews.isEmpty else { return }
|
||||
|
||||
addSubview(stackView)
|
||||
isUserInteractionEnabled = false
|
||||
|
||||
stackView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
|
||||
stackView.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor).isActive = true
|
||||
trailingAnchor.constraint(lessThanOrEqualTo: stackView.trailingAnchor).isActive = true
|
||||
|
||||
generateBars()
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Methods
|
||||
//--------------------------------------------------
|
||||
|
||||
func generateBars() {
|
||||
|
||||
guard let numberOfPages = numberOfPages,
|
||||
let currentIndex = currentIndex
|
||||
else { return }
|
||||
|
||||
var bars = [(View, NSLayoutConstraint)]()
|
||||
|
||||
for i in 0..<numberOfPages {
|
||||
let bar = View()
|
||||
bar.widthAnchor.constraint(equalToConstant: BarsIndicatorView.indicatorBarWidth).isActive = true
|
||||
bar.backgroundColor = enabledColor
|
||||
let barHeight = i == currentIndex ? BarsIndicatorView.indicatorBarHeight.selected : BarsIndicatorView.indicatorBarHeight.unselected
|
||||
let heightConstraint = bar.heightAnchor.constraint(equalToConstant: barHeight)
|
||||
heightConstraint.isActive = true
|
||||
|
||||
stackView.addArrangedSubview(bar)
|
||||
bars.append((bar, heightConstraint))
|
||||
}
|
||||
|
||||
barsReference = bars
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - IndicatorViewProtocol
|
||||
//--------------------------------------------------
|
||||
|
||||
public override func reset() {
|
||||
super.reset()
|
||||
barsReference.forEach { $0.view.removeFromSuperview() }
|
||||
barsReference = []
|
||||
}
|
||||
|
||||
public func updateUI(previousIndex: Int, newIndex: Int, totalCount: Int, isAnimated: Bool) {
|
||||
|
||||
let expression = {
|
||||
self.barsReference[previousIndex].view.backgroundColor = self.enabledColor
|
||||
self.barsReference[newIndex].view.backgroundColor = self.currentIndexColor
|
||||
self.barsReference[previousIndex].constraint.constant = BarsIndicatorView.indicatorBarHeight.unselected
|
||||
self.barsReference[newIndex].constraint.constant = BarsIndicatorView.indicatorBarHeight.selected
|
||||
self.layoutIfNeeded()
|
||||
}
|
||||
|
||||
isAnimated ? UIView.animate(withDuration: 0.3) { expression() } : expression()
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user