<?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 A-Tân on Medium]]></title>
        <description><![CDATA[Stories by A-Tân on Medium]]></description>
        <link>https://medium.com/@atan5?source=rss-cd4fbb6cf54------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_Xq4bRN7dPLXrmHTntXEvw.jpeg</url>
            <title>Stories by A-Tân on Medium</title>
            <link>https://medium.com/@atan5?source=rss-cd4fbb6cf54------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 02:04:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@atan5/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[GMUClusterManager處理相同經緯度的Marker時，出現的錯誤與解法]]></title>
            <link>https://atan5.medium.com/gmuclustermanager%E8%99%95%E7%90%86%E7%9B%B8%E5%90%8C%E7%B6%93%E7%B7%AF%E5%BA%A6%E7%9A%84marker%E6%99%82-%E5%87%BA%E7%8F%BE%E7%9A%84%E9%8C%AF%E8%AA%A4%E8%88%87%E8%A7%A3%E6%B3%95-1a9fc6bb2037?source=rss-cd4fbb6cf54------2</link>
            <guid isPermaLink="false">https://medium.com/p/1a9fc6bb2037</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[objective-c]]></category>
            <category><![CDATA[gmuclustermanager]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[google-map-marker]]></category>
            <dc:creator><![CDATA[A-Tân]]></dc:creator>
            <pubDate>Wed, 02 Jun 2021 15:48:48 GMT</pubDate>
            <atom:updated>2021-06-02T15:48:48.459Z</atom:updated>
            <content:encoded><![CDATA[<p>最近在做公司的App，用到iOS Google Map的SDK，因為公司的產品是跟房屋有關的，假設一棟房子中有好幾層樓的物件，都放在我們的App上，就會在地圖上出現經緯度完全一樣、完全重疊在一起的Marker。</p><p>而 iOS Goolge Map SDK 中，GMUClusterManager 預設的 GMUDefaultClusterRenderer有一個現象，是當地圖上相近的Marker大於4個點的時候，才會進行聚集的效果。</p><p>這時候出現一個嚴重的問題，就是假設同一個經緯度上有3個房源，也就是3個「1間房源」的Marker，而我們預期GMUClusterManager會幫我們把那3個集中在一起，變成一個呈現為「3間房源」的Marker，但因為上述Renderer的預設要4個才會聚集，因此就變成分散的3個「1間房源」的Marker完全重疊在地圖上。</p><p>當點擊那個「1間房源」的時候，理論上UI的邏輯是要讓那個Marker變深，但同時同個地點的另外兩個Marker都沒有變深，在地圖上就出現深、淺、深、淺…不停交互閃爍的錯誤效果，而且「1間」這個資料也是錯誤的。</p><p>（一開始不知道這個Bug是怎麼回事，苦苦抓了好幾天，最後才發現原來是Marker重疊在一起XDDD）</p><p>解決方法很簡單，參考<a href="https://github.com/googlemaps/google-maps-ios-utils/issues/213">Github上的這個Issue</a>，原因就在GMUDefaultClusterRenderer中minimumClusterSize這個參數預設為4，只要把它改成2就好，大功告成！</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1a9fc6bb2037" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Google Map: GMUClusterManager 設定最小聚集Zoom]]></title>
            <link>https://atan5.medium.com/google-map-gmuclustermanager-%E8%A8%AD%E5%AE%9A%E6%9C%80%E5%B0%8F%E8%81%9A%E9%9B%86zoom-407dcee5b622?source=rss-cd4fbb6cf54------2</link>
            <guid isPermaLink="false">https://medium.com/p/407dcee5b622</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[objective-c]]></category>
            <category><![CDATA[google-map-marker]]></category>
            <category><![CDATA[gmuclustermanager]]></category>
            <dc:creator><![CDATA[A-Tân]]></dc:creator>
            <pubDate>Sat, 29 May 2021 15:12:13 GMT</pubDate>
            <atom:updated>2021-05-29T15:12:13.321Z</atom:updated>
            <content:encoded><![CDATA[<p>最近工作上碰到Google Map，並用到GMUClusterManager，它的作用簡單來說，就是會自動聚合地圖上的Marker，當User把地圖縮小時，會自動把距離接近的Marker縮成一個點，假設是把五個各為1的Marker聚合，就會縮成一個顯示為5的Marker。</p><p>詳情可以看 <a href="https://medium.com/u/2bab2ea255a9">法蘭克的iOS世界</a> 的<a href="https://franksios.medium.com/swift-google-map-marker-clustering-%E6%A8%99%E8%A8%98%E5%8F%A2%E9%9B%86-a2402f9dcfb3">這篇貼文</a>，有完整的說明跟gif，介紹得非常詳盡。</p><p>後來設計大大給了一個有點困難的需求，就是當User把圖持續縮小時，希望到了一個臨界點之後，就不要再進行聚合，停留在原本臨界點中聚合的狀態。</p><p>舉例來說，假設User進入地圖時，MapView的Camera的Zoom設為15，大約是「左下角：大安森林公園，右上角：南京復興站」的大小，此時<strong>地圖上有12個各為「1」的Marker</strong>；當User把地圖縮小，Zoom到12時大約是「上方：北投，下方：碧潭」的範圍，這時候<strong>地圖上有5個各為「50」的Marker</strong>，就是GMUClusterManager的神奇效果；</p><p>然後我們希望將12的這個Zoom設為臨界點，接下來User往下繼續縮小，縮到11、10、9……的時候，<strong>台北市都一樣停留在5個「50」的Marker，而不是將整個北部地區都群聚成1個「250」的Marker</strong>，這個需求該如何達成呢？</p><p>Google Map官方的文件中，有一篇<a href="https://developers.google.com/maps/documentation/ios-sdk/utility/marker-clustering">關於Marker Clustering的文章</a>，不過沒有特別提到任何上限、下限、最大、最小的觀念，除了「For best performance, the recommended maximum number of markers is 10,000」這句沒什麼用的描述。</p><p>而GMUClusterManager中會用到的GMUDefaultClusterRenderer，有一個參數叫maximumClusterZoom，但如同它註解中寫的，”<em>Sets the maximium zoom level of the map on which the clustering should be applied. At zooms above this level, clusters will be expanded.</em>”，實際上的作用是讓任何大於它的Zoom，都不要再做任何群聚，例如設為15時，那放大到超過15後，就全部散開成一堆「1」的Marker，因此完全不是我們要的效果。</p><p>後來我再仔細看了一下官方文件，發現文件最後有提到一段<a href="https://developers.google.com/maps/documentation/ios-sdk/utility/marker-clustering#customize-marker-clustering">Customize marker clustering</a>：</p><blockquote>You can provide a custom implementation for the GMUClusterRenderer, GMUClusterIconGenerator, or GMUClusterAlgorithm. You can base your custom implementation on the sample implementation of these protocols included in the utility library, or you can code a fully custom implementation by fulfilling the protocols.</blockquote><p>所以照著這個想法，我跑去翻了一下我們選用的GMUNonHierarchicalDistanceBasedAlgorithm的Code，發現.m檔中有一段- (NSArray&lt;id&lt;GMUCluster&gt;&gt; *)clustersAtZoom:(float)zoom的function，雖然Objective C要看懂真的有點難…不過大概猜一下，這段有輸入型別為float的zoom，輸出的則是GMUCluster，那應該就是這段在處理Zoom跟群聚的邏輯。</p><p>因此，照著文件試試看，我設了一個自訂的class，繼承GMUNonHierarchicalDistanceBasedAlgorithm ，並override上述的function，用super去輸出它原本的邏輯，但加入zoom的判斷條件，結果如下：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DKQODIDlSEHx6_6d4QPu6w.png" /></figure><p>然後在放入地圖的View Controller裡宣告GMUClusterManager 的時候，用這個我自己客制的Algorithm 的 class：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DK4Su0Uwd7fj82skL8Xy4Q.png" /></figure><p>結果就成功了！當user把地圖縮小到小於12時，地圖上群聚的UI真的就停留在12的階段，不會再進一步群聚！</p><p>希望這篇文可以幫助到有需要的人 :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=407dcee5b622" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[彼得潘iOS課程心得——真的轉職了！]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/%E5%BD%BC%E5%BE%97%E6%BD%98ios%E8%AA%B2%E7%A8%8B%E5%BF%83%E5%BE%97-%E7%9C%9F%E7%9A%84%E8%BD%89%E8%81%B7%E4%BA%86-42fde7e5484a?source=rss-cd4fbb6cf54------2</link>
            <guid isPermaLink="false">https://medium.com/p/42fde7e5484a</guid>
            <dc:creator><![CDATA[A-Tân]]></dc:creator>
            <pubDate>Sat, 05 Dec 2020 12:48:32 GMT</pubDate>
            <atom:updated>2020-12-05T12:48:32.449Z</atom:updated>
            <content:encoded><![CDATA[<p>拖了好久終於寫出心得了….Peter抱歉…</p><p>先介紹一下我自己，大學讀的是跟程式無關的哲學系，畢業之後，當了一年的獵頭，後來覺得自己不擅業務類的工作，去年10月離職，離職後做過一陣子政治工作，到今年1月選完。</p><h4>初期自學</h4><p>選舉期間，就想清楚自己想要轉職成工程師，也決定要往iOS開發的方向走，而因為自己本來就有接圖書館的一個大型研究案，想說趁失業的空檔，邊把案子做完邊自學。</p><p>當初的想法是，在iOS領域中，網路上本來就有完全靠自學成功轉職的案例，例如2018年<a href="https://pala.tw/ios-developer-prepare-interview/">Pala的這篇文章</a>，把自學成功的過程中，接觸到的書、線上課程、專案等資源，做了很完整的盤點，看起來是一條被驗證過後的光明大道，所以年初的時候想說，邊做案子邊自學，粗淺地把目標定在年中開始找工作。</p><p>後來才發現代誌無遐爾簡單，主要是我的個性超級沒辦法自律，坐在家裡無拘束的上課是很難有進展的，而要一面做案子一面學，等於兩邊都要顧，時間跟注意力要不斷轉移、分配，對我的個性來說很難做到，所以會產生這輩子從來沒遇過的超巨大焦慮，然後一整團情緒就越漲越大，最後對身心狀態造成巨大負擔。</p><p>除此之外，線上課程跟書，都是單向的資訊傳遞，而且很多都是國外資源，就算遇到難關要問問題，也很難跨越語言、文化的隔閡；而我又沒有認識的iOS工程師可以問，也不敢主動去接觸（敢的話就不會從獵頭辭職了哈哈），變成卡關的時候很難突破，對我初期自學的學習過程造成很大的阻礙。</p><p>當然，這完全不代表我對自學保持否定的態度，只是我發現我自己的個性不適合，自學的型態跟我自己的個性有很多地方是衝突的，因此到了六月前後，我看到最常Google到的彼得潘又要開課，就毫不猶豫的報名下去，並且放下iOS的一切，拚命在6月專心把專案做完，7月就毫無顧慮的開始全心上課。</p><p>（而且彼得潘真的讓我報失業加油價，人真的超好，嗚嗚）</p><h4>上課</h4><p>因為之前有稍微自學過，所以我報的是彼得潘班（不是文組班），開課時間是一個禮拜上兩天晚上，一堂三小時，持續大約兩個月多一點點（總共17堂）。這個時間的密度跟長度，其實跟資策會或其他密集課程，有很大的差距，但確實很適合一些上班族或學生，只是想了解iOS領域而不是為了轉職的人，我記得班上就很多這樣的同學。</p><p>不過我自己是抱著轉職的目標來的，一開始也有點擔心會不會不夠？但現在印證了上完Peter的課再去找工作，是完全可行的！</p><p>Peter的課程架構，是從swift基本語法、基本的StoryBoard開始，延伸到資料型別、protocol、closure、generic、頁面間傳遞資料、網路功能、App生命週期、資料儲存、ARC、CocoaPods/SPM、SwiftUI…等等，其實對轉職後的新手工程師來說，應該是蠻完整的；而且課程中會教學生用zeplin、postman等軟體，到實際工作上變成非常實用的經驗，這點我覺得很值得特別提出來。</p><p>但有趣的部份就在於，這麼多東西要在短短17堂課塞完，變成我覺得Peter上課的速度很快，3小時的課程超級紮實，紮實到我現在工作一個月了，還是有些東西要回頭去翻筆記才能回想起來XD，好險Peter上課時，用的是Zoom直播+錄影，可以在現場也可以在家，更可以後續重複看錄影檔案，像是我記得delegate傳資料的那個部分我就看了三四次以上….一開始很難懂，真的是好險有錄影。</p><h4>作業</h4><p>最最重要的是，Peter上課的時候雖然快速飆完進度，但作業才是真正的重點，每堂課都會有對應的作業，而且是一大堆的作業，其實不可能完全做完，不過可以從自己有興趣的部分下手，再自己結合想完成的功能去練習。</p><p>例如有個作業是音樂播放器，我自己加入了串接itunes預覽音樂的API，抓到網路上的音樂，再加上用純程式碼刻出Segment Control+Table View的畫面（<a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/%E9%9F%B3%E6%A8%82app%E7%AC%AC%E4%B8%80%E9%9B%86-701d9524ffc2">作業連結</a>），就多了一些功能的實作，所以作業彈性很大，我覺得比較像提出一些可以做的點子，讓學習iOS開發的過程有一條實作的道路可以走。</p><p>而且Peter要求的作業形式，除了放到Github，還要寫成Medium文章，不僅可以完整地介紹作品，還可以在文章中盤點自己怎麼卡關、怎麼找到資源、過程是怎麼思考的，我覺得對學習程式開發很有幫助（類似概念可以參考<a href="https://zh.wikipedia.org/zh-tw/%E5%B0%8F%E9%BB%84%E9%B8%AD%E8%B0%83%E8%AF%95%E6%B3%95">小黃鴨除錯法</a>），而iOS又剛好是很好呈現成果的領域，作業要求我們錄成Gif或影片，最後要做累積起來的作品集就會很方便，<a href="https://www.notion.so/atan5/9ac9938d45df4414b9736effc1a0a995">這個連結</a>是我最後找工作時的作品集，真的找到工作了！</p><p>換句話說，Peter的課程編排，是上課飆很快，再靠自己複習、做作業慢慢學起來，雖然我沒有其他課程經驗，沒辦法跟密集上課的形式比較優劣，但至少對我自己而言，這個方式幫助我開拓很多視野，透過做作業學習的效果，也比單向的資訊傳遞更加紮實，也讓我現在對「尋找問題解方」的能力相對有自信。</p><h4>找工作</h4><p>Peter的課程在9月初左右結束，但前面邊上課邊做作業，我自己覺得還不太夠，想要在求職前累積多一點實作，就把9月拿來繼續做作業，10月初才開始找工作。Peter說找工作大約估一個月的時間，結果還真的在10/30接到Offer，隔幾天11月就開始工作了，超剛好。</p><p>找工作的過程中，我自己觀察覺得，非本科真的是蠻大的一個傷害，很多履歷就一去不回，據以前的獵頭經驗，如果不是人資偷懶不關104的假職缺，就很有可能就是因為學歷被刷掉的，後來104上主動投遞的20幾間只有一間真的約面試，其他面試都是公司主動邀約，另外剛好我在twitter上的求職文，突然被weak self的13轉推，透過這個管道也收到蠻多面試機會的。</p><p>講到twitter，其實也蠻有趣的，去年剛好朋友跟我說有個podcast叫weak self，就開始follow下去，也進一步發現iOS領域的交流很多都在twitter上，後來在今年8, 9月左右，就先辦好twitter，慢慢讓自己社交網路這條資訊來源，加入一些跟開發有相關的東西，也能得知這個社群在討論的東西，在自學的後期階段，加深自我認同感，我自己覺得會讓心裡更踏實，至少沒有軟體業就職經驗的我，能夠更融入一點點，降低非本科、自學帶來的自我陌生感。</p><p>Peter也在我找工作的過程中，給了一些建議和觀點，像是衡量一些職缺的優缺點等等，並且如果有機會，也都可以找他Code Review；而Peter在課程一開始就有說，問他問題不是只限於幾個月的上課時間，而是一輩子遇到問題都可以問他，後來找工作的過程，遇到面試作業無法突破的點，還真的有偷偷跑去問Peter XDD</p><p>從7月加入Peter的課程到現在，得到的收穫比當初預想的還要多很多，從轉職的角度來說，Peter的課程是很紮實又很全面的，確實有包含就業市場對初階iOS工程師的需求；而從我自己的角度來看，Peter讓我從年初跌跌撞撞的強烈焦慮狀態穩定下來，慢慢在實作中確立自己對寫程式的熱情，累積出一點自信心，一步一步走到現在成為工程師。</p><p>真的很感謝Peter，是我在慌亂的2020遇到最好的貴人，也期待這篇文的經驗分享，可以幫助到同樣在轉職路上徬徨的人，希望大家最終都能走在自己生命熱情所在的道路上。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42fde7e5484a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E5%BD%BC%E5%BE%97%E6%BD%98ios%E8%AA%B2%E7%A8%8B%E5%BF%83%E5%BE%97-%E7%9C%9F%E7%9A%84%E8%BD%89%E8%81%B7%E4%BA%86-42fde7e5484a">彼得潘iOS課程心得——真的轉職了！</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[To-Do List App第四集：Core Data]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/to-do-list-app%E7%AC%AC%E5%9B%9B%E9%9B%86-core-data-4ba042157321?source=rss-cd4fbb6cf54------2</link>
            <guid isPermaLink="false">https://medium.com/p/4ba042157321</guid>
            <dc:creator><![CDATA[A-Tân]]></dc:creator>
            <pubDate>Fri, 16 Oct 2020 05:28:26 GMT</pubDate>
            <atom:updated>2020-10-16T05:28:26.281Z</atom:updated>
            <content:encoded><![CDATA[<p>這是延續之前的作品，用Core Data去重構To-Do List的App。前三集連結如下：</p><ul><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/table-view-%E7%9A%84delegate%E8%88%87data-source%E7%B7%B4%E7%BF%92-todo-list-app%E7%AC%AC%E4%B8%80%E9%9B%86-b387315fab2c">Table View 的Delegate與Data Source練習：Todo List App第一集</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/todo-list-app%E7%AC%AC%E4%BA%8C%E9%9B%86-%E7%B7%A8%E8%BC%AF-%E6%96%B0%E5%A2%9E%E8%88%87%E5%8B%95%E7%95%AB%E9%87%8D%E6%A7%8B-ede7ed618a81">Todo List App第二集：編輯、新增與動畫重構</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/to-do-list-app%E7%AC%AC%E4%B8%89%E9%9B%86-%E8%B3%87%E6%96%99%E5%84%B2%E5%AD%98-%E6%99%82%E9%96%93%E6%A8%99%E8%A8%98%E8%88%87%E4%BD%BF%E7%94%A8%E8%80%85%E9%80%9A%E7%9F%A5-7b59f8202294">To-Do List App第三集：資料儲存、時間標記與使用者通知</a></li></ul><p>UI的部分跟前一集一樣，沒有更動，就不放Gif了。</p><h3>從現有專案手動加入Core Data</h3><p>因為一開始創project的時候，沒有勾Core Data的選項，就要重新設定幾個地方（可以參考<a href="https://medium.com/programming-with-swift/add-core-data-to-existing-ios-project-b9e22a519d7b">這篇文章</a>）：</p><ul><li>AppDelegate要加幾個function，還有NSPersistentContainer的Extension，新增部份如下：</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/21703487afbc8b0b17bc2a4853f658ae/href">https://medium.com/media/21703487afbc8b0b17bc2a4853f658ae/href</a></iframe><ul><li>有用到Core Data的Controller都要import CoreData</li><li>新增一個Data Model的檔案，然後再進去設定資料型別的Entity：</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/762/1*R968D2ikrsFoe8V0OmagFg.png" /></figure><h3>基本設定：用Entity處理資料</h3><p>Core Data的原理是設定資料型別(Entity)，在資料更動的時候，呼叫persistentContainer的saveContext()，就能保存所做的更動。我這裡把Entity取名叫做 <strong>Todo</strong>，原本設定的struct 叫 <strong>TodoItem</strong>。</p><p>一開始我想保存自己原本定義的struct <strong>TodoItem</strong>，讓 <strong>Todo</strong>可以隨時轉型成<strong>TodoItem</strong>，後來發現這樣是行不通的，例如當要載入CoreData裡的資料時，沒有辦法把Core Data的Entity (<strong>Todo</strong>)轉型成自己定義的<strong>TodoItem</strong>，查了很久怎麼樣都不行，所以就必須全部改用Entity去做。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*5V0ZLUJy_vF3ENnrOYPCeA.png" /><figcaption>註解起來的部分，就是我原本想要做的</figcaption></figure><p>可以參考<a href="https://stackoverflow.com/questions/46174543/core-data-migration-could-not-cast-value-of-type-nsmanagedobject-mytype-to-m">這篇stack overflow</a>，作者自己找到不能這樣casting的原因：</p><blockquote>I’ll just jump in here and report my findings, so that someone else doesn’t have to spend the time. Noteworthy excerpt from “The Migration Process” (which you can google and bring you to an Apple site… I don’t post the link because Apple links regularly die:) “The migration process itself is in three stages. It uses a copy of the source and destination models in which the validation rules are disabled and the class of all entities is changed to NSManagedObject.” So yes, in these policies, you should return NSManagedObject and not your subclass.</blockquote><h3>Entity設定</h3><p>在 <strong>Todo</strong>這個Entity中，notificationType的部分原本在Struct裡是Enum，後來發現Core Data並沒有直接支援Enum，跟 Objective C 和 Swift 的差異有關，於是乾脆直接從Enum改成String。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*2xP4SiutJWxeW066ZXBrww.png" /></figure><p>所以到了設定通知類型的NotificationTypeTableViewController中，再去定義這串Array，然後再去調整 <strong>Todo</strong>中的notificationType：</p><pre>var NotificationTypes = [&quot;十分鐘前&quot;,  &quot;一小時前&quot;, &quot;兩小時前&quot;, &quot;一天前&quot;]</pre><p>除此之外，Entity裡面的每個Attributes，都要記得Codegen要設定成Class Definition，才會自動生成model class。這部分的詳細資訊，參考<a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E8%87%AA%E5%8B%95%E7%94%9F%E6%88%90-model-class-%E7%9A%84-core-data-46275d9f66e2">彼得潘這篇文章</a>。</p><h3>在Controller中使用Core Data</h3><p>首先當然是上面提到的，先在會用到資料儲存的地方 <strong>import CoreData</strong>。</p><p>接下來，定義一個變數叫container，如下：</p><pre><strong>var</strong> container = (UIApplication.shared.delegate <strong>as</strong>! AppDelegate).persistentContainer</pre><p>這是用Singleton的方式，去確保每個調用都會用到同一個Container。</p><p>確定好之後，接著就能用<a href="https://developer.apple.com/documentation/coredata/nspersistentcontainer/1640622-viewcontext">Container.viewContext</a>下去處理所有事情，這好像是Core Data後期出的一個工具，所有新增、更新、刪除等功能，都可以透過這個viewContext去解決。詳見下文。</p><h3>資料處理</h3><ul><li>讀取資料：使用在待辦事項清單的viewDidLoad中</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2237a7ae5863edd9e8070457ab3a007/href">https://medium.com/media/e2237a7ae5863edd9e8070457ab3a007/href</a></iframe><ul><li>新增：在按下新增按鈕時，分成「快速新增」跟「編輯詳細資料」，兩者都要呼叫viewContext去新增資料，再用saveContext去儲存更動</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/037cedf40eb4f68775b8daa4d8e0d0d7/href">https://medium.com/media/037cedf40eb4f68775b8daa4d8e0d0d7/href</a></iframe><ul><li>更新：在設定待辦事項詳細內容的DetailTableViewController中，可以直接在最後的computed property <strong>Todo</strong> 中 saveContext就好：</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d97a71e3e8835bf50079d855f5c2a24/href">https://medium.com/media/5d97a71e3e8835bf50079d855f5c2a24/href</a></iframe><ul><li>最後遇到一個小難題，在Todo List的頁面，可以調整待辦事項顯示的順序，但Core Data並不會跟著改動，所以我只好在viewWillDisappear的時候，為了保存順序，全部清掉重存（但這應該不是最好的做法，歡迎不吝指教）：</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a1377ab6c4a630901ddeffb134fcbb6/href">https://medium.com/media/9a1377ab6c4a630901ddeffb134fcbb6/href</a></iframe><h3>Github</h3><p>用Core Data處理資料的版本，tag為4.0（其實只是儲存資料的更動，好像不到大版本哈哈）。</p><p><a href="https://github.com/A-Tan5/To-do-list">A-Tan5/To-do-list</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4ba042157321" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/to-do-list-app%E7%AC%AC%E5%9B%9B%E9%9B%86-core-data-4ba042157321">To-Do List App第四集：Core Data</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[To-Do List App第三集：資料儲存、時間標記與使用者通知]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/to-do-list-app%E7%AC%AC%E4%B8%89%E9%9B%86-%E8%B3%87%E6%96%99%E5%84%B2%E5%AD%98-%E6%99%82%E9%96%93%E6%A8%99%E8%A8%98%E8%88%87%E4%BD%BF%E7%94%A8%E8%80%85%E9%80%9A%E7%9F%A5-7b59f8202294?source=rss-cd4fbb6cf54------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b59f8202294</guid>
            <dc:creator><![CDATA[A-Tân]]></dc:creator>
            <pubDate>Mon, 28 Sep 2020 14:36:01 GMT</pubDate>
            <atom:updated>2020-09-28T14:36:01.503Z</atom:updated>
            <content:encoded><![CDATA[<p>這是延續之前的作品，繼續完成To-Do List的App。前兩集連結如下：</p><ul><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/table-view-%E7%9A%84delegate%E8%88%87data-source%E7%B7%B4%E7%BF%92-todo-list-app%E7%AC%AC%E4%B8%80%E9%9B%86-b387315fab2c">Table View 的Delegate與Data Source練習：Todo List App第一集</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/todo-list-app%E7%AC%AC%E4%BA%8C%E9%9B%86-%E7%B7%A8%E8%BC%AF-%E6%96%B0%E5%A2%9E%E8%88%87%E5%8B%95%E7%95%AB%E9%87%8D%E6%A7%8B-ede7ed618a81">Todo List App第二集：編輯、新增與動畫重構</a></li></ul><h4>先上成果：</h4><ul><li>設定待辦事項日期：</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*6ZjtW3L_rIHvcbN8cON2uw.gif" /></figure><p>※在模擬器或實機中，只要地區設定為台灣，新版的Date Picker好像都會遇到如上圖那種顯示不完全的問題，疑似是iOS 14還沒解決的Bug</p><ul><li>設定時間並開啟通知</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*8CCq5cSXPUOnosTogLUE_g.gif" /></figure><ul><li>通知跳出</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*6wfopo8dclneFL6Td34KPQ.gif" /></figure><ul><li>新增待辦事項</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*2V8X2Kn4UvE6ftd5eifYkQ.gif" /></figure><ul><li>資料儲存</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*uncL11FF-SWh4SqVTvUvjw.gif" /></figure><h3>資料儲存：User Defaults</h3><p>User Default是比較初階的儲存方法，不過相較於其他方式，User Default的內容會持續佔用記憶體，所以在這裡只是練習而已，最後捨棄不用，程式碼如下：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/041de67aac188f1829f69974944f8ca7/href">https://medium.com/media/041de67aac188f1829f69974944f8ca7/href</a></iframe><h3>資料儲存：Document Directory</h3><p>這是最終採用的方法，原理是在App的檔案中新增一個檔案，紀錄好所需的資料，並讀取出來，這樣App關掉重開的話，資料也不會消失，就能保存住資料。程式碼如下：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/52530cea17331c22ca5790d6023497bb/href">https://medium.com/media/52530cea17331c22ca5790d6023497bb/href</a></iframe><h3>時間處理</h3><p>這次最大的更動，是讓待辦事項多了日期與時間，我設計的概念比較像iOS內建的提醒事項，也就是使用者可以僅設定日期、也可以設定日期與時間，但通知的部分一定要有設定時間才能開啟，並可以設定要多久之前通知。</p><p>因此，在資料的部分，struct裡面就要有兩個Date；而畫面中要呈現兩個不同的Date Picker，分別是「日期」跟「時間」，其中又要讓「時間」的Date的範圍跟著「日期」的Date移動，才能在選中的日期中的時間正確地跳出通知。</p><p>這部分折騰了蠻久，好像沒有一個內建的function可以取得同一時區中、同一日期的24小時，所以我自己寫了這一串，再導入「日期」的Date Picker的時間，去限縮「時間」的Date Picker 範圍：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d12df3ea6a1e54a288f978e089eed8a/href">https://medium.com/media/7d12df3ea6a1e54a288f978e089eed8a/href</a></iframe><p>簡單來說就是「日期」選中的Date，去減掉這個日期除以86400（等於24小時）的餘數，再減掉28800（時區調整），就是選中的日期當天的台灣時間凌晨0點，就能當作「時間」的Date Picker的最小值；最後再加上一天的86400，就能當作最大值，於是就限縮了「時間」這個Date值的範圍。</p><p>唯一的缺點就是無法跟著時區改變而調整，或是要更複雜地去定義每個時區的矯正值，如果有其他更好的方法，請大家不吝指教&gt;&lt;</p><p>這次是我第一次深入處理Date這種資料形式，可以參考<a href="https://medium.com/@mikru168/ios-%E9%97%9C%E6%96%BC%E8%99%95%E7%90%86%E6%97%A5%E6%9C%9F%E7%9A%84%E9%82%A3%E4%BA%9B%E4%BA%8B%E5%85%92-date-%E7%89%A9%E4%BB%B6-e46223a89cdd">這篇文章</a>，內容關於Date解釋地非常詳盡、全面。</p><h3>UI部分</h3><p>如上所述，在待辦事項的詳細資料頁面中，要讓使用者可以「僅選日期」、「選擇日期與時間」、「選擇關閉通知」，因此在這部分也花了蠻大的力氣，來處理各種Button、Label顯示與否的連動，礙於篇幅就不附上詳細的程式碼，成果如下：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*h70AO4Qmxx_qcXMSNAK9EA.gif" /></figure><p>這個詳細資料的頁面，是用Table View Controller + Static Cell完成的，一開始不知道怎麼下手，問了Peter之後，才知道蘋果官方的App Development with Swift中有很詳盡的範例，Peter有寫一篇文章，連結如下，可參考「4.9 Building Complex Input Screens」的部分：</p><p><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/app-development-with-swift-%E7%A8%8B%E5%BC%8F%E7%AF%84%E4%BE%8B%E9%87%8D%E9%BB%9E%E6%95%B4%E7%90%86-492121cb2f7e">App Development with Swift 程式範例重點整理</a></p><p>從這份範例中，學到最大的突破應該是把IndexPath跟switch 結合，先定義好要調整的目標（例如要調整是否隱藏的TimeDatePicker那行）的indexPath，先存在一個常數中，然後在heightForRowAt中，用switch去設條件，就可以決定在什麼情況下，表格中哪些特定的行要凸顯或隱藏。我的程式碼如下：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/622bfe2b899dc4bf605e91018f64891a/href">https://medium.com/media/622bfe2b899dc4bf605e91018f64891a/href</a></iframe><p>除此之外，還有個小細節，就是範例中都是用beginUpdates與endUpdates，來重整表格，呈現出來的效果會比tableView.reloadData還要好！</p><p>第一頁基本待辦事項的表格中，也分別對「僅有日期」、「有日期與時間」做出不同的UI處理，成果如下：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/383/1*qtfNcXWFsbMB_aksEVKvzw.png" /></figure><p>另一方面，我的待辦事項有Notes（附註）的部分，也就會牽涉到TextView的使用，我參考了<a href="https://itisjoe.gitbooks.io/swiftgo/content/uikit/uitextview.html">這篇文章</a>來寫TextView編輯時，按空白處收鍵盤的function，並用<a href="https://stackoverflow.com/questions/27652227/add-placeholder-text-inside-uitextview-in-swift">這篇文章</a>提供的解答，做出了TextView的Placeholder。</p><h3>Navigation Controller相關</h3><p>從第二頁待辦事項的詳細資料，傳送回第一頁基本的待辦事項表格，打算用unwind segue完成，結果遇到一個問題是，當初的navigation controller是用程式寫出來的，所以storyboard上就沒有顯示Navigation Item，變成也沒辦法去拉unwind segue。</p><p>求助Peter後，得到下面這篇文章，用Storyboard的Simulated Metrics去加上模擬的Navigation Bar，就可以做出想要的效果了！</p><p><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E5%88%A9%E7%94%A8-simulated-metrics-%E7%82%BA-controller-%E5%8A%A0%E4%B8%8A%E5%81%87%E7%9A%84-navigation-bar-b89a855484ea">利用 Simulated Metrics 為 controller 加上模擬的 navigation bar</a></p><p>另外，在實作的過程中，也有遇到一個問題：navigation bar會自動產生一個backBarButtonItem，但把那個backBarButtonItem加上action後卻完全無效。</p><p>後來找到<a href="https://stackoverflow.com/questions/30522230/how-to-override-self-navigationitem-backbarbuttonitem-action">這篇stack overflow</a>，確實沒辦法把那個backBarButtonItem加上我們想要的action，必須先把navigationItem.hidesBackButton設成true，再另外寫出我們要的按鈕，才能加上我們要的action。</p><h3>通知功能</h3><p>Peter在AppCoda上的這篇文章，把使用者通知推播的功能解釋地非常清楚：</p><p><a href="https://www.appcoda.com.tw/ios10-user-notifications/">結合 iOS 10 的 User Notifications：傳送米花兒的幸福打氣通知</a></p><p>礙於篇幅就不另外貼上程式碼。寫出來之後，一開始跑下去就跳出使用者通知的權限要求，看到的當下真是無比感動…..</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/378/1*AdDu7z0Z4iPiSiTF1jVjIw.png" /></figure><h3>其他</h3><ul><li>我設定Notification type的部分，是用enum做出來的，但在後續處理的過程中發現有點棘手，查了一下，發現果然有把enum當成Array來處理的方法：遵從 protocol CaseIterable。可以參考Peter這篇文章：</li></ul><p><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E8%AE%93-enum-%E8%AE%8A%E6%88%90-array-%E7%9A%84-caseiterable-allcases-swift-4-2-%E6%96%B0%E5%8A%9F%E8%83%BD-bc52f475e6c7">讓 enum 變成 array 的 CaseIterable &amp; allCases — Swift 4.2 新功能</a></p><ul><li>在調整待辦事項的頁面中，要調整的東西很多很繁瑣，我透過上述提到的蘋果官方範例，是用computed property直接去整理出最終的TodoItem，就不用另外寫零碎的function去處理，程式碼如下，也變得乾淨簡潔很多！</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/adf7be066871e7983f0739856e00a001/href">https://medium.com/media/adf7be066871e7983f0739856e00a001/href</a></iframe><h3>Github</h3><p>終於完成這個作品了！Github上的Tag為3.0，連結如下：</p><p><a href="https://github.com/A-Tan5/To-do-list">A-Tan5/To-do-list</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b59f8202294" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/to-do-list-app%E7%AC%AC%E4%B8%89%E9%9B%86-%E8%B3%87%E6%96%99%E5%84%B2%E5%AD%98-%E6%99%82%E9%96%93%E6%A8%99%E8%A8%98%E8%88%87%E4%BD%BF%E7%94%A8%E8%80%85%E9%80%9A%E7%9F%A5-7b59f8202294">To-Do List App第三集：資料儲存、時間標記與使用者通知</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[音樂App第一集]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/%E9%9F%B3%E6%A8%82app%E7%AC%AC%E4%B8%80%E9%9B%86-701d9524ffc2?source=rss-cd4fbb6cf54------2</link>
            <guid isPermaLink="false">https://medium.com/p/701d9524ffc2</guid>
            <dc:creator><![CDATA[A-Tân]]></dc:creator>
            <pubDate>Mon, 21 Sep 2020 04:57:21 GMT</pubDate>
            <atom:updated>2020-09-21T04:57:21.363Z</atom:updated>
            <content:encoded><![CDATA[<p>拖了好久的作業終於做出成果了（淚），先上結果：</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FbUNRj7eucck%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DbUNRj7eucck&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FbUNRj7eucck%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d85a3aeef38e15262019dfd88c770a17/href">https://medium.com/media/d85a3aeef38e15262019dfd88c770a17/href</a></iframe><h3>構想</h3><p>原先的要求只是利用AVPlayer，單純做一個音樂播放的App，但好像有點單調，我就自己加上複雜的功能：在選歌頁面可以利用segment control，選擇要播放的是本機的檔案，還是線上從itunes store API抓下來的30秒Preview。</p><p>於是成果如下，本機檔案用的是1989年黑名單工作室的《抓狂歌》專輯，是流行音樂史上開啟新台語歌運動的重要推手，線上音樂則用伍佰下去搜尋，讓API去跑出50首歌曲的Preview。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/335/1*SdIjdmrtyMZK7G51LIeVuw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/331/1*YNr9H5pGU7bgMuli9nZQOA.png" /></figure><h3>純程式碼刻畫面：Segment Control + Table View</h3><p>選歌的畫面，用Segment Control去決定Table View呈現的內容，是參考<a href="https://www.youtube.com/watch?v=OF69qnOFZro">「Lets Build That App」的影片</a>的做法，完全用程式碼直接寫出來的。順帶一提他的影片很平易近人，也有一些直播的影片，在焦慮的時候很適合拿來像一般直播一樣慢慢看，哈哈！</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c13ee40a8f21a6b6ace180f3f39a6d7/href">https://medium.com/media/9c13ee40a8f21a6b6ace180f3f39a6d7/href</a></iframe><p>表格中TableViewCell的部份，包括Cell裡面的Label，也都是純程式碼產生的，因此除了在cellForRowAt裡面的dequeueReusableCell(withIdentifier:for:)之外，還要在View Controller的viewDidLoad裡面對TableView做register(_:forCellReuseIdentifier:)。</p><p>而這個TableViewCell因為是從程式產生的，沒有掛在nib、xib或storyboard裡，所以要寫Label等元件，並不是寫在awakeFromNib()中，而是要override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?)，謝謝Peter提醒這一點！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/1*OtblRBYIq1bXSie8r9Voxw.png" /><figcaption>在UITableViewCell中用程式碼生出兩個UILabel</figcaption></figure><h3>基本播放功能</h3><p>可以參考彼得潘的這幾篇文章，礙於篇幅就不贅述了：</p><ul><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E5%88%A9%E7%94%A8-viewdidload-%E9%BB%9E%E4%B8%80%E9%A6%96%E6%AD%8C-%E6%92%AD%E6%94%BE%E4%B8%80%E6%AC%A1pl-bfe59e9e22c">利用 AVPlayer 播放音樂音效</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E8%A8%AD%E5%AE%9A-background-mode-%E5%9C%A8%E8%83%8C%E6%99%AF%E6%92%AD%E6%94%BE%E9%9F%B3%E6%A8%82-9bab5db75cc9">設定 Background Mode 在背景播放音樂</a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TrDlhiBv6QE6oimooHXmZQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wsliHUEqScBGjSMrQoKY5A.png" /><figcaption>播放器介面長這樣，比較沒有特別去調整、美化UI元件</figcaption></figure><h3>進階功能</h3><p>首先是進度條的部分，我參考了<a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/3-%E4%BF%AE%E7%85%89%E7%A8%8B%E5%BC%8F%E7%9A%84%E5%BF%83%E9%85%B8-f475f84c3090">學長姐的作品</a>，用AVPlayer的addPeriodicTimeObserver，去更新歌曲的進度條，與左右兩個顯示「播放時間」和「剩餘時間」的Label：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd4257b06a0cf5ad9ccb82e81dd5a9e6/href">https://medium.com/media/fd4257b06a0cf5ad9ccb82e81dd5a9e6/href</a></iframe><p>而「隨機播放」和「重複播放」，則是自己練習用enum去定義出不同的case，再去跟著調整播放的內容，細節就不另外貼上來了。</p><p>其中重複播放的部分，會影響到的是歌曲結束的時候，是要接下一首歌，還是把player倒回開頭（seek），還是在最後一首結束後要停止播放，這裡就可以用AVPlayer內建的Notification 「AVPlayerItemDidPlayToEndTime」來處理，寫在viewDidLoad中：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/934b70a69a577644bd764325e835bcd4/href">https://medium.com/media/934b70a69a577644bd764325e835bcd4/href</a></iframe><p>最後，在播放時遇到一個問題，就是當使用者跳回前一頁，再點另一首歌曲播放的時候，會變成有兩個AVPlayer同時播放；但我又不想在使用者跳回前一頁時，就直接呼叫viewwilldisappear把player暫停，因此跟Peter討論過後，是採取「另外寫一個controller，再去呼叫裡面的AVplayer」的做法，如下面兩張圖：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*foZ_206mfefaj64KbQ9fGQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/491/1*k8vmdwqWn5ms-esvjEyy7Q.png" /></figure><h3>傳送、下載資料</h3><p>這次刻意在音樂App中練習網路下載資料的功能，果然就學到了新東西，又再多練習了一次，非常值得！</p><p>我想要讓資料從歌曲列表的頁面，傳送到音樂播放器的頁面時，可以單純一點，因此我的做法是把本機或線上的音樂，都歸類在同一個struct “music”裡。</p><p>其中，本機的資料相對單純，只要記載它的曲名、演唱者，實際拿來播放的playeritem就寫一個function去抓本機URL就好；而線上的資料就又多了專輯圖片的URL，playitem的URL也要改成網址資料。</p><p>因此，我直接在struct music裡面去分開定義兩種URL，並把變數都設成optional，這樣在傳送到播放器時，只要在播放器的地方偵測「線上音樂有的、本機音樂沒有的」變數是否為nil，就能知道這首歌是哪裡來的，再去執行相對應的程式碼。這樣一來在傳送資料的prepare function中，只要單純地傳入點選的歌曲和全部的歌單就好，不用多做太多篩選。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/661/1*z4lt77yfd9ifE6dVAkUGpw.png" /><figcaption>trackName與artistName是本機與線上音樂共有，previewURL和artworkUrl100則是線上音樂透過JSON解析才會有的，URL也分成兩種方式獲取。</figcaption></figure><p>（上述的做法是我自己粗淺的想法，可能不是很完美，歡迎指教！）</p><p>而下載資料的部分，我在歌曲列表頁面的viewDidLoad中，就先抓了完整的歌單，至於圖片的部分則是要到音樂播放器的頁面中，才去載入圖片，才不會導致圖片還沒抓好，下一頁的viewDidLoad就自己先跑好了。（謝謝Peter的提醒！）</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*V7i6bDKGHFo9lnK0m2FSYQ.png" /><figcaption>抓圖片的function，要把回傳的資料包在closure的參數中，並加上@escaping，才能正確地把圖片撈出來</figcaption></figure><h3>下一步</h3><p>之後如果有機會繼續做這個App，希望能加上MPNowPlayingInfoCenter的運用，讓這個音樂播放器能在通知欄中顯示、更新與控制。</p><p><a href="https://developer.apple.com/documentation/mediaplayer/mpnowplayinginfocenter">Apple Developer Documentation</a></p><p>除此之外，也希望能模仿真正的音樂App，在回到第一頁歌曲列表的同時，可以在畫面下方出現浮動的播放器，並能夠更新、控制。</p><p><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E5%88%A9%E7%94%A8-scroll-view-%E7%9A%84-frame-layout-guide-%E8%A8%AD%E8%A8%88%E6%B5%AE%E5%8B%95%E5%85%83%E4%BB%B6-805f935e1877">利用 scroll view 的 Frame Layout Guide 設計浮動元件</a></p><h3>Github</h3><p>這次的版本tag為1.0。</p><p><a href="https://github.com/A-Tan5/MusicPlayer">A-Tan5/MusicPlayer</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=701d9524ffc2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E9%9F%B3%E6%A8%82app%E7%AC%AC%E4%B8%80%E9%9B%86-701d9524ffc2">音樂App第一集</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SheetDB與CRUD實作：簡易登入系統App 第一集]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/sheetdb%E8%88%87crud%E5%AF%A6%E4%BD%9C-%E7%B0%A1%E6%98%93%E7%99%BB%E5%85%A5%E7%B3%BB%E7%B5%B1app-%E7%AC%AC%E4%B8%80%E9%9B%86-67b7e928cd94?source=rss-cd4fbb6cf54------2</link>
            <guid isPermaLink="false">https://medium.com/p/67b7e928cd94</guid>
            <dc:creator><![CDATA[A-Tân]]></dc:creator>
            <pubDate>Thu, 03 Sep 2020 14:13:57 GMT</pubDate>
            <atom:updated>2020-10-05T12:35:01.501Z</atom:updated>
            <content:encoded><![CDATA[<p>這是參考<a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/%E8%A9%A6%E8%A9%A6%E7%B0%A1%E5%96%AE%E5%A5%BD%E7%94%A8%E7%9A%84sheetdb-8c588b9b4eab">學長的作品</a>而成的。先上結果：</p><p><strong>登入：</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*FbchaB8kw89eOLK3eSE9Sg.gif" /></figure><p><strong>登出：</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*VlhNTIFiRq9hLQ9hyeGxTw.gif" /></figure><p><strong>修改帳號</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*CAgoNCT8NRYxwwF4jubyRA.gif" /></figure><p><strong>修改密碼</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*Xn7otfKEOs6Kn4ZtJ9ATNA.gif" /></figure><p><strong>刪除帳號</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*MXAuFP3HTXmbMeOuzO74qg.gif" /></figure><p><strong>註冊新帳號</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*GR1LX4D-0PGExnaIOzOvag.gif" /></figure><h3>UI部分</h3><p>其實這次主要放在網路的功能上，UI的部分完全都是照xcode預設的，沒有特別去調整；不過，在storyboard中（如下圖），看似複雜的頁面其實都是重複使用的，只要在頁面之間傳送login的狀態、是要登入還是註冊等資料，再設定text field的isHidden、button的setTitle就好。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/777/1*Bhccu0uZoPKpXnGwaVvItg.png" /></figure><h3>頁面間傳值</h3><p>在這個專案中，我刻意用了許多種頁面間傳值的方法，當作練習。在登入 / 註冊帳號的頁面間，用的是prepare+delegate：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cf7825b8a7fc24306ad631cd324602b6/href">https://medium.com/media/cf7825b8a7fc24306ad631cd324602b6/href</a></iframe><p>而從帳號設定的頁面，到改帳號 / 密碼，用的是單純的prepare：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30d778692245cddc1670727438c878e1/href">https://medium.com/media/30d778692245cddc1670727438c878e1/href</a></iframe><p>至於編輯結束後跳回前面的頁面，也就是往回傳值的部分，上述登入 / 註冊畫面，因為只要往前跳一頁，所以是直接用navigationController的popViewController做的；而另一半處理修改帳號、密碼，再跳回首頁的部分，就要用到更細緻的popToViewController，不過這個方法中，其實不用設delegate也可以直接傳值！</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f500d532604fc1597d78d80f8c1c8fa6/href">https://medium.com/media/f500d532604fc1597d78d80f8c1c8fa6/href</a></iframe><h3>網路部分</h3><p>這次使用的是相對簡單、好上手的SheetDB，在Create、Update和Delete的寫法上，其實大同小異，都是用URLSession.shared.uploadTask去處理，並接收API回傳的值去驗證是否上傳成功，只是中間的httpMethod不一樣而已。</p><p>而在實際面的登入、註冊、修改帳密與刪除等功能中，只有登入的部分需要從API抓下使用者的帳密來做比對，也就是只有登入的部分會寫到CRUD中的Read，比對成功之後，再上傳到資料庫更新Login_Status。</p><p>因此以登入為例，程式碼如下（寫的有點亂QQ）：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b4aabab6d24e970c4be10815e2cfbb0e/href">https://medium.com/media/b4aabab6d24e970c4be10815e2cfbb0e/href</a></iframe><p>要注意的是，SheetDB接受的上傳格式，是把資料在{data:[]}裡面，比方說我的資料格式是叫AccountPassword，上傳的時候就要弄成{data:[AccountPassword]}（宣告如下），才能被SheetDB接受。</p><pre><strong>struct</strong> AccPasUpload : Codable, Equatable{</pre><pre><strong>var</strong> data: [AccountPassword]</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_mpQxT2Ok3Xoc7Tm7M-1Mg.png" /><figcaption>API文件中的資料格式</figcaption></figure><h3>未來內容</h3><p>未來希望新增以下的功能：</p><ul><li>參考蘋果官方的寫法，把網路相關實作的function統一放進model controller</li><li>用Document directory或其他適合的方式存帳號密碼</li><li>用firebase實現註冊、登入</li><li>UI進階調整</li></ul><h3>Github</h3><p>因為這只是第一版的內容，所以tag設為1.0。</p><p><a href="https://github.com/A-Tan5/SheetDB-APLogin">A-Tan5/SheetDB-APLogin</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=67b7e928cd94" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/sheetdb%E8%88%87crud%E5%AF%A6%E4%BD%9C-%E7%B0%A1%E6%98%93%E7%99%BB%E5%85%A5%E7%B3%BB%E7%B5%B1app-%E7%AC%AC%E4%B8%80%E9%9B%86-67b7e928cd94">SheetDB與CRUD實作：簡易登入系統App 第一集</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[天氣App 第二集：API抓圖片、Table View嵌入Collection View Cell]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/%E5%A4%A9%E6%B0%A3app-%E7%AC%AC%E4%BA%8C%E9%9B%86-api%E6%8A%93%E5%9C%96%E7%89%87-table-view%E5%B5%8C%E5%85%A5collection-view-cell-fa63215b788f?source=rss-cd4fbb6cf54------2</link>
            <guid isPermaLink="false">https://medium.com/p/fa63215b788f</guid>
            <dc:creator><![CDATA[A-Tân]]></dc:creator>
            <pubDate>Wed, 19 Aug 2020 02:17:20 GMT</pubDate>
            <atom:updated>2020-08-19T02:17:20.537Z</atom:updated>
            <content:encoded><![CDATA[<p>延續<a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/collection-view-%E8%88%87-api%E4%B8%B2%E6%8E%A5%E7%B7%B4%E7%BF%92-%E5%A4%A9%E6%B0%A3app-%E7%AC%AC%E4%B8%80%E9%9B%86-4bbf6ee3ab0c">上一篇文章</a>，這篇繼續做同一個天氣App。</p><p>先上成果：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*siM5OVuZO92mBmSpaHSwqg.gif" /></figure><p>這個版本主要是在原來的Collection View Cell（就是左右滑的分頁）中，嵌入一個Table View，並在裡面顯示接下來每個小時的天氣狀況、溫度、濕度等資料。</p><h3>在Collection View裡面的Table View</h3><p>第一步就是很簡單的在Story Board中，把一個Table View拖進Collection View Cell的Content View裡面：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/653/1*mUUH3VWYH_XdJPhKRZbv4Q.png" /></figure><p>拖進去之後，這個Table View就像是一般的元件一樣，可以拉constraint、調大小等等。</p><p>接著，因為Table View Cell的內容，是隨著左右滑動的Collection View Cell來載入，例如台北市的頁面就要載入台北市的每小時預報，台中市的就要載入台中市的，因此，決定Table View內容的Delegate跟Data Source，就要設定成Collection View Cell的檔案。這個步驟要用Storyboard拉線：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/440/1*G0SZl654-lLGX6hZUbz7kQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*0OYUSTMFSANxeaMv-tWh8Q.png" /><figcaption>要確定右邊dataSource跟delegate兩個都有點選（顯示圓點）</figcaption></figure><p>另一方面，Collection View Cell的檔案，也應該要遵從dataSource跟delegate兩個protocol，因此要在最開頭宣告的地方加上兩個protocol，如下：</p><pre>class WeatherCollectionViewCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource {</pre><p>還有一個重點，因為我的天氣資料是包在一串包含各個城市的Array中，因此要取得Table View Cell的內容，需要知道Collection View現在滑到第幾個cell了，也就是要把 CollectionViewController裡面cellForItemAt function的indexPath，傳到CollectionViewCell來。</p><p>因此我在CollectionViewController檔案中，最最外層的地方宣告一個變數indexforCollcell，再在cellForItemAt function中，讓indexforCollcell等於當下的indexPath.row。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/1*hgCm9thfvwc0f21BCIJD1Q.png" /><figcaption>在class宣告之外，設定一個變數</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/711/1*yH4ndnxtUWdUdiTT9FphOA.png" /><figcaption>讓每次Collection View滑動時，更新該變數等於indexPath.row</figcaption></figure><p>接下來就可以在Collection View Cell中設定內容了！</p><h3>獲取表格資料</h3><p>一樣是要先另外拉一個Table View Cell的檔案，然後把表格中的元件拉Outlet過去，然後再用cellForRowAt來載入表格：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6345ec95e6e7368e294bdb24dd25563/href">https://medium.com/media/b6345ec95e6e7368e294bdb24dd25563/href</a></iframe><ul><li>Open Weather的 <a href="https://openweathermap.org/api/one-call-api">One Call API</a> 提供的時間格式是Unix, UTC，因此可以直接套進timeIntervalSince1970格式的Date中，再去用DateFormatter處理成本地時區的資料。</li><li>API有提供天氣狀況的圖片，可參考<a href="https://openweathermap.org/weather-conditions#Weather-Condition-Codes-2">這個連結</a>，而雖然API提供的網址是http開頭，導致一開始被擋住沒辦法連上，但我在字尾硬加上s，一樣連得上而且載得了圖片XD，就順利載入天氣狀況的icon了</li></ul><h3>Collection View的間隔問題</h3><p>上一篇文章中，提到Collection View在左右滑動時，會多出一條縫縫，後來研究了很久，要用<a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E5%88%A9%E7%94%A8-flow-layout-%E7%9A%84-collection-view-%E8%A3%BD%E4%BD%9C%E7%85%A7%E7%89%87%E7%89%86-e31dad55a7ac">彼得潘這篇詳細的教學</a>的方法去寫，在CollectionViewController的viewDidLoad中設定Collection View Cell的大小：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8013615009ec3510a73a65cf0bf7f957/href">https://medium.com/media/8013615009ec3510a73a65cf0bf7f957/href</a></iframe><p>而左右滑動時的縫，其實是minimum Line Spacing造成的，我自己是想像成對xcode來說，Collection View的滑動是行(line)跟行之間的切換，而因此水平滑動的時候，第一頁跟第二頁是不同行，所以要把行距（line spacing）設定成0才不會出現那條縫。反過來說，Interitem Spacing則是同一行中不同元件的距離，所以不會是滑動時出現的間距。</p><h3>下一步</h3><p>下一個版本，希望能做出搭配Collection View滑動的Page Control，優化整體UI，讓使用者可以新增城市，並跑出該城市的天氣資料，以及取得使用者當下的地理位置，去載入當地的天氣與預測。</p><p>除此之外最重要的是，要優化網路載入的過程，現階段連線的時候所有Label都變成0，實在很不理想，而且滑到右邊又滑回來左邊後，Table View裡面的資料也會重疊到。現在API還是一直掛掉，要了解並解決為什麼會打破每分鐘60次的限制。</p><h3>Github</h3><p>因為邊寫文章邊調整一些註解 XD，所以這個版本最後的tag是2.1。</p><p><a href="https://github.com/A-Tan5/WeatherApp">A-Tan5/WeatherApp</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fa63215b788f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E5%A4%A9%E6%B0%A3app-%E7%AC%AC%E4%BA%8C%E9%9B%86-api%E6%8A%93%E5%9C%96%E7%89%87-table-view%E5%B5%8C%E5%85%A5collection-view-cell-fa63215b788f">天氣App 第二集：API抓圖片、Table View嵌入Collection View Cell</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Collection View 與 API串接練習：天氣App 第一集]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/collection-view-%E8%88%87-api%E4%B8%B2%E6%8E%A5%E7%B7%B4%E7%BF%92-%E5%A4%A9%E6%B0%A3app-%E7%AC%AC%E4%B8%80%E9%9B%86-4bbf6ee3ab0c?source=rss-cd4fbb6cf54------2</link>
            <guid isPermaLink="false">https://medium.com/p/4bbf6ee3ab0c</guid>
            <dc:creator><![CDATA[A-Tân]]></dc:creator>
            <pubDate>Tue, 18 Aug 2020 07:39:33 GMT</pubDate>
            <atom:updated>2020-08-18T07:39:33.004Z</atom:updated>
            <content:encoded><![CDATA[<p>這是一系列作品的第一集，先上成果：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*yHuvFzwSngfSS85RKMHI7w.gif" /></figure><h3>API使用：OpenWeather</h3><p>我使用的是OpenWeather的”<a href="https://openweathermap.org/api/one-call-api">One Call API</a>”，包含現在天氣、分鐘預測、每小時預測、七天預測等資料，不過免費的只能接受60 calls / minute，結果在做天氣app的過程中，一直不小心造成太多次Call，一直被鎖住，一旦被鎖住就要花好幾個小時等他解鎖，因此推薦可以多註冊幾隻帳號去使用。</p><p>API的JSON格式，可以從<a href="http://jsoneditoronline.org/">JSON Editor Online</a>去整理出來，畫面如下：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1aiQwVQ86H1hsfcQzxAQrw.png" /></figure><p>於是照著右側整理出來的API的JSON格式，建立Weather Object和不同城市的資料Array：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e33a6fa45c28a182e6d09cc27adf2ab9/href">https://medium.com/media/e33a6fa45c28a182e6d09cc27adf2ab9/href</a></iframe><p>我整理出來的做法是，只要是Dictionary，就用Struct再去指定裡面的變數，如果遇到Array，就用[]包住一個Struct的型別（這個型別可以自己命名），再另外去定義那個Struct型別裡面的變數，這樣只要Data type跟詳細的變數名稱都有對到，就可以成功解碼JSON了！</p><p>然後在Collection View的cellForItemAt function裡呼叫API並解碼：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a2082783df00b1a4184cfd3653b0959/href">https://medium.com/media/5a2082783df00b1a4184cfd3653b0959/href</a></iframe><p><a href="https://openweathermap.org/api/one-call-api">API文件</a>裡有提到，網址欄除了輸入經緯度資料，還可以指定度量衡單位、天氣狀況（Description）的語言等，因此URL的String裡面，就有用<strong><em>units=metric</em></strong>、<strong><em>lang=zh_tw</em></strong>來指定這些資料格式；除此之外，因為不需要每分鐘的預報資料，就加上一段<strong><em>exclude=minutely</em></strong>。</p><p>URL最後的”appid=”，後面就是接上註冊會員之後取得的API Key，但是我範例裡面用的已經暫時失效了，哈哈。</p><h3>載入API資料</h3><p>接著就是把從API取得的資料，載入Collection View的Cell裡，一樣是寫在cellForItemAt function裡：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/77d9438e501dfa7719731b179b987102/href">https://medium.com/media/77d9438e501dfa7719731b179b987102/href</a></iframe><p>我有從Pinterest抓幾張圖片，當作天氣狀況的背景圖片，並用程式碼偵測API裡面main的資料，去跑出對應的圖片；但在做Gif圖的時候剛好每個城市都是多雲的XD，就只出現clouds的image。</p><h3>下一步</h3><p>這個作品只是很粗淺的第一集，下一步會把Table View拉進 Collection View的Cell中，透過collectionViewCell去控制表格，讓畫面下方有一個可以滑動的表格，顯示接下來每個小時的預報。</p><p>另外，Collection View的Cell大小有點超出螢幕的邊界，滑到第三張高雄市的時候就變得很明顯，我猜應該也是因為這樣，畫面會同時出現兩個Cell，導致不停的執行cellForItemAt向API呼叫，才讓API每分鐘只能呼叫60次的限制爆掉。</p><p>最後，如果順利的話，希望可以做出讓使用者新增、自訂、刪除城市的功能，並且希望可以取得使用者所在位置，讓使用者可以透過這個APP知道現在位置的天氣預報。</p><h3>Github</h3><p>這個版本的Tag為1.0，期待之後的版本！</p><p><a href="https://github.com/A-Tan5/WeatherApp">A-Tan5/WeatherApp</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4bbf6ee3ab0c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/collection-view-%E8%88%87-api%E4%B8%B2%E6%8E%A5%E7%B7%B4%E7%BF%92-%E5%A4%A9%E6%B0%A3app-%E7%AC%AC%E4%B8%80%E9%9B%86-4bbf6ee3ab0c">Collection View 與 API串接練習：天氣App 第一集</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Todo List App第二集：編輯、新增與動畫重構]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/todo-list-app%E7%AC%AC%E4%BA%8C%E9%9B%86-%E7%B7%A8%E8%BC%AF-%E6%96%B0%E5%A2%9E%E8%88%87%E5%8B%95%E7%95%AB%E9%87%8D%E6%A7%8B-ede7ed618a81?source=rss-cd4fbb6cf54------2</link>
            <guid isPermaLink="false">https://medium.com/p/ede7ed618a81</guid>
            <dc:creator><![CDATA[A-Tân]]></dc:creator>
            <pubDate>Wed, 12 Aug 2020 14:44:47 GMT</pubDate>
            <atom:updated>2020-08-12T14:44:47.817Z</atom:updated>
            <content:encoded><![CDATA[<p>延續<a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/table-view-%E7%9A%84delegate%E8%88%87data-source%E7%B7%B4%E7%BF%92-todo-list-app%E7%AC%AC%E4%B8%80%E9%9B%86-b387315fab2c">上一篇</a>貼文，這次繼續來處理Todo List App，主要調整了以下幾個部分：開頭畫面的動畫微調、Todo List的新增，和編輯現有的待辦事項。</p><p>先上成果：</p><ul><li>開頭動畫部分</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*YlYEYWj_mrgJR6KPBVFoBQ.gif" /></figure><ul><li>Todo List的新增</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*PK8XqjuGS-BUIDAGHPw2EA.gif" /></figure><ul><li>編輯待辦事項</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*mnU_F8-A5Eb0G4NIPMF2uw.gif" /></figure><h3>開頭動畫重構</h3><p>原本在上一篇中，開頭動畫是用animate(withDuration:animations:)去做，但其實蘋果已經不建議用這個方法做動畫，後來有個新的方法UIViewPropertyAnimator，可以參考<a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/ios-app-%E5%8B%95%E7%95%AB%E5%BB%BA%E8%AD%B0%E4%BD%BF%E7%94%A8-uiviewpropertyanimator-%E5%88%A5%E5%86%8D%E7%94%A8-uiview-animate-a868c579abb5">這篇</a>。</p><p>因此我就用UIViewPropertyAnimator重構，並參考這個<a href="https://www.youtube.com/watch?v=cHwdvH3cStE">youtube影片</a>的方式做出來，先定義好一個Animator，再另外去加Animations跟Conpletion：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a73bea63e901514e6eaf3e6802b35cb/href">https://medium.com/media/8a73bea63e901514e6eaf3e6802b35cb/href</a></iframe><p>（從影片裡面學到很多東西，原來youtube也是一大寶庫…）</p><h3>Todo List新增</h3><p>原先的構想是要在table view的末端加上一個cell，在cell裡面加button，再從button新增，但這樣一來變得超複雜，而且捲動table view畫面時會重複出現最後一個cell的button。</p><p>後來找到彼得潘<a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E5%A6%82%E4%BD%95%E5%BE%9E-interface-builder-%E5%8A%A0%E5%85%A5-table-view-%E7%9A%84-header-footer-4dd2e0c76171">這篇文章</a>，才想起來表格可以自己加header和footer，就算是dynamic也不例外，就像音樂播放app上方的專輯圖案和播放按鈕，就是用header做成的。</p><p>因此我就在main.storyboard拉了一個view當footer，在裡面放上button，再拖到ViewController當IBAction，裡面加上可以打字的UIAlertController：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e7cf1f32b7991eb73ad9dd7815c0af0/href">https://medium.com/media/8e7cf1f32b7991eb73ad9dd7815c0af0/href</a></iframe><p>UIAlertController裡面加上textfield的方法，可以參考<a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E9%A1%AF%E7%A4%BA%E8%AD%A6%E5%91%8A%E8%A8%8A%E6%81%AF%E5%92%8C%E9%81%B8%E5%96%AE%E7%9A%84-uialertcontroller-a7548b606eea">這篇</a>。而要進一步處理使用者輸入在textfield的內容，一定要把程式碼寫在UIAlertAction裡面，這樣使用者按下去才會觸發後續的動作。</p><h3>編輯待辦事項</h3><p>待辦事項的編輯，與新增待辦事項的方式大同小異，都是跳出UIAlertController然後打字，只是觸發的方式變成按表格任意一個cell，因此是寫在UITableViewDelegate定義的didSelectRowAt方法中。</p><p>程式碼如下：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c54a63e4dad9f47a1513ae29c79d9972/href">https://medium.com/media/c54a63e4dad9f47a1513ae29c79d9972/href</a></iframe><p>之後如果學到更進階的功能，說不定會再回來繼續精進這個 Todo List的專案！</p><h3>Github</h3><p>因為跟上一篇是同一個作品，所以Github上傳時，有用到不同的tag去區分版本，所以這次修改後的版本是2.0。詳情可以參考彼得潘的<a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E5%BE%9E-xcode-%E5%88%A9%E7%94%A8-git-tag-%E8%A8%98%E9%8C%84-app-%E7%89%88%E6%9C%AC-35452a97819a">這篇教學</a>。</p><p><a href="https://github.com/A-Tan5/To-do-list/tree/2.0">A-Tan5/To-do-list</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ede7ed618a81" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/todo-list-app%E7%AC%AC%E4%BA%8C%E9%9B%86-%E7%B7%A8%E8%BC%AF-%E6%96%B0%E5%A2%9E%E8%88%87%E5%8B%95%E7%95%AB%E9%87%8D%E6%A7%8B-ede7ed618a81">Todo List App第二集：編輯、新增與動畫重構</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>