From 99bf3f8458577ae24ba923edc705078b5a53903a Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 1 Jan 2017 19:49:34 -0800 Subject: [PATCH 1/2] Disable animation on first render, since we'd be animating from a default value so the animation is deceptive. --- LoopUI/Views/BasalStateView.swift | 25 +++++++++++-------------- LoopUI/Views/LevelMaskView.swift | 8 +++++--- LoopUI/Views/LoopStateView.swift | 5 ++++- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/LoopUI/Views/BasalStateView.swift b/LoopUI/Views/BasalStateView.swift index 16c3c82eff..59332eb91a 100644 --- a/LoopUI/Views/BasalStateView.swift +++ b/LoopUI/Views/BasalStateView.swift @@ -10,6 +10,7 @@ import UIKit public final class BasalStateView: UIView { + var netBasalPercent: Double = 0 { didSet { animateToPath(drawPath()) @@ -30,8 +31,6 @@ public final class BasalStateView: UIView { shapeLayer.lineWidth = 2 shapeLayer.fillColor = UIColor.doseTintColor.withAlphaComponent(0.5).cgColor shapeLayer.strokeColor = UIColor.doseTintColor.cgColor - - shapeLayer.path = drawPath() } required public init?(coder aDecoder: NSCoder) { @@ -40,14 +39,10 @@ public final class BasalStateView: UIView { shapeLayer.lineWidth = 2 shapeLayer.fillColor = UIColor.doseTintColor.withAlphaComponent(0.5).cgColor shapeLayer.strokeColor = UIColor.doseTintColor.cgColor - - shapeLayer.path = drawPath() } override public func layoutSubviews() { super.layoutSubviews() - - shapeLayer.path = drawPath() } private func drawPath() -> CGPath { @@ -75,14 +70,16 @@ public final class BasalStateView: UIView { private static let AnimationKey = "com.loudnate.Naterade.shapePathAnimation" private func animateToPath(_ path: CGPath) { - let animation = CABasicAnimation(keyPath: "path") - animation.fromValue = shapeLayer.path ?? drawPath() - animation.toValue = path - animation.duration = 1 - animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) - - shapeLayer.add(animation, forKey: type(of: self).AnimationKey) - + if shapeLayer.path != nil { + let animation = CABasicAnimation(keyPath: "path") + animation.fromValue = shapeLayer.path ?? drawPath() + animation.toValue = path + animation.duration = 1 + animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) + + shapeLayer.add(animation, forKey: type(of: self).AnimationKey) + } + shapeLayer.path = path } } diff --git a/LoopUI/Views/LevelMaskView.swift b/LoopUI/Views/LevelMaskView.swift index ef5b4d72f0..8ec090e12c 100644 --- a/LoopUI/Views/LevelMaskView.swift +++ b/LoopUI/Views/LevelMaskView.swift @@ -12,10 +12,12 @@ import UIKit // Inspired by https://github.com/carekit-apple/CareKit/blob/master/CareKit/CareCard/OCKHeartView.h public class LevelMaskView: UIView { + var firstDataUpdate = true var value: Double = 1.0 { didSet { - animateFill() + animateFill(duration: firstDataUpdate ? 0 : 1.25) + firstDataUpdate = false } } @@ -66,8 +68,8 @@ public class LevelMaskView: UIView { fillView?.backgroundColor = tintColor } - private func animateFill() { - UIView.animate(withDuration: 1.25, delay: 0, options: .beginFromCurrentState, animations: { + private func animateFill(duration: Double) { + UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState, animations: { self.updateFillViewFrame() }, completion: nil) } diff --git a/LoopUI/Views/LoopStateView.swift b/LoopUI/Views/LoopStateView.swift index 2cae4ec3e4..df07365c08 100644 --- a/LoopUI/Views/LoopStateView.swift +++ b/LoopUI/Views/LoopStateView.swift @@ -9,6 +9,8 @@ import UIKit public final class LoopStateView: UIView { + var firstDataUpdate = true + enum Freshness { case fresh case aging @@ -112,7 +114,7 @@ public final class LoopStateView: UIView { let group = CAAnimationGroup() group.animations = [path, width] - group.duration = 1 + group.duration = firstDataUpdate ? 0 : 1 group.repeatCount = HUGE group.autoreverses = true group.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) @@ -122,6 +124,7 @@ public final class LoopStateView: UIView { shapeLayer.removeAnimation(forKey: type(of: self).AnimationKey) } } + firstDataUpdate = false } } } From 6cca3191264263a5d179f6f3762061980c784445 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 2 Jan 2017 10:27:35 -0800 Subject: [PATCH 2/2] Use the more specific TimeInterval type for duration --- LoopUI/Views/LevelMaskView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LoopUI/Views/LevelMaskView.swift b/LoopUI/Views/LevelMaskView.swift index 8ec090e12c..ec7840fd71 100644 --- a/LoopUI/Views/LevelMaskView.swift +++ b/LoopUI/Views/LevelMaskView.swift @@ -68,7 +68,7 @@ public class LevelMaskView: UIView { fillView?.backgroundColor = tintColor } - private func animateFill(duration: Double) { + private func animateFill(duration: TimeInterval) { UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState, animations: { self.updateFillViewFrame() }, completion: nil)