<?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 Duarte Lopes on Medium]]></title>
        <description><![CDATA[Stories by Duarte Lopes on Medium]]></description>
        <link>https://medium.com/@duarte.lopes85?source=rss-1ff2f6e51407------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*wnsgn_HNXY0yxokoIz1l7Q.png</url>
            <title>Stories by Duarte Lopes on Medium</title>
            <link>https://medium.com/@duarte.lopes85?source=rss-1ff2f6e51407------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 03:59:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@duarte.lopes85/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[Rita Hayworth and UIViewPropertyAnimator]]></title>
            <link>https://medium.com/@duarte.lopes85/uiviewpropertyanimator-f1b8d7315948?source=rss-1ff2f6e51407------2</link>
            <guid isPermaLink="false">https://medium.com/p/f1b8d7315948</guid>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[animation]]></category>
            <dc:creator><![CDATA[Duarte Lopes]]></dc:creator>
            <pubDate>Mon, 25 Jun 2018 00:38:28 GMT</pubDate>
            <atom:updated>2020-01-10T15:59:46.303Z</atom:updated>
            <content:encoded><![CDATA[<p>Easy peasy japanesey!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*cw2MDFhvBUSdntTtIs1kYQ.jpeg" /><figcaption>Get busy readin’…</figcaption></figure><p><strong>Enter UIViewPropertyAnimator,</strong> what is it and what is this for? This new class was introduced in iOS 10, and it allows animations to views and dynamic changes to those animations. UIViewPropertyAnimator came to replace the UIView.animate static functions, with more functionalities done in an easier way! One can now keep a reference to it which gives us the power to change properties after the animation starts running, like pausing an animation or scrubbing through it with a slider. UIViewPropertyAnimator provides a lot more control than its predecessor.</p><h4>Properties and Initialisers</h4><p>First thing to understand is what properties of the UIView are animatable. And these are:</p><ul><li>frame</li><li>bounds</li><li>center</li><li>transform (only transformations for 2D)</li><li>alpha</li><li>backgroundColor</li></ul><p><strong>Let’s see a quick example:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d174949a7634a02166dd88825d67d87a/href">https://medium.com/media/d174949a7634a02166dd88825d67d87a/href</a></iframe><p>The animation block changes a view’s background color to red. The duration is set to 10 seconds and the animation curve is linear (more on this later…)</p><blockquote>Animations are a set of property changes and time. That’s all it takes, really. Property changes… and time.</blockquote><p>And this is how we can add a completion block, that executes after the animation ends.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01feb7a0105971513ce46652bee302ae/href">https://medium.com/media/01feb7a0105971513ce46652bee302ae/href</a></iframe><p>Only one thing left, we need to run the animation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98a39de0ad323b7a07e486635a163979/href">https://medium.com/media/98a39de0ad323b7a07e486635a163979/href</a></iframe><p><strong>There are 4 initialisers:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fcfedbb18dd96828889154a05122d970/href">https://medium.com/media/fcfedbb18dd96828889154a05122d970/href</a></iframe><p>The duration, in seconds, is the time the animation lasts. The curve argument is an enumeration defining a built-in timing curve, with cases: .easeInOut, .easeIn, .easeOut and linear. The cases with the <em>“ease”</em> word means a slower part of the animation, e.g., easeIn will be slow at the beginning, and fast/abrupt at the end. Linear runs all the animation at the same speed. The animation block argument contains the code that modifies the properties of the view(s).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/376/1*GIsFRof0fZXxzjOy0NyRMw.gif" /><figcaption>Changing background color animation <a href="https://www.dropbox.com/sh/kt37jg6pparbewh/AABV8RjE4tQ8rpqHWqZ-6gfMa?dl=0">Download playground here</a></figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/81a42ad50e7336a6885dd4ad6d6678a9/href">https://medium.com/media/81a42ad50e7336a6885dd4ad6d6678a9/href</a></iframe><p>The parameters point1 and point2 initialises the animation with a custom cubic Bézier timing curve.</p><blockquote>Custom cubic Bézier timing curve? How could you be so obtuse?</blockquote><p>Do not worry, you can experiment and get the point values of your custom curve using this site: <a href="http://www.cubic-bezier.com/">www.cubic-bezier.com</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*XEyn9FpF1NUyIOpBfSa0Rw.gif" /><figcaption>Animation using two control points <a href="https://www.dropbox.com/sh/60pb4p0s9ky2ul4/AABoy-Ln6j3i0517ADyYE43la?dl=0">Download playground here</a></figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d32f0eb091a5c589a63fe2dd1d0387cb/href">https://medium.com/media/d32f0eb091a5c589a63fe2dd1d0387cb/href</a></iframe><p>Damping ratio to apply to the initial acceleration and oscillation. Values closer to 0 means less damping and more oscillation. Value 1 means a smooth deceleration and no oscillation. Change these values to see the difference</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*pEy_9hr0GBwQjE2fm9-gmg.gif" /><figcaption>Animation with damping ratio of 0.5 <a href="https://www.dropbox.com/sh/licw47ql5siimg2/AAB6KUWOlA1-STQ9LCrF-mrQa?dl=0">Download playground here</a></figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad80ffccbd1a6f7b12dec118583bfd3f/href">https://medium.com/media/ad80ffccbd1a6f7b12dec118583bfd3f/href</a></iframe><p>Initialises with a custom timing curve object. <a href="https://developer.apple.com/documentation/uikit/uitimingcurveprovider">UITimingCurveProvider</a> is a protocol. It can be specified a built-in UIKit curves (easeInOut, easeIn, easeOut, linear) or a cubic Bézier curve using class <a href="https://developer.apple.com/documentation/uikit/uicubictimingparameters">UICubicTimingParameters</a>. For a spring based function use class UISprintTimingParameters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*8X3LxcnuHAmJPKyioxbdMw.gif" /><figcaption>Example of animation using UISpringTimingParameters <a href="https://www.dropbox.com/sh/0trs2rju2bz7xr1/AACFhZCaw2MxNXtvp16X8DcQa?dl=0">Download playground here</a></figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4eac2f44d4cdd65ac2f15ac0dcd99a1b/href">https://medium.com/media/4eac2f44d4cdd65ac2f15ac0dcd99a1b/href</a></iframe><p>This is a static function, that creates and returns an animator object and which begins running its animations immediately. <a href="https://developer.apple.com/documentation/uikit/uiview/animationoptions">UIViewAnimationOptions</a> type include options like allowUserInteraction, repeat, autoreverse, and many others. <a href="https://developer.apple.com/documentation/uikit/uiviewanimatingposition">UIViewAnimatingPosition</a> refers to an enumeration indicating positions within the animation (cases: .end, .start and .current). More on this enumeration later.</p><h4>UIViewPropertyAnimator Object States</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/746/1*JsXic6TJSvRaoqGQv1R7CA.png" /></figure><p>UIViewPropertyAnimator has 3 states. Inactive, active and stopped. The initial state starts in the inactive state. It’s also the state it returns to when an animation finishes.</p><blockquote><strong>Note:</strong> Changes on the animations block can only be made while the animation is inactive, in another words, an animator can never return to the active state without being reconfigured and made inactive first.</blockquote><p>The animator object changes to the active state when startAnimation() or pauseAnimation() are called. With startAnimation() the animation starts running until the end of the animation where it goes to the stopped state and after that, to the inactive state. PauseAnimation() places the animator to the active state but makes the animation idle. One example of this function use is on scrubbing of the animation by modifying the <a href="https://developer.apple.com/documentation/uikit/uiviewanimating/1649787-fractioncomplete">fractionComplete</a> property. To resume the animation simply call startAnimation().</p><p>It’s possible to also stop the animation by calling <a href="https://developer.apple.com/documentation/uikit/uiviewanimating/1649750-stopanimation">stopAnimation(_ withoutFinishing: Bool)</a>. The stopAnimation ends the animations at their current positions.</p><ul><li>If withoutFinishing parameter is set to true it clears any animations and moves the animator to the inactive state directly (without performing any final actions).</li><li>If false, the animator goes to the stopped state. Once at the stopped state, one can call <a href="https://developer.apple.com/documentation/uikit/uiviewanimating/1649796-finishanimation">finishAnimation</a>(at finalPosition: <a href="https://developer.apple.com/documentation/uikit/uiviewanimatingposition">UIViewAnimatingPosition</a>), which subsequently will execute the animator completion block (but only if you specified false for the withoutFinishingParameter). It’s a programmer error to call this method at any time except after a call to stopAnimation(_:)</li></ul><p>Now, that’s when we can use those cases .start, .current and .end used in the completion block of an animation. The value of finalPosition parameter in the finishAnimation(at finalPosition: UIViewAnimatingPosition) will set the position where you want the animation to be finished. Check the example below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a9576a9f4c2fd5b23d09277110e7acaf/href">https://medium.com/media/a9576a9f4c2fd5b23d09277110e7acaf/href</a></iframe><p>Imagine we have a tap action where we can pause and resume the animation and a double tap action where the animation is stop and finish as shown below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8384ca53340d2a9569dc82cbb54d8cc/href">https://medium.com/media/e8384ca53340d2a9569dc82cbb54d8cc/href</a></iframe><p>The result of double tap will finish the animation in the start position and also the background color of the circle is changed to black.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*zFQFcGWUolb-fPKx_4elMA.gif" /><figcaption>First one tap and then double tap actions <a href="https://www.dropbox.com/sh/xvk13wx3919w2hq/AAAgztECrIBnqu6Zmoaqm9zua?dl=0">Download playground here</a></figcaption></figure><h4>Scrubbing</h4><p><em>Gilda, are you decent?</em></p><p>UIViewPropertyAnimator makes it so easy to apply scrubbing animations, it’s almost indecent! In this example there is a slider which controls the animation position.</p><p>The important part here is to set the property fractionComplete from the animator to the progress of the slider</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8324e1cdd23b1ae57a88720bcfe04c74/href">https://medium.com/media/8324e1cdd23b1ae57a88720bcfe04c74/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*Ud9raNXjiyiJejIiOl7Rpw.gif" /><figcaption>Scrubbing animation. <a href="https://www.dropbox.com/sh/icp4m7ha6slrncf/AAD8vNvfqmy82RfNRk-gpd0Na?dl=0">Download playground here</a></figcaption></figure><blockquote><strong>Note: </strong>In this case we shouldn’t call startAnimation. We want the animation to be in the inactive state so we can slide on the progress of the slide. If one wants the animation to start and then use the scrubbing somewhere in the middle of the animation then startAnimation makes sense and one can call pauseAnimation to go back to the inactive state.</blockquote><blockquote>I know that you liked the UIView.animate but I’m telling you, these UIViewPropertyAnimator functions are funny. First you hate them. Then you get used to them. Enough time passes, it gets so you depend on them. You just got institutionalized!</blockquote><p>And this is it! I hope this helped you to better understand the UIViewPropertyAnimator class. Enjoy :) and see you at Zihuatanejo!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f1b8d7315948" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>