<?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 Quang Quoc Tran on Medium]]></title>
        <description><![CDATA[Stories by Quang Quoc Tran on Medium]]></description>
        <link>https://medium.com/@quangtqag?source=rss-f0724b4008e3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*gLhC7buyasCbQRm2.jpg</url>
            <title>Stories by Quang Quoc Tran on Medium</title>
            <link>https://medium.com/@quangtqag?source=rss-f0724b4008e3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 10:47:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@quangtqag/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[Video Chat using WebRTC and Firestore]]></title>
            <link>https://medium.com/@quangtqag/video-chat-using-webrtc-and-firestore-a925de6f89f4?source=rss-f0724b4008e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/a925de6f89f4</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[webrtc]]></category>
            <category><![CDATA[firestore]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[realtime]]></category>
            <dc:creator><![CDATA[Quang Quoc Tran]]></dc:creator>
            <pubDate>Sat, 11 May 2019 03:52:27 GMT</pubDate>
            <atom:updated>2020-11-24T00:07:11.794Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*tMaGKCldwsOIYIXZl7FLUA.png" /></figure><p>In this article, you will be explained how a video chat app operate that using WebRTC and realtime database Firestore.</p><p>If you have not known about WebRTC yet then this is a sum up from <a href="https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API">Mozilla Developers Network</a>:</p><blockquote><strong>WebRTC</strong> (Web Real-Time Communications) is a technology which enables Web applications and sites to capture and optionally stream audio and/or video media, as well as to exchange arbitrary data between browsers without requiring an intermediary. The set of standards that comprise WebRTC makes it possible to share data and perform teleconferencing peer-to-peer, without requiring that the user installs plug-ins or any other third-party software.</blockquote><p>WebRTC have some terms that you need to know such as <strong>ICE, TURN, STUN, NAT, SDP, PeerConnection, ICECandidate, Signal Channel, Offer SDP, Answer SDP, DataChannel</strong>…Pls <a href="https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Protocols"><strong>read this document</strong></a> and <a href="https://hacks.mozilla.org/2013/07/webrtc-and-the-early-api/"><strong>this document</strong></a> to have a look about them.</p><p>Now you need to know the flow to connect two devices together. Assume device A want to transfer media to device B and vice versa then what two devices need to do?</p><h3>The flow</h3><ol><li>Two devices create a peer connection with configurations about STUN servers, TURN servers, input sources such as input audio, input video.</li><li>Device A and device B send a request to STUN server to get its public IP address.</li><li>Device A send offer SDP and ICE candidates to device B thru signaling server.</li><li>Device B also send answer SDP and ICE candidates to device A thru signaling server.</li><li>At this time, two devices could talk together thru a peer connection.</li></ol><p>It is enough for theory, now you can download my project and test it.</p><blockquote>Note: this project mainly based on <a href="https://github.com/stasel/WebRTC-iOS">WebRTC-iOS project of statel</a>, but I edited it to use Firestore instead of nodejs server and added a feature for reset peer connection, so you can start over again that no need to restart the app.</blockquote><h3>Steps to run the project</h3><ol><li>Clone the project at: <a href="https://github.com/quangtqag/ChatVideo">https://github.com/quangtqag/ChatVideo</a></li><li>Create an app on Firebase Console and config type of database is Firestore and test mode. If you have not known yet about Firestore then I recommend you experiment with <a href="https://codelabs.developers.google.com/codelabs/firestore-ios/#0">this codelab</a>.</li><li>Download <strong>GoogleService-Info.plist</strong> and import it into the project.</li><li>Run the app on two devices.</li></ol><blockquote>Note: you can test this app using a device and a simulator, but video feature will not operate on simulator. So if you have two devices then you are good to go.</blockquote><p>After you run the app you will see the screen as folllow:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/984/1*8A4kVzld0zebl56T2rLvHQ.png" /></figure><h3>Steps to test</h3><ol><li>At first, you need to choose you are <strong>Tom</strong> or <strong>Jerry</strong>, if device A choose Tom then device B you need to choose Jerry</li><li>Device A press <strong>Send Offer</strong> button to send offer SDP to device B.</li><li>Device B press <strong>Send Answer</strong> button to send answer SDP to device A.</li><li>After that you will see WebRTC status changed into connected.</li><li>You try to say something on device A and that voice will be emitted on device B. That’s great :]</li><li>To see video, both devices just press <strong>Video</strong> button. That’s so cool. Press back button to come back.</li><li>Press End Call button to close peer connection and create a new one.</li><li>You can try it again by go to step 2.</li></ol><h3>Some code need to know</h3><p>Here I will specify some important codes that you need to know</p><h4>Create and config peer connection</h4><p>To create a peer connection you need to pass a configuration that contains STUN and TURN servers and some other infos.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9cae9fe9c876f3649f10c4c0faf79987/href">https://medium.com/media/9cae9fe9c876f3649f10c4c0faf79987/href</a></iframe><p>After that you create audio track, video track and add them into peer connection as follow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6e0ba1ea30e0d42b25e211db6eb76ae/href">https://medium.com/media/b6e0ba1ea30e0d42b25e211db6eb76ae/href</a></iframe><h4>Create local offer SDP and send it to the other person thru Firestore</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6aa8ec025b4c7765d4aac0720a444df8/href">https://medium.com/media/6aa8ec025b4c7765d4aac0720a444df8/href</a></iframe><h4>Send local candidates to the other person thru Firestore</h4><p>After that peer connection auto generate local candidates. You also need to send them to the other person.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d872ce4b7c1f2cd4e2b5a03982d97297/href">https://medium.com/media/d872ce4b7c1f2cd4e2b5a03982d97297/href</a></iframe><h4>Listen remote SDP and Candidates</h4><p>Device B listen SDP and Candidate data and add it into the peer connection</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e9a851779fec00034b3c4283ee88d9d/href">https://medium.com/media/8e9a851779fec00034b3c4283ee88d9d/href</a></iframe><h4>Send answer SDP to device A</h4><p>Device B create answer SDP and send it along with Candidates to device A thru Firestore</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2a17ba1af311f86558593bbc9a047a98/href">https://medium.com/media/2a17ba1af311f86558593bbc9a047a98/href</a></iframe><h4>Close peer connection</h4><p>Close peer connection, clear Firestore, reset variables and re-create new peer connection, so it ready for new session.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f12fc9af9aeb8f8e3bcff46f12bc4b7/href">https://medium.com/media/1f12fc9af9aeb8f8e3bcff46f12bc4b7/href</a></iframe><p>That’s all about how video chap apps operate.</p><p>Additional, you can research more about PushKit framework, CallKit framework to create robust video chat apps as Facebook Messenger, Whatsapp, Skype…</p><h3>Please Support Me!</h3><p>If this post is useful, please support me by trying my free <a href="https://apps.apple.com/us/app/animal-5d/id1467725537">Unimal</a> app. Thanks for your support!</p><figure><a href="https://apps.apple.com/us/app/animal-5d/id1467725537"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jHcrUuPtag4KlcfDWOnGaA.jpeg" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a925de6f89f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Table View Infinite Scrolling using Firestore Paging]]></title>
            <link>https://medium.com/@quangtqag/table-view-infinite-scrolling-using-firestore-paging-1aa9755e063c?source=rss-f0724b4008e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/1aa9755e063c</guid>
            <category><![CDATA[pagination]]></category>
            <category><![CDATA[infinite-scrolling]]></category>
            <category><![CDATA[tableviews]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[firestore]]></category>
            <dc:creator><![CDATA[Quang Quoc Tran]]></dc:creator>
            <pubDate>Wed, 08 May 2019 14:08:40 GMT</pubDate>
            <atom:updated>2020-11-24T00:09:49.253Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*twN1YsyjwHlCX1WwV3lASA.png" /></figure><p>In this tutorial, you will create an app that allows you scroll user list infinitely and smoothly. Specially, you will use Firestore as your back-end with paging feature that you will not need to write any server lines of code. That’s great!</p><p>At the end of this tutor you will make an app as follow:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/281/1*ohcZewQnJzPmy8FSA0BGnw.gif" /></figure><h3>Some Knowledge</h3><p>At first, you need to know something:</p><ul><li>Firestore is an improver compared to its former Realtime Database. If you want to know the difference between Firestore and Realtime Database then you can read <a href="https://medium.com/datadriveninvestor/realtime-database-vs-cloud-firestore-which-database-is-suitable-for-your-mobile-app-87e11b56f50f">this article</a>. If you want to familiar with Firestore then you can do <a href="https://codelabs.developers.google.com/codelabs/firestore-ios/#0">this codelab</a>.</li><li>Firestore now allows you get a batch of documents that you defined in queries, and get next batch by using cursors. <a href="https://firebase.google.com/docs/firestore/query-data/query-cursors">Read more here</a>.</li></ul><h3>Getting Started</h3><p>Do these steps to begin your starter project:</p><ol><li><a href="https://github.com/quangtqag/Tutorials/raw/master/UsersInfiniteScrolling/starter.zip">Download starter project</a>. Open it and run pod install.</li><li>Create an app on Firebase Console, download <strong>GoogleService-Info.plist</strong> and import into the project.</li><li>Select Database is Cloud Firestore and edit security rules as figure:</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_rwWJCbxIpetWAO7RTVoDA.png" /><figcaption>With this security rules, it allows you to write data into Firestore that no need to authenticate . It just for test. In production mode you need to update it.</figcaption></figure><p>4. Run the app.</p><p>After run the app, you will see this screen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*gy2ojFZNrpm1Qkh8DyH4jQ.png" /></figure><p>On the navigation bar you will see 2 buttons. <strong>Populate</strong> button is used to insert fake data into Firestore. <strong>Fetch</strong> button is used to fetch data from Firestore.</p><h3>Insert User List into Firestore</h3><p>At first, you need to insert fake data into Firestore. You not only insert user list info but also user count info. Why you need to save user count info. B/c Firestore doesn’t have any function that allows us get ducument count inside a collection. So we need to save this info and update it every time you insert or delete user documents.</p><p>Open <strong>UserTableViewController.swift</strong> and add the following block of code into.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1fca7dfa83f8f74cb941799fa353c4c7/href">https://medium.com/media/1fca7dfa83f8f74cb941799fa353c4c7/href</a></iframe><p>This method is used to create <strong>counters</strong> collection, <strong>users</strong> document and <strong>count</strong> property with value of zero if it is not exist. This property is used to contain user count info that you inserted into database.</p><p>Next, we will push user list info into database and update users counter. Add the following block of code into:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/efeb62e6e9c4626d96a4b76b6641ec7d/href">https://medium.com/media/efeb62e6e9c4626d96a4b76b6641ec7d/href</a></iframe><p>In the above method you use transaction b/c you have read and write operations. If you only have write operations then you can use batch. <a href="https://firebase.google.com/docs/firestore/manage-data/transactions#batched-writes">Read more about them here</a>.</p><p>Final, conbine the above methods into <strong>populate</strong> method as follow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a391fe02326fca282c14803fd0c5dcbf/href">https://medium.com/media/a391fe02326fca282c14803fd0c5dcbf/href</a></iframe><p>Now, you can run the app, tap <strong>Populate</strong> button and waiting for some seconds you will see this message <strong>“Transaction successfully committed!”</strong> inside console. Refresh Firestore to see the changes.</p><h3>Fetch data from Firestore</h3><p>At first, you need to get user count as follow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/852df8737861b87430f6b310538ebd5e/href">https://medium.com/media/852df8737861b87430f6b310538ebd5e/href</a></iframe><p>After you had user count then you can get user list info as follow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/29fb15d9ed6935400470e8bfc0597951/href">https://medium.com/media/29fb15d9ed6935400470e8bfc0597951/href</a></iframe><p>Next, you will combine the above methods into <strong>fetch</strong> method as follow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99647d0b826c0f64f93db56000efefd6/href">https://medium.com/media/99647d0b826c0f64f93db56000efefd6/href</a></iframe><p>Now, you can run the project, tap <strong>Fetch</strong> button and user list will be shown up in table view.</p><p>But when you scroll down then data don’t continue to show up. We will fix it. Add the following block of code into:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/26f3a6c5f5ee9cd78794e2b9fa53bce9/href">https://medium.com/media/26f3a6c5f5ee9cd78794e2b9fa53bce9/href</a></iframe><p>Since iOS 10, Apple introduced to us the method <strong>tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath])</strong> that notify to us which rows will be shown up early. This is the chance for you to fetch data from database that time-consuming.</p><p>That is it. Now run the app and enjoy.</p><p>You can download <a href="https://github.com/quangtqag/Tutorials/raw/master/UsersInfiniteScrolling/final.zip">the final project here</a>.</p><h3>Please Support Me!</h3><p>If this post is useful, please support me by trying my free <a href="https://apps.apple.com/us/app/animal-5d/id1467725537">Unimal</a> app. Thanks for your support!</p><figure><a href="https://apps.apple.com/us/app/animal-5d/id1467725537"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jHcrUuPtag4KlcfDWOnGaA.jpeg" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1aa9755e063c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Add Search Bar Quickly]]></title>
            <link>https://medium.com/@quangtqag/uisearchcontroller-ab315db5af7a?source=rss-f0724b4008e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab315db5af7a</guid>
            <category><![CDATA[search-controller]]></category>
            <category><![CDATA[searchbar]]></category>
            <category><![CDATA[uisearchcontroller]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[uisearchbar]]></category>
            <dc:creator><![CDATA[Quang Quoc Tran]]></dc:creator>
            <pubDate>Mon, 25 Feb 2019 07:25:42 GMT</pubDate>
            <atom:updated>2020-11-24T00:12:21.435Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fAOosYGYaa99AiGarhqSlg.png" /></figure><p>The fast way to add a search bar into your view controller is to use <a href="https://developer.apple.com/documentation/uikit/uisearchcontroller"><em>UISearchController</em></a>. <em>UISearchController</em> provide you a search bar and notify you when user enter something into the search bar.</p><p>In addition, <em>UISearchController</em> also responsible for show or dismiss the results view that you provide for it at the initialization.</p><p>To get started, you download <a href="https://github.com/quangtqag/Tutorials/raw/master/TableSearch/starter.zip">the starter project</a>. The starter project has already created for you a table view controller that show the flowers list and another table view controller that show filtered flower list when user enter search string into the search bar.</p><p>Run the starter project you will see the flower list, and you will not see any search bar at anywhere.</p><h3>Initialize Search Controller</h3><p>Now we will initialize search controller and add it into navigation item. Add the following block code into the end of <em>viewDidLoad</em> method in <em>MainTableViewController.swift</em> file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5cd73f5a70bfb5d6bf83e5834b1db4a9/href">https://medium.com/media/5cd73f5a70bfb5d6bf83e5834b1db4a9/href</a></iframe><h3>Update the Search Results</h3><p>When user enter something into the search bar then the search controller will notify you thru <em>updateSearchResults(for:)</em> method. From this method you can get the search string in the search bar, filter the flowers and update them on results table view.</p><p>Add the following block code into <em>MainTableViewController.swift</em> file and run the app.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6abbb58bcfdc88733755d6791594e8a1/href">https://medium.com/media/6abbb58bcfdc88733755d6791594e8a1/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/781/1*_HpJsAS8AnkeUm2p2A22aA.png" /></figure><p>Now you could see the search bar and can filter them. You can <a href="https://github.com/quangtqag/Tutorials/raw/master/TableSearch/final.zip">download the final source code project here</a>.</p><h3>Please Support Me!</h3><p>If this post is useful, please support me by trying my free <a href="https://apps.apple.com/us/app/animal-5d/id1467725537">Unimal</a> app. Thanks for your support.</p><figure><a href="https://apps.apple.com/us/app/animal-5d/id1467725537"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jHcrUuPtag4KlcfDWOnGaA.jpeg" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab315db5af7a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Arbitrary Drawing with Playground]]></title>
            <link>https://medium.com/@quangtqag/core-graphics-samples-7ee6dad7eb49?source=rss-f0724b4008e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ee6dad7eb49</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[graphic-context]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[arbitrary-drawing]]></category>
            <category><![CDATA[coregraphics]]></category>
            <dc:creator><![CDATA[Quang Quoc Tran]]></dc:creator>
            <pubDate>Sat, 23 Feb 2019 06:22:33 GMT</pubDate>
            <atom:updated>2020-03-06T07:07:58.342Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PbfMtZu90kdreBr2mH_KMA.jpeg" /></figure><blockquote>If this post is useful, please support me by trying my free <a href="https://apps.apple.com/us/app/animal-5d/id1467725537">Animal 5D</a> app.<a href="https://apps.apple.com/us/app/animal-5d/id1467725537"> Animal 5D</a> uses AR tech to teach children to know animals. This app helps children to have physical activities to create health and more interested in learning. <a href="https://apps.apple.com/us/app/animal-5d/id1467725537">Check It Out!</a></blockquote><p>To draw somethings on UIView, you need to override <em>draw</em> method of UIView and get current context.</p><p>I use Playground to test drawing commands quickly. So you need to create a new Playground too.</p><p>View the code of following samples, paste them into Playground and run it. Editing some parameters of the code to know how it works.</p><p>You can also <a href="https://github.com/quangtqag/Tutorials/raw/master/ArbitraryDrawing/ArbitraryDrawing.playground.zip">download Playground file here</a>.</p><h4>1. Fill background color</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56bc1392f84c1260f382c873f155739a/href">https://medium.com/media/56bc1392f84c1260f382c873f155739a/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/1*RRrpp7hXfNkpo6NbtiWqPg.png" /></figure><h3>2. Fill an Ellipse</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/160bfaaa7e13116d0550beae31b8234b/href">https://medium.com/media/160bfaaa7e13116d0550beae31b8234b/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/312/1*95B_zrhVbaheai0981gXDA.png" /></figure><h3>3. Fill a Path</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b592bf268303c7a9f8c4cc37ccb634b/href">https://medium.com/media/6b592bf268303c7a9f8c4cc37ccb634b/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/1*NGoDdhCeigW-ZZ25nxgFcw.png" /></figure><h3>4. Fill an Arc</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/03761da5de0ed0316c3229d2ea6f0328/href">https://medium.com/media/03761da5de0ed0316c3229d2ea6f0328/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/322/1*8C4DQkAbWCH7tpyew3_Exg.png" /></figure><h3>5. Stroke a Curve</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e217eda7b74d2ae930cc5881495c1904/href">https://medium.com/media/e217eda7b74d2ae930cc5881495c1904/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*xWM9iWnWHxfl8tT-Km-Qgg.png" /></figure><h3>6. Draw a Linear Gradient</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0307aff16bfe37149a503cd7896a31a0/href">https://medium.com/media/0307aff16bfe37149a503cd7896a31a0/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/318/1*DUdnr0ijjWqh-o58SyE7GQ.png" /></figure><h3>7. Draw a Radial Gradient</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/02f9fa73139528042d9ab2ea767df9ac/href">https://medium.com/media/02f9fa73139528042d9ab2ea767df9ac/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/316/1*JCJX47AuOz-eW2kfExT03Q.png" /></figure><p>That’s all. You can <a href="https://github.com/quangtqag/Tutorials/raw/master/ArbitraryDrawing/ArbitraryDrawing.playground.zip">download Playground file here</a>.</p><blockquote>If this post is useful, please support me by trying my free <a href="https://apps.apple.com/us/app/animal-5d/id1467725537">Animal 5D</a> app.<a href="https://apps.apple.com/us/app/animal-5d/id1467725537"> Animal 5D</a> uses AR tech to teach children to know animals. This app helps children to have physical activities to create health and more interested in learning. <a href="https://apps.apple.com/us/app/animal-5d/id1467725537">Check It Out!</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ee6dad7eb49" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Custom Photo Capture using AVCaptureSession]]></title>
            <link>https://medium.com/@quangtqag/simple-photo-capture-using-avcapturesession-b531c2f090b3?source=rss-f0724b4008e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/b531c2f090b3</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[capture-photo]]></category>
            <category><![CDATA[custom-photo-capture]]></category>
            <category><![CDATA[avcapturesession]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Quang Quoc Tran]]></dc:creator>
            <pubDate>Wed, 20 Feb 2019 09:00:18 GMT</pubDate>
            <atom:updated>2020-11-24T00:18:42.727Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7UMqQH8OHI2McP5TQuO6Gw.png" /></figure><p>In this tutor, you will create a simple custom photo capture app by using <a href="https://developer.apple.com/documentation/avfoundation/avcapturesession">AVCaptureSession</a>, to help you having a basic knowledge and easy to understand.</p><p>For a full feature app such as capture photo, live photo, depth data, portrait effects matte and video recording, you should view <a href="https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/avcam_building_a_camera_app">this sample</a> from Apple.</p><p>The heart of a custom capture photo app is <a href="https://developer.apple.com/documentation/avfoundation/avcapturesession">AVCaptureSession</a>. <a href="https://developer.apple.com/documentation/avfoundation/avcapturesession">AVCaptureSession</a> is as an intermediate component responsible for get data from the camera and put it into <a href="https://developer.apple.com/documentation/avfoundation/avcapturevideopreviewlayer">AVCaptureVideoPreviewLayer</a>.</p><p>Before we get started, you can download <a href="https://github.com/quangtqag/Tutorials/raw/master/CustomPhotoCapture/CustomPhotoCapture.zip">the full source code project</a> here, in case you stuck at any steps below.</p><h3>1. Create Preview View</h3><p>You need to create a preview view to show the content from the camera:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68e334e38ba4cc38292e0534365a9cf9/href">https://medium.com/media/68e334e38ba4cc38292e0534365a9cf9/href</a></iframe><h3>2. Config Session</h3><p>You need to allow the session know about inputs and outputs. Add the following snippet into your view controller:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d31f3f8bf1b67cc906362bdfadd7b39/href">https://medium.com/media/8d31f3f8bf1b67cc906362bdfadd7b39/href</a></iframe><p>Now run the app and you will see the content from the camera appear on your phone screen.</p><h3>3. Capture Photo</h3><p>To capture a photo you need to do the following steps:</p><ol><li>Add an action for capturing a photo:</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a585537e0a4faff159ba523d4e5b383/href">https://medium.com/media/3a585537e0a4faff159ba523d4e5b383/href</a></iframe><p>2. Add the methods for receiving image data and save the image data to Photo Library</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e9728cb7763a251f00b22c68ec430743/href">https://medium.com/media/e9728cb7763a251f00b22c68ec430743/href</a></iframe><p>3. Add the usage description for camera and Photo Library into the <em>info.plist</em> file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50ea52c609ff5f634e1accf9cc8546f3/href">https://medium.com/media/50ea52c609ff5f634e1accf9cc8546f3/href</a></iframe><p>Now run the app and capture photos. If you stuck at any steps, you can download <a href="https://github.com/quangtqag/Tutorials/raw/master/CustomPhotoCapture/CustomPhotoCapture.zip">the full source code project</a> here.</p><h3>Please Support Me!</h3><p>If this post is useful, please support me by trying my free <a href="https://apps.apple.com/us/app/animal-5d/id1467725537">Unimal</a> app. Thanks for your support.</p><figure><a href="https://apps.apple.com/us/app/animal-5d/id1467725537"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jHcrUuPtag4KlcfDWOnGaA.jpeg" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b531c2f090b3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Loop Playlist]]></title>
            <link>https://medium.com/@quangtqag/play-song-list-and-loop-it-indefinitely-e1c82c870b3c?source=rss-f0724b4008e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/e1c82c870b3c</guid>
            <category><![CDATA[loop-songs]]></category>
            <category><![CDATA[play-song-list]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[loop-playlist]]></category>
            <dc:creator><![CDATA[Quang Quoc Tran]]></dc:creator>
            <pubDate>Wed, 13 Feb 2019 04:28:03 GMT</pubDate>
            <atom:updated>2020-03-10T04:28:32.909Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-fq96xg8HLhtU2tq3gRFMQ.png" /></figure><p>In this tutor, you will play a mp3 song list and loop it. Before you start you can download <a href="https://drive.google.com/open?id=1fpHdLzKC4xr0HBObNROi4zTbOsxt0kaq">the full source code project here</a> in case you stuck at anywhere.</p><p>To play a song list you can use <a href="https://developer.apple.com/documentation/avfoundation/avqueueplayer">AVQueuePlayer</a>, but it does not support loop, and add the loop function into it is complicated. So we will not use it, we will do it manually.</p><p>Steps we will do:</p><ol><li>Create a songs bundle.</li><li>Load songs from the bundle and convert them into <strong>AVPlayerItem</strong>s.</li><li>Play first song.</li><li>Play next song.</li><li>Fix bug: the first song can not play again after it has been played first time.</li></ol><h3>1. Create a Songs Bundle</h3><p>At first, you need to create a bundle that contains songs. Creating a bundle help you get all files in a bundle that you do not need to know their name.</p><p>See the following steps to know how to create a bundle:</p><ol><li>Create a new folder inside your project folder with the extension is <strong>.bundle</strong> (for example: <strong>Songs.bundle</strong>).</li><li>Copy sound files into that new folder.</li><li>Drag that new folder into the project that opening in Xcode.</li><li>Retrieve the URLs by using urls(forResourcesWithExtension:subdirectory:) method of <strong>Bundle</strong> class</li></ol><h3>2. Create <strong>AVPlayerItems from the songs in the bundle</strong></h3><p>To be able to play the songs, you can use AVPlayer and AVPlayerItem.</p><p>Add the following code snippet into your view controller and call it from viewDidLoad() method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f72e99168d8b37c05c732698c580d3c/href">https://medium.com/media/5f72e99168d8b37c05c732698c580d3c/href</a></iframe><h3>3. Play First Song</h3><p>Add the following code snippet into your view controller to play first song:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6e945555466bd0e8bc8d038c18ee9fe/href">https://medium.com/media/b6e945555466bd0e8bc8d038c18ee9fe/href</a></iframe><p>Now run the app, you will hear the sound has been emitted out :)</p><h3>Play Next Song</h3><p>After the first song finished playing, the player does not continue playing next song. So we will do it.</p><p>At first, you need to register the notification when player item finished playing. Add the following snippet code intoviewDidLoad() method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3efedc1b68e164a1c72218fd77d651de/href">https://medium.com/media/3efedc1b68e164a1c72218fd77d651de/href</a></iframe><p>Add the method for receiving the notification and play next song:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3645ce75ec6bd3efecf7c72a2667ddff/href">https://medium.com/media/3645ce75ec6bd3efecf7c72a2667ddff/href</a></iframe><p>Run the app and you will see that the app can play next song. That’s great. But after the last song played and the first song begin playing again then no sound has been emitted out. Why is that? Pls view next guide.</p><h3>Fix Bug: The First Song can NOT Play Again after It has been Played First Time.</h3><p>B/c AVPlayerItem is a dynamic object, it store its current playing position. Not as AVAsset is a static object that only store the presentation of a media and can not change. Next time, when you play it then it will continue playing at the current position. In this case, the first player item played at the end position, when you play it again then it will play at the end position, so you will not hear any sound. So let it play at the starting position you need to seek it to the starting position by using <a href="https://developer.apple.com/documentation/avfoundation/avplayeritem/1387418-seek">seek(to:completionHandler:)</a> method. You will update playerItemDidFinishPlaying(sender: Notification) method as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ad808f73779f78b951ffa21c97bf81c/href">https://medium.com/media/3ad808f73779f78b951ffa21c97bf81c/href</a></iframe><p>That is all. You can download <a href="https://github.com/quangtqag/Tutorials/raw/master/LoopPlaylist/LoopPlaylist.zip">the full source code project here</a>.</p><h3>Please Support Me!</h3><p>If this post is useful, please support me by trying my free <a href="https://apps.apple.com/us/app/animal-5d/id1467725537">Animal 5D</a> app. Thanks for your support.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e1c82c870b3c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Play a Video and Continue Playing Audio in Background]]></title>
            <link>https://medium.com/@quangtqag/play-a-video-and-continue-playing-audio-in-background-456ec139eae?source=rss-f0724b4008e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/456ec139eae</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[avplayer]]></category>
            <category><![CDATA[audio-background]]></category>
            <category><![CDATA[play-a-video]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Quang Quoc Tran]]></dc:creator>
            <pubDate>Mon, 11 Feb 2019 10:25:22 GMT</pubDate>
            <atom:updated>2020-11-24T00:14:12.412Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*exPVbowcME_59lTXy989Pw.png" /></figure><p>Before read this tutor, you can download <a href="https://github.com/quangtqag/Tutorials/raw/master/BasicVideoPlayer/BasicVideoPlayer.zip">the full source code project here</a>, if you stuck at anywhere.</p><h3>Play a Video</h3><p>To play a video quickly then you can use <em>AVPlayerViewController</em> and <em>AVPlayer</em>. Copy the following code snippet into your view controller and try running it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2095bf8eea684ebb5b2aa074c8be496/href">https://medium.com/media/d2095bf8eea684ebb5b2aa074c8be496/href</a></iframe><p>You will realize that you can not play it. B/c the protocol of the video url is http, not https, so you need to disable <a href="https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW33">App Transport Security</a> by adding the following code snippet into <strong>Info.plist</strong> file as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/11452dea54f52b06c33baba671644f3e/href">https://medium.com/media/11452dea54f52b06c33baba671644f3e/href</a></iframe><p>Now run the app and you could play the video.</p><h3>Continue Playing Audio in Background</h3><p>Now when you press <em>Home</em> button then the sound will be muted. So to allow the sound continue playing in background mode then you need to enable this capability in <em>Signing &amp; Capabilities</em> tab as figure shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a3lw0Lgv-3iusUHXqsLIww.png" /></figure><p>In addition, you also add the following code snippet into application(_:didFinishLaunchingWithOptions:) method in <strong>AppDelegate.swift</strong> file as follow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a333d852d2ac991d48627f7f59065ee/href">https://medium.com/media/9a333d852d2ac991d48627f7f59065ee/href</a></iframe><p>Add the following methods into your view controller to disconnect and reconnect AVPlayer:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/202559ca526718060ac5a0c0b627fd4d/href">https://medium.com/media/202559ca526718060ac5a0c0b627fd4d/href</a></iframe><p>Add the following methods into your <em>AppDelegate.swift</em> file for calling disconnect and reconnect AVPlayer:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1e5216d9e0848866a460252f4be7389b/href">https://medium.com/media/1e5216d9e0848866a460252f4be7389b/href</a></iframe><p>Now run the app and test it. You can download <a href="https://github.com/quangtqag/Tutorials/raw/master/BasicVideoPlayer/BasicVideoPlayer.zip">the full source code project here</a>.</p><h3>References</h3><p><a href="https://developer.apple.com/documentation/avfoundation/media_assets_playback_and_editing/creating_a_basic_video_player_ios_and_tvos/enabling_background_audio">Enabling Background Audio</a><br><a href="https://developer.apple.com/documentation/avfoundation/avaudiosession">AVAudioSession</a><br><a href="https://developer.apple.com/documentation/avfoundation/media_assets_playback_and_editing/creating_a_basic_video_player_ios_and_tvos/playing_audio_from_a_video_asset_in_the_background">Playing Audio from a Video Asset in the Background</a></p><h3>Please Support Me!</h3><p>If this post is useful, please support me by trying my free <a href="https://apps.apple.com/us/app/animal-5d/id1467725537">Unimal</a> app. Thanks for your support!</p><figure><a href="https://apps.apple.com/us/app/animal-5d/id1467725537"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jHcrUuPtag4KlcfDWOnGaA.jpeg" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=456ec139eae" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rename Project Completely]]></title>
            <link>https://medium.com/@quangtqag/rename-a-project-48a8c6d43262?source=rss-f0724b4008e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/48a8c6d43262</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[rename]]></category>
            <category><![CDATA[rename-project]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Quang Quoc Tran]]></dc:creator>
            <pubDate>Mon, 11 Feb 2019 04:45:14 GMT</pubDate>
            <atom:updated>2020-03-09T04:31:10.476Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CIwLyyaK0Y_-QLSYm54Ixg.jpeg" /></figure><p>This tutorial will guide you how to rename a project completely.</p><h4>Steps:</h4><ol><li>Create a new project with the name <em>“Demo”</em>. We will change it’s name to <em>“Demo2”</em></li><li>Rename the project</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/542/1*R32iANumxBEG7U_RVIgzYg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*O46hDE_iK057nxKYiBfKLg.png" /></figure><p>3. Rename the target folder</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*5lWwHelSKMHPK8zYAjjb8A.png" /></figure><p>4. Update the location of the <em>Info.plist</em> file</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VGghgNqagsetXOvwwhKtTQ.png" /></figure><p>5. Rename the scheme</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/1*N2l_yMkK_nrcYHkzUrZuQg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/868/1*9Q80BZV-3jqT-8Lwe9rPBA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yVM8fNMw4zzCBymcceoLqA.png" /></figure><p>6. Update the bundle identifier</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ce59M098xh7zheM0gy2oxQ.png" /></figure><p>That’s all. Now you can run the project without any problems.</p><h3>Support Me!</h3><p>If this post is useful, please support me by trying my free <a href="https://apps.apple.com/us/app/animal-5d/id1467725537">Animal 5D</a> app. Thanks for your support.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=48a8c6d43262" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Background Audio Player Sync Control Center]]></title>
            <link>https://medium.com/@quangtqag/background-audio-player-sync-control-center-516243c2cdd1?source=rss-f0724b4008e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/516243c2cdd1</guid>
            <category><![CDATA[audio-background]]></category>
            <category><![CDATA[player-audio]]></category>
            <category><![CDATA[control-center]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Quang Quoc Tran]]></dc:creator>
            <pubDate>Mon, 11 Feb 2019 01:05:09 GMT</pubDate>
            <atom:updated>2020-11-24T00:15:50.339Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yeDaoADTT7Htz4WNQIloHw.jpeg" /></figure><p>In this tutorial, you will create a player with capability of play audio in background mode and synchronize with music controls in Control Center.</p><p>Features:</p><ol><li>Play a sound file.</li><li>Make the sound continue playing in background mode.</li><li>Control the audio from Control Center.</li><li>Handle finish playing.</li><li>Handle interruptions.</li><li>Handle route changes.</li></ol><p>Before go into every feature, you can download <a href="https://github.com/quangtqag/Tutorials/raw/master/BackgroundAudioPlayer/BackgroundAudioPlayer.zip">the full source code project here</a> if you stuck at anywhere.</p><h3>1. Play a Sound File</h3><p>To play a sound file, you need to initializae a player. Add the following block code into your view controller to initialize the player:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ef80a87120d80d6f1dca448d2d6bbdf/href">https://medium.com/media/9ef80a87120d80d6f1dca448d2d6bbdf/href</a></iframe><p>To play the mp3 file, you call play() method of the player as follows:</p><p>player.play()</p><p>Now you can hear the sound from the audio file that I imported “song.mp3”. But when you press Home button to move the app into background mode then the sound has been muted. So we need to allow it continue playing in background mode in next guide.</p><h3>2. Make the Sound Continue Playing in Background Mode</h3><p>Now when you press <em>Home</em> button then the sound will be muted. So to allow the sound continue playing in background mode then you need to enable this capability in <em>Signing &amp; Capabilities</em> tab as figure shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a3lw0Lgv-3iusUHXqsLIww.png" /></figure><p>In addition, you also replace application(_:didFinishLaunchingWithOptions:) method in <strong>AppDelegate.swift</strong> file as follows to config audio session:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f87545049fb69621fbfacea5f3702eab/href">https://medium.com/media/f87545049fb69621fbfacea5f3702eab/href</a></iframe><p>Now run the app, play the sound and press Home button to move the app into background mode and check if the sound still continue playing.</p><h3>3. Control the Audio from Control Center</h3><p>Read the following article to know how to controll background audio from the Control Center and iOS Lock screen:</p><p><a href="https://developer.apple.com/documentation/avfoundation/media_assets_playback_and_editing/creating_a_basic_video_player_ios_and_tvos/controlling_background_audio"><strong>Controlling Background Audio</strong></a><strong><br></strong>Support controlling background audio from the Control Center and iOS Lock screen.</p><p>Add the following methods into your view controller and call setupRemoteTransportControls(), setupNowPlaying() methods from viewDidLoad() method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0bcee08427a11d18fdafac980ee9fad/href">https://medium.com/media/c0bcee08427a11d18fdafac980ee9fad/href</a></iframe><p>Now run the app, open Control Center and check it.</p><h3>4. Handle Finish Playing</h3><p>After audio player finished playing, you need to update nowPlayingInfo and UI. To receive this event you need to implement <a href="https://developer.apple.com/documentation/avfoundation/avaudioplayerdelegate/1389160-audioplayerdidfinishplaying">audioPlayerDidFinishPlaying(_:successfully:)</a> method of <a href="https://developer.apple.com/documentation/avfoundation/avaudioplayerdelegate">AVAudioPlayerDelegate</a> protocol and update setUpPlayer() method as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9883cba9ad09058dbfb060345da5b66/href">https://medium.com/media/c9883cba9ad09058dbfb060345da5b66/href</a></iframe><p>Run the app and check it.</p><h3>5. Handle Interruptions</h3><p>When you are playing in background mode, if a phone call come then the sound will be muted but when hang off the phone call then the sound does not automatically continue playing. So we need to handle these interruption events by reading the following article:</p><p><a href="https://developer.apple.com/documentation/avfoundation/avaudiosession/responding_to_audio_session_interruptions"><strong>Responding to Audio Session Interruptions</strong></a><strong><br></strong>Directly observe audio session notifications to ensure that your app responds to interruptions.</p><p>Add the following code snippet for autoplay when the interruption ended:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c88aa9c667a504e92443fca8b6a032d/href">https://medium.com/media/6c88aa9c667a504e92443fca8b6a032d/href</a></iframe><p>Now run the app and test it.</p><h3>6. Handle Route Changes</h3><p>Now when you plug a headphone into the phone then the sound will emit on the headphone. But when you unplug the headphone then the sound automatically continue playing on built-in speaker. Maybe this is the behavior that you don’t expect. B/c when you plug the headphone into you want the sound is private to you, and when you unplug it you don’t want it emit out to other people. We will handle it by receiving events when the route change.</p><p>At first you need to read this article to understand about route changes:</p><p><a href="https://developer.apple.com/documentation/avfoundation/avaudiosession/responding_to_audio_session_route_changes"><strong>Responding to Audio Session Route Changes</strong></a><strong><br></strong>Directly observe audio session notifications to ensure that your app responds to interruptions.</p><p>Update setupNotifications() method and add handleRouteChange(: Notification) method as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ce11e15e7411610753eea7dc2624eed/href">https://medium.com/media/7ce11e15e7411610753eea7dc2624eed/href</a></iframe><p>Run the app and test it.</p><p>That is all. You can download <a href="https://github.com/quangtqag/Tutorials/raw/master/BackgroundAudioPlayer/BackgroundAudioPlayer.zip">the full source code project here</a></p><h3>Please Support Me!</h3><p>If this post is useful, please support me by trying my free <a href="https://apps.apple.com/us/app/animal-5d/id1467725537">Unimal</a> app. Thanks for your support!</p><figure><a href="https://apps.apple.com/us/app/animal-5d/id1467725537"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jHcrUuPtag4KlcfDWOnGaA.jpeg" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=516243c2cdd1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Core Animation Examples]]></title>
            <link>https://medium.com/@quangtqag/core-animation-examples-742f9af00188?source=rss-f0724b4008e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/742f9af00188</guid>
            <category><![CDATA[sample]]></category>
            <category><![CDATA[core-animation]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[example]]></category>
            <dc:creator><![CDATA[Quang Quoc Tran]]></dc:creator>
            <pubDate>Wed, 30 Jan 2019 08:51:45 GMT</pubDate>
            <atom:updated>2019-01-30T08:51:45.577Z</atom:updated>
            <content:encoded><![CDATA[<p>What Core Animation help you:</p><ul><li>High frame rates and smooth animations without burdening the CPU and slowing down your app.</li><li>More fine-grained control over your app’s animations.</li><li>Style UIView a higher level as corner radius, shadow…</li></ul><p>You can download <a href="https://drive.google.com/open?id=1CiHt5I0T68gp0GFz3c7SjPg0Zep7dCZN">the final project</a> to understand more when reading this tutor</p><h3>Core Animation Manages Your App’s Content</h3><p>Core Animation is not a drawing system itself. It is an infrastructure for compositing and manipulating your app’s content in hardware. At the heart of this infrastructure are <em>layer objects</em>, which you use to manage and manipulate your content. A layer captures your content into a bitmap that can be manipulated easily by the graphics hardware. In most apps, layers are used as a way to manage the content of views but you can also create standalone layers depending on your needs.</p><p>You access this layer object by calling layer property of a view. The following sample change background color of a view by changing backgound color of view layer:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5f2431b22f87f65c35d4db76733cba9/href">https://medium.com/media/e5f2431b22f87f65c35d4db76733cba9/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*nT1ZL5sqD2NuDMy-148t-A.gif" /></figure><p>You also need to convert UIColor into CGColor for the value of backgroundColor property of layer</p><h3>Layers Can Be Organized into Hierarchies</h3><p>Layers can be arranged hierarchically to create parent-child relationships. The arrangement of layers affects the visual content that they manage in a way that is similar to views. The hierarchy of a set of layers that are attached to views mirrors the corresponding view hierarchy. You can also add standalone layers into a layer hierarchy to extend the visual content of your app beyond just your views.</p><p>We will view thefollowing sample. We will create a new layer and add it into the primary layer of view:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79261062bd151144911c32f27da642da/href">https://medium.com/media/79261062bd151144911c32f27da642da/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*je_STwS3w4fG5rapRhkPFQ.gif" /></figure><h3>Layers Support a Corner Radius</h3><p>You can create a rounded rectangle effect for your layer by adding a corner radius to it. To apply a corner radius to your layer, specify a value for the <a href="https://developer.apple.com/documentation/quartzcore/calayer/1410818-cornerradius">cornerRadius</a> property of the layer. The radius value you specify is measured in points and applied to all four corners of the layer prior to display.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f9a20ab7ceb7841b3e2d0c240079716/href">https://medium.com/media/6f9a20ab7ceb7841b3e2d0c240079716/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*E6eQKApmFr-aYpCRFFNj6Q.gif" /></figure><h3>Layers Support Built-In Shadows</h3><p>The <a href="https://developer.apple.com/documentation/quartzcore/calayer">CALayer</a> class includes several properties for configuring a shadow effect. A shadow adds depth to the layer by making it appear as if it is floating above its underlying content. You can control the shadow’s color, placement relative to the layer’s content, opacity, and shape.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5969753d9a9f477ce4acb516dd74aaf8/href">https://medium.com/media/5969753d9a9f477ce4acb516dd74aaf8/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*4-HpFlFC3lvEO7EJk2ynfA.gif" /></figure><h3>Animating Simple Changes to a Layer’s Properties</h3><p>You can perform simple animations implicitly or explicitly depending on your needs. Implicit animations use the default timing and animation properties to perform an animation, whereas explicit animations require you to configure those properties yourself using an animation object. So implicit animations are perfect for situations where you want to make a change without a lot of code and the default timing works well for you.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cf69e51f2cd7ac67f56aacc1169aa6de/href">https://medium.com/media/cf69e51f2cd7ac67f56aacc1169aa6de/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*KqvhRVQP-CnuG_oNbLCmsA.gif" /></figure><p>To make the same change explicitly using an animation object, create a <a href="https://developer.apple.com/documentation/quartzcore/cabasicanimation">CABasicAnimation</a> object and use that object to configure the animation parameters. You can set the start and end values for the animation, change the duration, or change any other animation parameters before adding the animation to a layer. The following sample shows how to fade out a layer using an animation object. When creating the object, you specify the key path for the property you want to animate and then set your animation parameters. To execute the animation, you use the <a href="https://developer.apple.com/documentation/quartzcore/calayer/1410848-addanimation">addAnimation:forKey:</a> method to add it to the layers you want to animate.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/48f6f6fc698e60e6cb30f81b57dd8c07/href">https://medium.com/media/48f6f6fc698e60e6cb30f81b57dd8c07/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*eQ_7n2lalkYJVjEPg46meg.gif" /></figure><h3>Using a Keyframe Animation to Change Layer Properties</h3><p>Whereas a property-based animation changes a property from a start value to an end value, a <a href="https://developer.apple.com/documentation/quartzcore/cakeyframeanimation">CAKeyframeAnimation</a> object lets you animate through a set of target values in a way that might or might not be linear. A key frame animation consists of a set of target data values and the times at which each value should be reached. In the simplest configuration, you specify both the values and times using an array. For changes to a layer’s position, you can also have the changes follow a path. The animation object takes the key frames you specify and builds the animation by interpolating from one value to the next over the given time periods.</p><p>The following sample shows a 5-second animation of a layer’s <a href="https://developer.apple.com/documentation/quartzcore/calayer/1410791-position">position</a> property. The position is animated to follow a path, which was specified using a <a href="https://developer.apple.com/documentation/coregraphics/cgpath">CGPath</a> data type.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89f824a43e7619fedced4131da56d84b/href">https://medium.com/media/89f824a43e7619fedced4131da56d84b/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*qW_-9XRvxFjfGGHrPt4ccg.gif" /></figure><h4>Specifying Keyframe Values</h4><p>The key frame values are the most important part of a keyframe animation. These values define the behavior of the animation over the course of its execution. The main way to specify keyframe values is as an array of objects. What you put into the array depends on the data type required by the property.</p><ul><li>For properties that take a <a href="https://developer.apple.com/documentation/coregraphics/cgrect">CGRect</a> (such as the bounds and frame properties), wrap each rectangle in an NSValue object.</li><li>For the layer’s transform property, wrap each <a href="https://developer.apple.com/documentation/quartzcore/catransform3d">CATransform3D</a> matrix in an NSValue object. Animating this property causes the keyframe animation to apply each transform matrix to the layer in turn.</li><li>When animating the layer’s <a href="https://developer.apple.com/documentation/quartzcore/calayer/1410773-contents">contents</a> property, specify an array of <a href="https://developer.apple.com/documentation/coregraphics/cgimageref">CGImageRef</a> data types.</li></ul><p>For properties that take a CGPoint data type, you can create an array of points (wrapped in NSValue objects) or you can use a CGPathRefobject to specify the path to follow. When you specify an array of points, the keyframe animation object draws a straight line between each successive point and follows that path. When you specify a CGPathRef object, the animation starts at the beginning point of the path and follows its outline, including along any curved surfaces.</p><h4>Specifying the Timing of a Keyframe Animation</h4><p>The keyTimes property defines the points in time at which to apply each keyframe value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b8c1442deb64c39db26cea4041b0010/href">https://medium.com/media/2b8c1442deb64c39db26cea4041b0010/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*teIc_No47EyrCTDgOtA_wg.gif" /></figure><h3>Animating Multiple Changes Together</h3><p>If you want to apply multiple animations to a layer object simultaneously, you can group them together using a <a href="https://developer.apple.com/documentation/quartzcore/caanimationgroup">CAAnimationGroup</a>object. Using a group object simplifies the management of multiple animation objects by providing a single configuration point. Timing and duration values applied to the group override those same values in the individual animation objects.</p><p>The following sample shows how you would use an animation group to perform two border-related animations at the same time and with the same duration.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/564b96ec2d153217a5164a30515b829e/href">https://medium.com/media/564b96ec2d153217a5164a30515b829e/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*ZJiL3cGKeMD3el1XN4qV3g.gif" /></figure><h3>Transition Animations Support Changes to Layer Visibility</h3><p>As the name implies, a transition animation object creates an animated visual transition for a layer. The most common use for transition objects is to animate the appearance of one layer and the disappearance of another in a coordinated manner. Unlike a property-based animation, where the animation changes one property of a layer, a transition animation manipulates a layer’s cached image to create visual effects that would be difficult or impossible to do by changing properties alone. The standard types of transitions let you perform reveal, push, move, or crossfade animations.</p><p>To perform a transition animation, you create a <a href="https://developer.apple.com/documentation/quartzcore/catransition">CATransition</a> object and add it to the layers involved in the transition. You use the transition object to specify the type of transition to perform and the start and end points of the transition animation. You do not need to use the entire transition animation either. The transition object lets you specify the start and end progress values to use when animating. These values let you do things like start or end an animation at its midpoint.</p><p>The following sample shows the code used to create an animated push transition between two views. In the example, both myView1 and myView2 are located at the same position in the same parent view but only myView1 is currently visible. The push transition causes myView1 to slide out to the left and fade until it is hidden while myView2 slides in from the right and becomes visible. Updating the hidden property of both views ensures that the visibility of both views is correct at the end of the animation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6171ba176127c622009e8df9445e44bc/href">https://medium.com/media/6171ba176127c622009e8df9445e44bc/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*nkWzLB8pN-g5qwXOBeewqg.gif" /></figure><p>When two layers are involved in the same transition, you can use the same transition object for both. Using the same transition object also simplifies the code you have to write. However, you can use different transition objects and would definitely need to do so if the transition parameters for each layer are different.</p><h3>Adding Perspective to Your Animations</h3><p>Apps can manipulate layers in three spatial dimensions, but for simplicity Core Animation displays layers using a parallel projection, which essentially flattens the scene into a two-dimensional plane. This default behavior causes identically sized layers with different <a href="https://developer.apple.com/documentation/quartzcore/calayer/1410884-zposition">zPosition</a>values to appear as the same size, even if they are far apart on the z axis. The perspective that you would normally have viewing such a scene in three dimensions is gone. However, you can change that behavior by modifying the transformation matrix of your layers to include perspective information.</p><p>The following sample shows the the layer rotate along y axis. In this case the custom eyePosition variable specifies the relative distance along the z axis from which to view the layers. Usually you specify a positive value for eyePosition to keep the layers oriented in the expected way. Larger values result in a flatter scene while smaller values cause more dramatic visual differences between the layers.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/550f27208684032e886ee320f4392b5c/href">https://medium.com/media/550f27208684032e886ee320f4392b5c/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*i_NtBE8JIXoMR3GHjQn0Sg.gif" /></figure><h3>Mask Properties</h3><p>You can use a mask to obscure all or part of a layer’s contents. The mask is itself a layer object whose alpha channel is used to determine what is blocked and what is transmitted. Opaque portions of the mask layer’s contents allow the underlying layer content to show through while transparent portions partially or fully obscure the underlying content. The following sample shows a sample layer composited with a mask layer:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b495fd98b8b0360574ce692502050e3/href">https://medium.com/media/2b495fd98b8b0360574ce692502050e3/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/828/1*1i97lGwTt89Oit3QNe06iQ.png" /></figure><p>You can download <a href="https://drive.google.com/open?id=1CiHt5I0T68gp0GFz3c7SjPg0Zep7dCZN">the final project here</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=742f9af00188" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>