<?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 Perry Waxman on Medium]]></title>
        <description><![CDATA[Stories by Perry Waxman on Medium]]></description>
        <link>https://medium.com/@werry_paxman?source=rss-36fa50ba8d61------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*4p4qXlFTnZzG4YmRGuSadQ.jpeg</url>
            <title>Stories by Perry Waxman on Medium</title>
            <link>https://medium.com/@werry_paxman?source=rss-36fa50ba8d61------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 05:21:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@werry_paxman/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[Bring your UITabBar to Life: Animating UITabBarItem Images with Swift and CoreGraphics]]></title>
            <link>https://medium.com/@werry_paxman/bring-your-uitabbar-to-life-animating-uitabbaritem-images-with-swift-and-coregraphics-d3be75eb8d4d?source=rss-36fa50ba8d61------2</link>
            <guid isPermaLink="false">https://medium.com/p/d3be75eb8d4d</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Perry Waxman]]></dc:creator>
            <pubDate>Fri, 23 Oct 2015 22:25:41 GMT</pubDate>
            <atom:updated>2015-12-11T04:59:27.051Z</atom:updated>
            <content:encoded><![CDATA[<p>For my latest project, I wanted to add some flair to my app’s <em>UITabBar</em> with some unique animation. I simply needed to animate the image which resides inside my <em>UITabBar</em>’s items! But wait — that’s a lot easier said than done. Here’s why: <em>UITabBarItem </em>isn’t a subclass of <em>UIView</em>, and <em>UITabBarItem</em> doesn’t have a publicly accessible <em>UIImageView</em> property. <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITabBarItem_Class/index.html#//apple_ref/occ/cl/UITabBarItem">See the <em>UITabBarItem</em> documentation for yourself</a>. This presents a challenge to us because we can’t directly iterate through subviews of <em>UITabBarItem</em>s nor do we have a <em>UIImageView</em> to directly access. So how exactly <em>can</em> we animate a UITabBarItem’s image? Is there a <em>UIImageView</em> somewhere that we can access? Yep.</p><p>The following example will teach you how to animate a <em>UITabBarItem</em>’s <em>UIImageView</em>. Let’s get started with a new tabbed application Xcode project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dY1OJIK1BPcb3u1fpDrElA.png" /><figcaption>Selected Tabbed Application, then name the project whatever you’d like.</figcaption></figure><p>Go ahead and create a UITabBarController subclass. This subclass gives us the ability to manipulate our <em>UITabBar</em> and ulimately <em>UITabBarItem</em> images.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*J22kwneiQnLKAmxgQ4gvGA.png" /><figcaption>Save this then let’s jump into our subclass.</figcaption></figure><p>We’re going to need a <em>UIImageView</em> object to hold on to our desired <em>UITabBarItem</em>’s<em> </em>image view. At this point we need to select the subview of our <em>UITabBar</em> that correlates to our desired <em>UITabBarItem</em>. Set that image view we created to the last subview of our <em>UITabBarItem</em>. Finally, set the image view’s content mode to center. The content mode allows us to control how our image view will layout its content.</p><pre>import UIKit</pre><pre>class AnimatedTabBarController: UITabBarController {</pre><pre>var secondItemImageView: UIImageView!</pre><pre>override func viewDidLoad() {</pre><pre>        super.viewDidLoad()<br>      <br>     let secondItemView = self.tabBar.subviews[1]<br>     <br>     let secondItemImageView = self.tabBar.subviews[1]</pre><pre>    self.secondItemImageView = firstItemView.subviews.first as! UIImageView</pre><pre>    self.secondItemImageView.contentMode = .Center</pre><pre>    }</pre><pre>}</pre><p>Please note the <em>self.tabBar.subview[1]</em>. This means that we’re accessing the subview (UITabBarItem view) at index 1 of our UITabBar. You can change this to correlate to any UITabBarItem you want. For this example we’re just going to be manipulating the second UITabBarItem in the tab bar.</p><p>Now that we have our image we can start animating. Let’s add a tag to our desired UITabBarItem in our storyboard. Also be sure to connect your <em>UITabBarController</em> subclass to your <em>UITabBarController</em> in your storyboard.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/926/1*OlRwueB7henRUO81YfwsWw.png" /><figcaption>I set the tag of the second tab to 1.</figcaption></figure><p>Override the <em>tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) </em>delegate method of our <em>UITabBarController</em> subclass and add in the logic for our second tab’s tag. This function will be responsible for detecting which tab we select.</p><pre>override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {</pre><pre>    if item.tag == 1{</pre><pre>         //do our animations</pre><pre>    }</pre><pre>}</pre><p>Let’s make a simple rotation for our image. We’ll do this with CoreGraphics by creating a <em>CGAffineTransform</em>. Let’s set our our image view’s transform property to <em>CGAffineTransformIdentity</em>. This will enable our image view to animate each time we decide to change its transform later on. Write this inside our <em>if</em> statement.</p><pre>self.secondItemImageView.transform = CGAffineTransformIdentity</pre><p>Let’s do some animation. I’m going to use a simple spring animation for this example. Inside the animation closure, let’s set our image view’s transform to rotate 360°.</p><pre>UIView.animateWithDuration(0.7, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: .CurveEaseInOut, animations: { () -&gt; Void in</pre><pre>    let rotation = CGAffineTransformMakeRotation(CGFloat(M_PI_2))</pre><pre>    self.secondItemImageView.transform = rotation</pre><pre>}, completion: nil)</pre><p>Once you have all this code written, you’re UITabBarController subclass should look something like this:</p><pre>import UIKit</pre><pre>class AnimatedTabBarController: UITabBarController {</pre><pre>    var secondItemImageView: UIImageView!</pre><pre>    override func viewDidLoad() {</pre><pre>        super.viewDidLoad()<br>      <br>        let secondItemView = self.tabBar.subviews[1]<br>     <br>        let secondItemImageView = self.tabBar.subviews[1]</pre><pre>        self.secondItemImageView = firstItemView.subviews.first as! UIImageView</pre><pre>        self.secondItemImageView.contentMode = .Center</pre><pre>     }</pre><pre>}</pre><pre>    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {</pre><pre>        if item.tag == 1{</pre><pre>        //do our animations</pre><pre>            self.secondItemImageView.transform = CGAffineTransformIdentity</pre><pre>            UIView.animateWithDuration(0.7, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: .CurveEaseInOut, animations: { () -&gt; Void in</pre><pre>                let rotation = CGAffineTransformMakeRotation(CGFloat(M_PI_2))</pre><pre>                self.secondItemImageView.transform = rotation</pre><pre>            }, completion: nil)</pre><pre>        }</pre><pre>    }</pre><pre>}</pre><p>Let’s run our project and see the results.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fzippy.gfycat.com%2FVengefulAmpleAnhinga.mp4&amp;src_secure=1&amp;url=http%3A%2F%2Fgfycat.com%2FVengefulAmpleAnhinga&amp;image=https%3A%2F%2Fthumbs.gfycat.com%2FVengefulAmpleAnhinga-size_restricted.gif&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=video%2Fmp4&amp;schema=gfycat" width="804" height="1448" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d97283e20c5db0149b576258f36aaec/href">https://medium.com/media/1d97283e20c5db0149b576258f36aaec/href</a></iframe><p>Thanks for taking the time to check out example; I hope you learned something! Feel free to examine the source code at <a href="https://github.com/pwax/AnimatedTabBarItemImage">https://github.com/pwax/AnimatedTabBarItemImage</a>. If you want to learn more about me, please go to my site at <a href="http://perrywaxman.com/">http://perrywaxman.com</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d3be75eb8d4d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>