<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Juan Navas on Medium]]></title>
        <description><![CDATA[Stories by Juan Navas on Medium]]></description>
        <link>https://medium.com/@juannavas7?source=rss-8c7e40dc9afc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*JuZqF5faZtc1k4-CV1Gm2Q.jpeg</url>
            <title>Stories by Juan Navas on Medium</title>
            <link>https://medium.com/@juannavas7?source=rss-8c7e40dc9afc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 01:22:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@juannavas7/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to make an animated Circle Progress View]]></title>
            <link>https://juannavas7.medium.com/how-to-make-an-animated-circle-progress-view-48fa2adb1501?source=rss-8c7e40dc9afc------2</link>
            <guid isPermaLink="false">https://medium.com/p/48fa2adb1501</guid>
            <category><![CDATA[uibezierpath]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[cabasicanimation]]></category>
            <category><![CDATA[cashapelayer]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Juan Navas]]></dc:creator>
            <pubDate>Sun, 14 Feb 2021 20:11:09 GMT</pubDate>
            <atom:updated>2021-02-15T11:18:58.941Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://developer.apple.com/documentation/quartzcore/cashapelayer"><strong>CAShapeLayer</strong></a> and <a href="https://developer.apple.com/documentation/uikit/uibezierpath/1624358-init"><strong>UIBezierPath</strong></a> are going to be your best buddies to draw a circle.</p><p>You can combine them like we do in the following snippet in order to get a arc drawn, if you add the created layer to a <strong>UIView</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/812c8a01cea5a5b5d0a4154f1b92d822/href">https://medium.com/media/812c8a01cea5a5b5d0a4154f1b92d822/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QukWIJsbcwyZXo6FGlQDug.png" /><figcaption>Reference cicle for UIBezierPath - <strong>Clockwise</strong>: true - <strong>Clockwise</strong>: false</figcaption></figure><p>If you initialize a UIBezierPath with a startAngle of <strong>0 radians</strong>, the starting point will be as in the figure 1, this would be like the <strong>East</strong> cardinal point in a circle.</p><p>And the arc drawn would be like the one in the middle, with it starting point in 0 radians (East) and end point in Pi/2 radians (South). If you set <strong>clockwise</strong> to <strong>false</strong>, then the arc would be drawn counterclockwise, just like in the right image.</p><p>So if you want like me to start in the <strong>North</strong> position, you may want to use a CGFloat extension such as the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f02d8350771b49b70c441720f5f4cfbe/href">https://medium.com/media/f02d8350771b49b70c441720f5f4cfbe/href</a></iframe><p>Now instead, of using 0 radians and Pi/2 as start and end points, you can use the extension to calculate the endAngle, base on the progress of the circle that you want to fill.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/259/1*D3zI7T_eKg34YFLjj5TjuQ.png" /></figure><p>We could add another layer, with another bezier path, and a progress of 1, to draw a full gray circle, for a better looking progress circle. Furthermore, using layer.lineCap = .round the start and end points of the arc would have a nice rounded caps for the drawn arc.</p><p>So now that we know how to draw a circle, let’s add an <strong>animation</strong> to the progress layer.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d43afa5fd2d2eb3298a195905065768/href">https://medium.com/media/7d43afa5fd2d2eb3298a195905065768/href</a></iframe><pre>let animation = CABasicAnimation(keyPath: &quot;strokeEnd&quot;)</pre><p>With this keyPath we’re telling the animation the layer’s property we will be animating.</p><pre>animation.beginTime = CACurrentMediaTime() + delay    animation.duration = duration</pre><p>beginTime allows to set a firing date for the animation, so you can add a delay, while duration sets the duration in seconds for the animation.</p><pre>animation.fromValue = 0    <br>animation.toValue = 1</pre><p>With these two properties we’re telling from which towhich value we want thestrokeEnd property to be animated. Since we told the arc we would have a full angle of 90º (Pi/2 radians), 0 would be 0º and 1 would be 90º. In other words, fromValue and toValue are expressed as a percentage of the value to be animated.</p><pre>animation.timingFunction = CAMediaTimingFunction(name: .easeIn)    animation.fillMode = .forwards    <br>animation.isRemovedOnCompletion = false</pre><p>With timingFunction we can specify the <strong>animation curve</strong>, while fillMode determines <em>how the timed object behaves once its active duration has completed</em>. With .fillMode = .forwards <em>the receiver remains visible in its final state when the animation is completed</em>. That combined with .isRemovedOnCompletion allows that the orange arc remains visible at the end of the animation.</p><p>There are other ways to make the object remain visible after the animation, but this one serves our needs and it’s easy enough. But you could use var delegate: CAAnimationDelegate? and conform to that delegate to use the method: optional func animationDidStop(_ anim: CAAnimation, finished flag: Bool) to set the strokeEndto its final value there.</p><pre>layer.removeAnimation(forKey: &quot;circleAnimation&quot;)<br>layer.strokeEnd = 0<br>layer.add(animation, forKey: &quot;circleAnimation&quot;)</pre><p>Finally we add the animation using the key circleAnimation, this is just how we want to name our animation. It can be set to nil and it will continue to work, with one caveat; if in this same layer we wanted to play the animation again, if the key was set to nil the new animation wouldn’t be added. That’s why we are making sure the strokeEnd is set to its initial value, and we remove any previous animation with that same key so that it can be added again, and it will just animate beautifully as many times as needed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/317/1*L2WuIMHcaZLvL1wHWVyQkg.gif" /></figure><p>If we don’t remove the animations for the specified key, changes to that property will be ignored because the animation is controlling that property. That’s why we are removing it prior to setting the strokeEnd back again to its initial value.</p><p>I hope you find this article useful and you’ve learned something as I did along the way 😊.</p><p>You can find the full code (for a playground) <a href="https://gist.github.com/jdev7/40b1416d70766f7b38b92d3c9eae985c">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=48fa2adb1501" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exploring how to use reduce in Swift]]></title>
            <link>https://juannavas7.medium.com/exploring-reduce-in-swift-ead6b707b455?source=rss-8c7e40dc9afc------2</link>
            <guid isPermaLink="false">https://medium.com/p/ead6b707b455</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[swift-4]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Juan Navas]]></dc:creator>
            <pubDate>Fri, 03 Nov 2017 17:12:35 GMT</pubDate>
            <atom:updated>2020-11-26T08:19:59.103Z</atom:updated>
            <content:encoded><![CDATA[<p>Every time I’ve tried to learn how to use reduce, I have usually found the same examples.</p><p>The one where it’s used to combine numeric elements of an array</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8d853fdb23769700ab93ab47e14a43a/href">https://medium.com/media/e8d853fdb23769700ab93ab47e14a43a/href</a></iframe><p>Likewise, we can combine different strings into a single one</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3595cf53b647c170354887295bebab9f/href">https://medium.com/media/3595cf53b647c170354887295bebab9f/href</a></iframe><p>Basically <strong>reduce </strong>iterates over a collection, taking 2 arguments, an <em>initial value </em>and a <em>closure</em> which applies a <em>transformation</em> to that value.</p><p>This closure provides two parameters, the <em>partial result</em> obtained in each iteration, and the <em>next element</em> of the collection.</p><p>Since functions in swift are closures, we can pass on the function as a closure as the second argument. We’ll see this in action in an example below.</p><p>Besides, due to Swift’s flexibility writing closures, we can see different syntax usages with this function (and others). <em>Take a look at the different uses that are shown in the code examples!</em></p><p><strong>Exploring different uses 🤔</strong></p><p>There are other interesting usages for reduce.</p><p>For instance, if we have an array of (boolean) conditions, we could reduce them like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa5dbbdc39e443d2972609303eacd6a0/href">https://medium.com/media/fa5dbbdc39e443d2972609303eacd6a0/href</a></iframe><p>This opens up interesting possibilities. Imagine we have a struct <strong>Person</strong> and we have an array of this type. We could use reduce to iterate through all these persons, to know if they’re all adults, for example.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aafe3b757de5115e10e2547b576caa14/href">https://medium.com/media/aafe3b757de5115e10e2547b576caa14/href</a></iframe><p>But if you wanted to generate <strong>strings of random alphanumeric characters</strong>, like passwords, you could use reduce like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/20e9ff1dc9efd48592dfbad8a04a24ba/href">https://medium.com/media/20e9ff1dc9efd48592dfbad8a04a24ba/href</a></iframe><p>As stated above, <em>functions are closures</em>, so we could extract the closure to a function, and then pass the function name as parameter to <strong>reduce</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0ef574bc1102aa537e56bc158f017d1/href">https://medium.com/media/d0ef574bc1102aa537e56bc158f017d1/href</a></iframe><p><strong>Swift 4: Improving performance 🤓</strong></p><p>Since Xcode 9.0, Apple added the <a href="https://developer.apple.com/documentation/swift/array/2924692-reduce"><em>reduce(into:_:)</em></a><em> </em>function, which passes the <em>accumulatingResult</em> by <strong>reference</strong> instead of value, and since it’s not copying all the elements on every iteration but passing the same object around, it gives a better performance. This is Apple’s recommendation:</p><blockquote>This method is preferred over reduce(_:_:) for efficiency when the result is a copy-on-write type, for example an Array or a Dictionary.</blockquote><p>So we could use the last example with this function like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6db62a80b2ee5d1ab663a52bb8ed61c7/href">https://medium.com/media/6db62a80b2ee5d1ab663a52bb8ed61c7/href</a></iframe><p>We can now iterate for example 1000 times, to see the improvement using one method over the other one:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97513980d8301b54e87ca5bc838ca766/href">https://medium.com/media/97513980d8301b54e87ca5bc838ca766/href</a></iframe><p>Using <a href="https://developer.apple.com/documentation/swift/array/2924692-reduce"><em>reduce(into:_:)</em></a><em> </em>1000 times we can sense a gain of <strong>0.14 seconds</strong>.</p><p>This is just copying around simple strings. If instead we were copying an array of more complex structs, I guess that the gain would be more significant.</p><p>Please feel free to share your thoughts, the article or comments. Any feedback will be appreciated! 😃</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ead6b707b455" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>