Skip to content

Commit 9d3cfad

Browse files
Bharat Medirattaps2
authored andcommitted
Disable animation on first render, since we'd be animating from a (#354)
* Disable animation on first render, since we'd be animating from a default value so the animation is deceptive. * Use the more specific TimeInterval type for duration
1 parent 9566eac commit 9d3cfad

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

LoopUI/Views/BasalStateView.swift

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import UIKit
1010

1111

1212
public final class BasalStateView: UIView {
13+
1314
var netBasalPercent: Double = 0 {
1415
didSet {
1516
animateToPath(drawPath())
@@ -30,8 +31,6 @@ public final class BasalStateView: UIView {
3031
shapeLayer.lineWidth = 2
3132
shapeLayer.fillColor = UIColor.doseTintColor.withAlphaComponent(0.5).cgColor
3233
shapeLayer.strokeColor = UIColor.doseTintColor.cgColor
33-
34-
shapeLayer.path = drawPath()
3534
}
3635

3736
required public init?(coder aDecoder: NSCoder) {
@@ -40,14 +39,10 @@ public final class BasalStateView: UIView {
4039
shapeLayer.lineWidth = 2
4140
shapeLayer.fillColor = UIColor.doseTintColor.withAlphaComponent(0.5).cgColor
4241
shapeLayer.strokeColor = UIColor.doseTintColor.cgColor
43-
44-
shapeLayer.path = drawPath()
4542
}
4643

4744
override public func layoutSubviews() {
4845
super.layoutSubviews()
49-
50-
shapeLayer.path = drawPath()
5146
}
5247

5348
private func drawPath() -> CGPath {
@@ -75,14 +70,16 @@ public final class BasalStateView: UIView {
7570
private static let AnimationKey = "com.loudnate.Naterade.shapePathAnimation"
7671

7772
private func animateToPath(_ path: CGPath) {
78-
let animation = CABasicAnimation(keyPath: "path")
79-
animation.fromValue = shapeLayer.path ?? drawPath()
80-
animation.toValue = path
81-
animation.duration = 1
82-
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
83-
84-
shapeLayer.add(animation, forKey: type(of: self).AnimationKey)
85-
73+
if shapeLayer.path != nil {
74+
let animation = CABasicAnimation(keyPath: "path")
75+
animation.fromValue = shapeLayer.path ?? drawPath()
76+
animation.toValue = path
77+
animation.duration = 1
78+
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
79+
80+
shapeLayer.add(animation, forKey: type(of: self).AnimationKey)
81+
}
82+
8683
shapeLayer.path = path
8784
}
8885
}

LoopUI/Views/LevelMaskView.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import UIKit
1212
// Inspired by https://github.com/carekit-apple/CareKit/blob/master/CareKit/CareCard/OCKHeartView.h
1313

1414
public class LevelMaskView: UIView {
15+
var firstDataUpdate = true
1516

1617
var value: Double = 1.0 {
1718
didSet {
18-
animateFill()
19+
animateFill(duration: firstDataUpdate ? 0 : 1.25)
20+
firstDataUpdate = false
1921
}
2022
}
2123

@@ -66,8 +68,8 @@ public class LevelMaskView: UIView {
6668
fillView?.backgroundColor = tintColor
6769
}
6870

69-
private func animateFill() {
70-
UIView.animate(withDuration: 1.25, delay: 0, options: .beginFromCurrentState, animations: {
71+
private func animateFill(duration: TimeInterval) {
72+
UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState, animations: {
7173
self.updateFillViewFrame()
7274
}, completion: nil)
7375
}

LoopUI/Views/LoopStateView.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import UIKit
1010

1111
public final class LoopStateView: UIView {
12+
var firstDataUpdate = true
13+
1214
enum Freshness {
1315
case fresh
1416
case aging
@@ -112,7 +114,7 @@ public final class LoopStateView: UIView {
112114

113115
let group = CAAnimationGroup()
114116
group.animations = [path, width]
115-
group.duration = 1
117+
group.duration = firstDataUpdate ? 0 : 1
116118
group.repeatCount = HUGE
117119
group.autoreverses = true
118120
group.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
@@ -122,6 +124,7 @@ public final class LoopStateView: UIView {
122124
shapeLayer.removeAnimation(forKey: type(of: self).AnimationKey)
123125
}
124126
}
127+
firstDataUpdate = false
125128
}
126129
}
127130
}

0 commit comments

Comments
 (0)