latest state of the page control
This commit is contained in:
parent
af7d2e406e
commit
4577274e08
@ -9,7 +9,7 @@
|
||||
import Foundation
|
||||
|
||||
public protocol IndicatorViewProtocol {
|
||||
func updateUI()
|
||||
func updateUI(oldIndex: Int, newIndex: Int)
|
||||
var isEnabled: Bool { get set }
|
||||
var currentIndex: Int? { get set }
|
||||
var numberOfPages: Int? { get }
|
||||
@ -118,13 +118,18 @@ open class CarouselIndicator: Control {
|
||||
}
|
||||
}
|
||||
|
||||
private(set) var oldIndex = 0
|
||||
private var _currentIndex = 0
|
||||
|
||||
public var currentIndex: Int {
|
||||
get { return _currentIndex }
|
||||
set {
|
||||
guard _currentIndex != newValue else { return }
|
||||
_currentIndex = newValue
|
||||
set (newIndex) {
|
||||
guard _currentIndex != newIndex else { return }
|
||||
oldIndex = _currentIndex
|
||||
_currentIndex = newIndex
|
||||
sendActions(for: .valueChanged)
|
||||
indicatorTappedBlock?(newIndex)
|
||||
indicatorView?.updateUI(oldIndex: oldIndex, newIndex: newIndex)
|
||||
}
|
||||
}
|
||||
|
||||
@ -143,7 +148,7 @@ open class CarouselIndicator: Control {
|
||||
indicatorView = BarsIndicatorView(numberOfBars: numberOfPages)
|
||||
}
|
||||
|
||||
indicatorView?.updateUI()
|
||||
indicatorView?.updateUI(oldIndex: oldIndex, newIndex: currentIndex)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -10,16 +10,23 @@ import Foundation
|
||||
|
||||
|
||||
public class CarouselIndicatorModel: MoleculeModelProtocol {
|
||||
//--------------------------------------------------
|
||||
// MARK: - Properties
|
||||
//--------------------------------------------------
|
||||
|
||||
public var backgroundColor: Color?
|
||||
public var barsColor: Color?
|
||||
|
||||
|
||||
public static var identifier: String {
|
||||
return "carouselIndicator"
|
||||
}
|
||||
|
||||
public var moleculeName: String?
|
||||
public var type: String? = "hybrid"
|
||||
public var barsColor: Color?
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Keys
|
||||
//--------------------------------------------------
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case moleculeName
|
||||
@ -28,16 +35,20 @@ public class CarouselIndicatorModel: MoleculeModelProtocol {
|
||||
case barsColor
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Codec
|
||||
//--------------------------------------------------
|
||||
|
||||
required public init(from decoder: Decoder) throws {
|
||||
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
|
||||
// if let state = try typeContainer.decodeIfPresent(Bool.self, forKey: .state) {
|
||||
// self.state = state
|
||||
// }
|
||||
// action = try typeContainer.decodeModelIfPresent(codingKey: .action, typeCodingKey: ActionCodingKey.actionType)
|
||||
// alternateAction = try typeContainer.decodeModelIfPresent(codingKey: .alternateAction, typeCodingKey: ActionCodingKey.actionType)
|
||||
// if let state = try typeContainer.decodeIfPresent(Bool.self, forKey: .state) {
|
||||
// self.state = state
|
||||
// }
|
||||
// action = try typeContainer.decodeModelIfPresent(codingKey: .action, typeCodingKey: ActionCodingKey.actionType)
|
||||
// alternateAction = try typeContainer.decodeModelIfPresent(codingKey: .alternateAction, typeCodingKey: ActionCodingKey.actionType)
|
||||
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
|
||||
barsColor = try typeContainer.decodeIfPresent(Color.self, forKey: .barsColor)
|
||||
type = try typeContainer.decodeIfPresent(String.self, forKey: .type) ?? "hybrid"
|
||||
type = try typeContainer.decodeIfPresent(String.self, forKey: .type) ?? "hybrid"
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
|
||||
@ -22,7 +22,7 @@ open class BarsIndicatorView: View, IndicatorViewProtocol {
|
||||
return stackView
|
||||
}()
|
||||
|
||||
var barsReference: [(View, NSLayoutConstraint)] = []
|
||||
var barsReference: [(view: View, constraint: NSLayoutConstraint)] = []
|
||||
|
||||
// Dimensions are based on InVision Design Guidelines.
|
||||
public static let indicatorBarWidth: CGFloat = 24
|
||||
@ -55,10 +55,8 @@ open class BarsIndicatorView: View, IndicatorViewProtocol {
|
||||
|
||||
open var isEnabled: Bool = true {
|
||||
didSet {
|
||||
if isEnabled {
|
||||
|
||||
} else {
|
||||
|
||||
barsReference.forEach { view, heightConstraint in
|
||||
view.backgroundColor = isEnabled ? enabledColor : disabledColor
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -94,7 +92,7 @@ open class BarsIndicatorView: View, IndicatorViewProtocol {
|
||||
stackView.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor).isActive = true
|
||||
trailingAnchor.constraint(lessThanOrEqualTo: stackView.trailingAnchor).isActive = true
|
||||
|
||||
removeIndicators()
|
||||
clearBars()
|
||||
|
||||
let tapGesture = UITapGestureRecognizer()
|
||||
tapGesture.addTarget(self, action: #selector(indicatorTapped(_:)))
|
||||
@ -114,20 +112,20 @@ open class BarsIndicatorView: View, IndicatorViewProtocol {
|
||||
let bar = View()
|
||||
bar.widthAnchor.constraint(equalToConstant: BarsIndicatorView.indicatorBarWidth).isActive = true
|
||||
bar.backgroundColor = enabledColor
|
||||
// let barHeight = i == currentIndex ? indicatorBarWidth.indicatorBarHeight.selected : indicatorBarWidth.indicatorBarHeight.unselected
|
||||
// let heightConstraint = bar.heightAnchor.constraint(equalToConstant: barHeight)
|
||||
// heightConstraint.isActive = true
|
||||
// let barHeight = i == currentIndex ? indicatorBarWidth.indicatorBarHeight.selected : indicatorBarWidth.indicatorBarHeight.unselected
|
||||
// let heightConstraint = bar.heightAnchor.constraint(equalToConstant: barHeight)
|
||||
// heightConstraint.isActive = true
|
||||
|
||||
// stackView.app
|
||||
// bars.append((bar, heightConstraint))
|
||||
// stackView.app
|
||||
// bars.append((bar, heightConstraint))
|
||||
}
|
||||
|
||||
barsReference = bars
|
||||
}
|
||||
|
||||
func removeIndicators() {
|
||||
// indicatorBars.forEach { $0.removeFromSuperview() }
|
||||
// indicatorBars = []
|
||||
func clearBars() {
|
||||
barsReference.forEach { $0.view.removeFromSuperview() }
|
||||
barsReference = []
|
||||
}
|
||||
|
||||
func indicatorTapped(_ tapGesture: UITapGestureRecognizer?) {
|
||||
@ -135,15 +133,9 @@ open class BarsIndicatorView: View, IndicatorViewProtocol {
|
||||
if isEnabled {
|
||||
let touchPoint_X = tapGesture?.location(in: self).x ?? 0.0
|
||||
|
||||
// let index = (indicatorView as? BarsIndicatorView)?.stackView.arrangedSubviews.firstIndex { indicator in
|
||||
// return indicator.frame.maxX >= touchPoint_X && indicator.frame.minX <= touchPoint_X
|
||||
// }
|
||||
//
|
||||
// if let selectIndex = index {
|
||||
// currentIndex = selectIndex
|
||||
// (indicatorView as? BarsIndicatorView)?sendActions(for: .valueChanged)
|
||||
// indicatorTappedBlock?(selectIndex)
|
||||
// }
|
||||
currentIndex = barsReference.firstIndex { view, _ in
|
||||
return view.frame.maxX >= touchPoint_X && view.frame.minX <= touchPoint_X
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -151,14 +143,14 @@ open class BarsIndicatorView: View, IndicatorViewProtocol {
|
||||
// MARK: - IndicatorViewProtocol
|
||||
//--------------------------------------------------
|
||||
|
||||
public func updateUI() {
|
||||
public func updateUI(oldIndex: Int, newIndex: Int) {
|
||||
|
||||
let expression = {
|
||||
// stackView.arrangedSubviews[oldIndex].heightConstraint?.constant = BarsIndicatorView.indicatorBarHeight.unselected
|
||||
// self.heightConstraint?.constant = BarsIndicatorView.indicatorBarHeight.selected
|
||||
// self.layoutIfNeeded()
|
||||
self.barsReference[oldIndex].constraint.constant = BarsIndicatorView.indicatorBarHeight.unselected
|
||||
self.barsReference[newIndex].constraint.constant = BarsIndicatorView.indicatorBarHeight.selected
|
||||
self.layoutIfNeeded()
|
||||
}
|
||||
|
||||
// isAnimated ? UIView.animate(withDuration: 0.3) { expression() } : expression()
|
||||
isAnimated ? UIView.animate(withDuration: 0.3) { expression() } : expression()
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,22 +22,11 @@ open class NumericIndicatorView: View, IndicatorViewProtocol {
|
||||
return label
|
||||
}()
|
||||
|
||||
|
||||
// Left and right arrows for Numeric indicator
|
||||
open var leftArrow = MFLoadImageView()
|
||||
open var rightArrow = MFLoadImageView()
|
||||
|
||||
// open var activeColor: (enabled: UIColor, disabled: UIColor)
|
||||
|
||||
open var isEnabled: Bool = true {
|
||||
didSet {
|
||||
if isEnabled {
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
open var isEnabled: Bool = true
|
||||
|
||||
/// Returns the currentIndex from its parent CarouselIndicator.
|
||||
public var currentIndex: Int? {
|
||||
@ -148,12 +137,12 @@ open class NumericIndicatorView: View, IndicatorViewProtocol {
|
||||
// MARK: - IndicatorViewProtocol
|
||||
//--------------------------------------------------
|
||||
|
||||
open func updateUI() {
|
||||
open func updateUI(oldIndex: Int, newIndex: Int) {
|
||||
|
||||
DispatchQueue.main.async { [weak self] in
|
||||
guard let self = self else { return }
|
||||
|
||||
self.titleLabel.text = "\(self.currentIndex)/\(self.numberOfPages)"
|
||||
self.titleLabel.text = "\(newIndex)/\(self.numberOfPages ?? 0)"
|
||||
self.layoutIfNeeded()
|
||||
(self.superview as? CarouselIndicator)?.sendActions(for: .valueChanged)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user