Skip to content
This repository was archived by the owner on Jan 19, 2022. It is now read-only.

Commit bb9bf6a

Browse files
committed
Add new animatable properties
1 parent c9be3ca commit bb9bf6a

10 files changed

+282
-11
lines changed

CHANGELOG.md

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
# Version 1.8.0
2+
3+
Add new `animateProperties(duration:animations:completion:)` method.
4+
5+
This allows you to animate any of the animatable properties of the `UICircularProgressRing`, which currently are: `innerRingColor`, `innerRingWidth`, `outerRingColor`, `outerRingWidth`, `innerRingSpacing`, `fontColor`.
6+
7+
Example usage:
8+
9+
```swift
10+
self.ring.animateProperties(duration: 1.5) {
11+
// Animate things here
12+
self.ring.innerRingColor = .purple
13+
self.ring.outerRingColor = .blue
14+
}
15+
```
16+
117
# Version 1.7.7
218

319
Add Objective-C support, thanks to [@hohteri](https://github.com/.hohteri)

UICircularProgressRing.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Pod::Spec.new do |s|
33

44
s.name = "UICircularProgressRing"
5-
s.version = "1.7.7"
5+
s.version = "1.8.0"
66
s.summary = "A highly customizable circular progress bar for iOS written in Swift 3"
77

88
s.description = <<-DESC

UICircularProgressRing/UICircularProgressRingLayer.swift

+32-8
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,25 @@ class UICircularProgressRingLayer: CAShapeLayer {
108108

109109
// The value label which draws the text for the current value
110110
lazy private var valueLabel: UILabel = UILabel(frame: .zero)
111-
111+
112+
// MARK: Animatable properties
113+
114+
// Whether or not animatable properties should be animated when changed
115+
internal var shouldAnimateProperties: Bool = false
116+
117+
// The animation duration for a animatable property animation
118+
internal var propertyAnimationDuration: TimeInterval = 0.0
119+
120+
// The properties which are animatable
121+
private static let animatableProperties: [String] = ["innerRingWidth", "innerRingColor",
122+
"outerRingWidth", "outerRingColor",
123+
"fontColor", "innerRingSpacing"]
124+
125+
// Returns whether or not a given property key is animatable
126+
private static func isAnimatableProperty(_ key: String) -> Bool {
127+
return animatableProperties.index(of: key) != nil
128+
}
129+
112130
// MARK: Draw
113131

114132
/**
@@ -137,11 +155,11 @@ class UICircularProgressRingLayer: CAShapeLayer {
137155
Watches for changes in the value property, and setNeedsDisplay accordingly
138156
*/
139157
override class func needsDisplay(forKey key: String) -> Bool {
140-
if key == "value" {
158+
if key == "value" || isAnimatableProperty(key) {
141159
return true
160+
} else {
161+
return super.needsDisplay(forKey: key)
142162
}
143-
144-
return super.needsDisplay(forKey: key)
145163
}
146164

147165
/**
@@ -151,12 +169,18 @@ class UICircularProgressRingLayer: CAShapeLayer {
151169
if event == "value" && self.animated {
152170
let animation = CABasicAnimation(keyPath: "value")
153171
animation.fromValue = self.presentation()?.value(forKey: "value")
154-
animation.timingFunction = CAMediaTimingFunction(name: animationStyle)
155-
animation.duration = animationDuration
172+
animation.timingFunction = CAMediaTimingFunction(name: self.animationStyle)
173+
animation.duration = self.animationDuration
156174
return animation
175+
} else if UICircularProgressRingLayer.isAnimatableProperty(event) {
176+
let animation = CABasicAnimation(keyPath: event)
177+
animation.fromValue = self.presentation()?.value(forKey: event)
178+
animation.timingFunction = CAMediaTimingFunction(name: self.animationStyle)
179+
animation.duration = self.propertyAnimationDuration
180+
return animation
181+
} else {
182+
return super.action(forKey: event)
157183
}
158-
159-
return super.action(forKey: event)
160184
}
161185

162186

UICircularProgressRing/UICircularProgressRingView.swift

+43
Original file line numberDiff line numberDiff line change
@@ -836,4 +836,47 @@ import UIKit
836836
self.value = value
837837
CATransaction.commit()
838838
}
839+
840+
/**
841+
Typealias for animateProperties(duration:animations:completion:) fucntion completion
842+
*/
843+
public typealias PropertyAnimationCompletion = (() -> Void)
844+
845+
/**
846+
This function allows animation of the animatable properties of the `UICircularProgressRing`.
847+
These properties include `innerRingColor, innerRingWidth, outerRingColor, outerRingWidth, innerRingSpacing, fontColor`.
848+
849+
Simply call this function and inside of the animation block change the animatable properties as you would in any `UView`
850+
animation block.
851+
852+
The completion block is called when all animations finish.
853+
*/
854+
@objc open func animateProperties(duration: TimeInterval, animations: () -> Void) {
855+
self.animateProperties(duration: duration, animations: animations, completion: nil)
856+
}
857+
858+
/**
859+
This function allows animation of the animatable properties of the `UICircularProgressRing`.
860+
These properties include `innerRingColor, innerRingWidth, outerRingColor, outerRingWidth, innerRingSpacing, fontColor`.
861+
862+
Simply call this function and inside of the animation block change the animatable properties as you would in any `UView`
863+
animation block.
864+
865+
The completion block is called when all animations finish.
866+
*/
867+
@objc open func animateProperties(duration: TimeInterval, animations: () -> Void,
868+
completion: PropertyAnimationCompletion? = nil) {
869+
self.ringLayer.shouldAnimateProperties = true
870+
self.ringLayer.propertyAnimationDuration = duration
871+
CATransaction.begin()
872+
CATransaction.setCompletionBlock {
873+
// Reset and call completion
874+
self.ringLayer.shouldAnimateProperties = false
875+
self.ringLayer.propertyAnimationDuration = 0.0
876+
completion?()
877+
}
878+
// Commit and perform animations
879+
animations()
880+
CATransaction.commit()
881+
}
839882
}

docs/Classes/UICircularProgressRingView.html

+94
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,100 @@ <h4>Parameters</h4>
14541454
</section>
14551455
</div>
14561456
</li>
1457+
<li class="item">
1458+
<div>
1459+
<code>
1460+
<a name="/s:22UICircularProgressRing0abC4ViewC27PropertyAnimationCompletiona"></a>
1461+
<a name="//apple_ref/swift/Alias/PropertyAnimationCompletion" class="dashAnchor"></a>
1462+
<a class="token" href="#/s:22UICircularProgressRing0abC4ViewC27PropertyAnimationCompletiona">PropertyAnimationCompletion</a>
1463+
</code>
1464+
</div>
1465+
<div class="height-container">
1466+
<div class="pointer-container"></div>
1467+
<section class="section">
1468+
<div class="pointer"></div>
1469+
<div class="abstract">
1470+
<p>Typealias for animateProperties(duration:animations:completion:) fucntion completion</p>
1471+
1472+
</div>
1473+
<div class="declaration">
1474+
<h4>Declaration</h4>
1475+
<div class="language">
1476+
<p class="aside-title">Swift</p>
1477+
<pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">typealias</span> <span class="kt">PropertyAnimationCompletion</span> <span class="o">=</span> <span class="p">(()</span> <span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">)</span></code></pre>
1478+
1479+
</div>
1480+
</div>
1481+
</section>
1482+
</div>
1483+
</li>
1484+
<li class="item">
1485+
<div>
1486+
<code>
1487+
<a name="/c:@M@UICircularProgressRing@objc(cs)UICircularProgressRingView(im)animatePropertiesWithDuration:animations:"></a>
1488+
<a name="//apple_ref/swift/Method/animateProperties(duration:animations:)" class="dashAnchor"></a>
1489+
<a class="token" href="#/c:@M@UICircularProgressRing@objc(cs)UICircularProgressRingView(im)animatePropertiesWithDuration:animations:">animateProperties(duration:animations:)</a>
1490+
</code>
1491+
</div>
1492+
<div class="height-container">
1493+
<div class="pointer-container"></div>
1494+
<section class="section">
1495+
<div class="pointer"></div>
1496+
<div class="abstract">
1497+
<p>This function allows animation of the animatable properties of the <code>UICircularProgressRing</code>.
1498+
These properties include <code>innerRingColor, innerRingWidth, outerRingColor, outerRingWidth, innerRingSpacing, fontColor</code>.</p>
1499+
1500+
<p>Simply call this function and inside of the animation block change the animatable properties as you would in any <code>UView</code>
1501+
animation block.</p>
1502+
1503+
<p>The completion block is called when all animations finish.</p>
1504+
1505+
</div>
1506+
<div class="declaration">
1507+
<h4>Declaration</h4>
1508+
<div class="language">
1509+
<p class="aside-title">Swift</p>
1510+
<pre class="highlight swift"><code><span class="kd">@objc</span> <span class="kd">open</span> <span class="kd">func</span> <span class="nf">animateProperties</span><span class="p">(</span><span class="nv">duration</span><span class="p">:</span> <span class="kt">TimeInterval</span><span class="p">,</span> <span class="nv">animations</span><span class="p">:</span> <span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">)</span></code></pre>
1511+
1512+
</div>
1513+
</div>
1514+
</section>
1515+
</div>
1516+
</li>
1517+
<li class="item">
1518+
<div>
1519+
<code>
1520+
<a name="/c:@M@UICircularProgressRing@objc(cs)UICircularProgressRingView(im)animatePropertiesWithDuration:animations:completion:"></a>
1521+
<a name="//apple_ref/swift/Method/animateProperties(duration:animations:completion:)" class="dashAnchor"></a>
1522+
<a class="token" href="#/c:@M@UICircularProgressRing@objc(cs)UICircularProgressRingView(im)animatePropertiesWithDuration:animations:completion:">animateProperties(duration:animations:completion:)</a>
1523+
</code>
1524+
</div>
1525+
<div class="height-container">
1526+
<div class="pointer-container"></div>
1527+
<section class="section">
1528+
<div class="pointer"></div>
1529+
<div class="abstract">
1530+
<p>This function allows animation of the animatable properties of the <code>UICircularProgressRing</code>.
1531+
These properties include <code>innerRingColor, innerRingWidth, outerRingColor, outerRingWidth, innerRingSpacing, fontColor</code>.</p>
1532+
1533+
<p>Simply call this function and inside of the animation block change the animatable properties as you would in any <code>UView</code>
1534+
animation block.</p>
1535+
1536+
<p>The completion block is called when all animations finish.</p>
1537+
1538+
</div>
1539+
<div class="declaration">
1540+
<h4>Declaration</h4>
1541+
<div class="language">
1542+
<p class="aside-title">Swift</p>
1543+
<pre class="highlight swift"><code><span class="kd">@objc</span> <span class="kd">open</span> <span class="kd">func</span> <span class="nf">animateProperties</span><span class="p">(</span><span class="nv">duration</span><span class="p">:</span> <span class="kt">TimeInterval</span><span class="p">,</span> <span class="nv">animations</span><span class="p">:</span> <span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">,</span>
1544+
<span class="nv">completion</span><span class="p">:</span> <span class="kt"><a href="../Classes/UICircularProgressRingView.html#/s:22UICircularProgressRing0abC4ViewC27PropertyAnimationCompletiona">PropertyAnimationCompletion</a></span><span class="p">?</span> <span class="o">=</span> <span class="kc">nil</span><span class="p">)</span></code></pre>
1545+
1546+
</div>
1547+
</div>
1548+
</section>
1549+
</div>
1550+
</li>
14571551
</ul>
14581552
</div>
14591553
</section>

docs/docsets/UICircularProgressRing.docset/Contents/Resources/Documents/Classes/UICircularProgressRingView.html

+94
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,100 @@ <h4>Parameters</h4>
14541454
</section>
14551455
</div>
14561456
</li>
1457+
<li class="item">
1458+
<div>
1459+
<code>
1460+
<a name="/s:22UICircularProgressRing0abC4ViewC27PropertyAnimationCompletiona"></a>
1461+
<a name="//apple_ref/swift/Alias/PropertyAnimationCompletion" class="dashAnchor"></a>
1462+
<a class="token" href="#/s:22UICircularProgressRing0abC4ViewC27PropertyAnimationCompletiona">PropertyAnimationCompletion</a>
1463+
</code>
1464+
</div>
1465+
<div class="height-container">
1466+
<div class="pointer-container"></div>
1467+
<section class="section">
1468+
<div class="pointer"></div>
1469+
<div class="abstract">
1470+
<p>Typealias for animateProperties(duration:animations:completion:) fucntion completion</p>
1471+
1472+
</div>
1473+
<div class="declaration">
1474+
<h4>Declaration</h4>
1475+
<div class="language">
1476+
<p class="aside-title">Swift</p>
1477+
<pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">typealias</span> <span class="kt">PropertyAnimationCompletion</span> <span class="o">=</span> <span class="p">(()</span> <span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">)</span></code></pre>
1478+
1479+
</div>
1480+
</div>
1481+
</section>
1482+
</div>
1483+
</li>
1484+
<li class="item">
1485+
<div>
1486+
<code>
1487+
<a name="/c:@M@UICircularProgressRing@objc(cs)UICircularProgressRingView(im)animatePropertiesWithDuration:animations:"></a>
1488+
<a name="//apple_ref/swift/Method/animateProperties(duration:animations:)" class="dashAnchor"></a>
1489+
<a class="token" href="#/c:@M@UICircularProgressRing@objc(cs)UICircularProgressRingView(im)animatePropertiesWithDuration:animations:">animateProperties(duration:animations:)</a>
1490+
</code>
1491+
</div>
1492+
<div class="height-container">
1493+
<div class="pointer-container"></div>
1494+
<section class="section">
1495+
<div class="pointer"></div>
1496+
<div class="abstract">
1497+
<p>This function allows animation of the animatable properties of the <code>UICircularProgressRing</code>.
1498+
These properties include <code>innerRingColor, innerRingWidth, outerRingColor, outerRingWidth, innerRingSpacing, fontColor</code>.</p>
1499+
1500+
<p>Simply call this function and inside of the animation block change the animatable properties as you would in any <code>UView</code>
1501+
animation block.</p>
1502+
1503+
<p>The completion block is called when all animations finish.</p>
1504+
1505+
</div>
1506+
<div class="declaration">
1507+
<h4>Declaration</h4>
1508+
<div class="language">
1509+
<p class="aside-title">Swift</p>
1510+
<pre class="highlight swift"><code><span class="kd">@objc</span> <span class="kd">open</span> <span class="kd">func</span> <span class="nf">animateProperties</span><span class="p">(</span><span class="nv">duration</span><span class="p">:</span> <span class="kt">TimeInterval</span><span class="p">,</span> <span class="nv">animations</span><span class="p">:</span> <span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">)</span></code></pre>
1511+
1512+
</div>
1513+
</div>
1514+
</section>
1515+
</div>
1516+
</li>
1517+
<li class="item">
1518+
<div>
1519+
<code>
1520+
<a name="/c:@M@UICircularProgressRing@objc(cs)UICircularProgressRingView(im)animatePropertiesWithDuration:animations:completion:"></a>
1521+
<a name="//apple_ref/swift/Method/animateProperties(duration:animations:completion:)" class="dashAnchor"></a>
1522+
<a class="token" href="#/c:@M@UICircularProgressRing@objc(cs)UICircularProgressRingView(im)animatePropertiesWithDuration:animations:completion:">animateProperties(duration:animations:completion:)</a>
1523+
</code>
1524+
</div>
1525+
<div class="height-container">
1526+
<div class="pointer-container"></div>
1527+
<section class="section">
1528+
<div class="pointer"></div>
1529+
<div class="abstract">
1530+
<p>This function allows animation of the animatable properties of the <code>UICircularProgressRing</code>.
1531+
These properties include <code>innerRingColor, innerRingWidth, outerRingColor, outerRingWidth, innerRingSpacing, fontColor</code>.</p>
1532+
1533+
<p>Simply call this function and inside of the animation block change the animatable properties as you would in any <code>UView</code>
1534+
animation block.</p>
1535+
1536+
<p>The completion block is called when all animations finish.</p>
1537+
1538+
</div>
1539+
<div class="declaration">
1540+
<h4>Declaration</h4>
1541+
<div class="language">
1542+
<p class="aside-title">Swift</p>
1543+
<pre class="highlight swift"><code><span class="kd">@objc</span> <span class="kd">open</span> <span class="kd">func</span> <span class="nf">animateProperties</span><span class="p">(</span><span class="nv">duration</span><span class="p">:</span> <span class="kt">TimeInterval</span><span class="p">,</span> <span class="nv">animations</span><span class="p">:</span> <span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">,</span>
1544+
<span class="nv">completion</span><span class="p">:</span> <span class="kt"><a href="../Classes/UICircularProgressRingView.html#/s:22UICircularProgressRing0abC4ViewC27PropertyAnimationCompletiona">PropertyAnimationCompletion</a></span><span class="p">?</span> <span class="o">=</span> <span class="kc">nil</span><span class="p">)</span></code></pre>
1545+
1546+
</div>
1547+
</div>
1548+
</section>
1549+
</div>
1550+
</li>
14571551
</ul>
14581552
</div>
14591553
</section>

0 commit comments

Comments
 (0)