From e4384066ae1615e36f1d5cefa6e20314a9e52a8a Mon Sep 17 00:00:00 2001 From: Kevin G Christiano Date: Mon, 15 Jun 2020 11:36:42 -0400 Subject: [PATCH] opening diameter for dev --- .../Atomic/Atoms/Views/LoadingSpinner.swift | 38 ++++++++++++++----- .../Atoms/Views/LoadingSpinnerModel.swift | 4 ++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/MVMCoreUI/Atomic/Atoms/Views/LoadingSpinner.swift b/MVMCoreUI/Atomic/Atoms/Views/LoadingSpinner.swift index 17be8ba2..b3224f68 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/LoadingSpinner.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/LoadingSpinner.swift @@ -19,6 +19,13 @@ open class LoadingSpinner: View { public var lineWidth: CGFloat = 3.0 public var speed: Float = 1.5 + + //-------------------------------------------------- + // MARK: - Constraints + //-------------------------------------------------- + + public var heightConstraint: NSLayoutConstraint? + public var widthConstraint: NSLayoutConstraint? //-------------------------------------------------- // MARK: - Lifecycle @@ -41,8 +48,19 @@ open class LoadingSpinner: View { layer.speed = speed let halfWidth = lineWidth / 2 let radius = (bounds.width - lineWidth) / 2 - layer.path = UIBezierPath(arcCenter: CGPoint(x: radius + halfWidth, y: radius + halfWidth), radius: radius, startAngle: -CGFloat.pi / 2, endAngle: 2 * CGFloat.pi, clockwise: true).cgPath - + layer.path = UIBezierPath(arcCenter: CGPoint(x: radius + halfWidth, + y: radius + halfWidth), + radius: radius, + startAngle: -CGFloat.pi / 2, + endAngle: 2 * CGFloat.pi, + clockwise: true).cgPath + } + + public override func reset() { + super.reset() + + heightConstraint?.isActive = false + widthConstraint?.isActive = false } //-------------------------------------------------- @@ -152,14 +170,12 @@ open class LoadingSpinner: View { layer.removeAllAnimations() } - func pinWidthAndHeight(radius: CGFloat) { + func pinWidthAndHeight(diameter: CGFloat) { - let diameter: CGFloat = radius * 2 + lineWidth - - NSLayoutConstraint.activate([ - heightAnchor.constraint(equalToConstant: diameter), - widthAnchor.constraint(equalToConstant: diameter) - ]) + heightConstraint = heightAnchor.constraint(equalToConstant: diameter + lineWidth) + widthConstraint = widthAnchor.constraint(equalToConstant: diameter + lineWidth) + heightConstraint?.isActive = true + widthConstraint?.isActive = true } //-------------------------------------------------- @@ -172,6 +188,10 @@ open class LoadingSpinner: View { strokeColor = model.strokeColor.uiColor lineWidth = model.lineWidth + + if let diameter = model.diameter { + pinWidthAndHeight(diameter: diameter) + } } open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? { diff --git a/MVMCoreUI/Atomic/Atoms/Views/LoadingSpinnerModel.swift b/MVMCoreUI/Atomic/Atoms/Views/LoadingSpinnerModel.swift index 604c0689..ef299d46 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/LoadingSpinnerModel.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/LoadingSpinnerModel.swift @@ -18,6 +18,7 @@ open class LoadingSpinnerModel: MoleculeModelProtocol { public static var identifier: String = "loadingSpinner" public var strokeColor = Color(uiColor: .mvmBlack) public var lineWidth: CGFloat = 3 + public var diameter: CGFloat? //-------------------------------------------------- // MARK: - Keys @@ -28,6 +29,7 @@ open class LoadingSpinnerModel: MoleculeModelProtocol { case backgroundColor case strokeColor case lineWidth + case diameter } //-------------------------------------------------- @@ -38,6 +40,7 @@ open class LoadingSpinnerModel: MoleculeModelProtocol { let typeContainer = try decoder.container(keyedBy: CodingKeys.self) backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) + diameter = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .diameter) if let strokeColor = try typeContainer.decodeIfPresent(Color.self, forKey: .strokeColor) { self.strokeColor = strokeColor @@ -52,6 +55,7 @@ open class LoadingSpinnerModel: MoleculeModelProtocol { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(moleculeName, forKey: .moleculeName) try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) + try container.encodeIfPresent(diameter, forKey: .diameter) try container.encode(strokeColor, forKey: .strokeColor) try container.encode(lineWidth, forKey: .lineWidth) }