<?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 Monica 的 iOS APP 程式設計入門 on Medium]]></title>
        <description><![CDATA[Stories by Monica 的 iOS APP 程式設計入門 on Medium]]></description>
        <link>https://medium.com/@shelly1123?source=rss-74745a788318------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*OG5B95huaNYHQ3OQXsapaA.jpeg</url>
            <title>Stories by Monica 的 iOS APP 程式設計入門 on Medium</title>
            <link>https://medium.com/@shelly1123?source=rss-74745a788318------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 17:53:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shelly1123/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[日本娛樂新聞APP (網路下載資料)]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/%E6%97%A5%E6%9C%AC%E5%A8%9B%E6%A8%82%E6%96%B0%E8%81%9Eapp-%E4%B8%8B%E8%BC%89%E8%B3%87%E6%96%99-dbd2a21af099?source=rss-74745a788318------2</link>
            <guid isPermaLink="false">https://medium.com/p/dbd2a21af099</guid>
            <category><![CDATA[解析json]]></category>
            <category><![CDATA[串接第三方api]]></category>
            <category><![CDATA[轉換成自訂型別顯示]]></category>
            <dc:creator><![CDATA[Monica 的 iOS APP 程式設計入門]]></dc:creator>
            <pubDate>Tue, 21 Sep 2021 14:11:29 GMT</pubDate>
            <atom:updated>2021-09-22T06:53:48.323Z</atom:updated>
            <content:encoded><![CDATA[<h4>串接第 3 方 API, 解析 JSON 資料, 轉換成自訂型別顯示</h4><blockquote><strong>成品：最近事情不少這堂課補得斷斷續續，終於可以來寫網誌囉～</strong></blockquote><p>▲起先貪心構思很多，最後回歸基本功能練習但網誌仍寫很久= =<br>▲ 3 個物件 AutoLayout / StackView 還和我鬧不合？還好強者我朋友<br>▲還沒體會到用 do catch print error 美好的你請跟上時代腳步<br>▲無限輪迴聽上課影片到很厭世…<br>▲參考來源：<a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84%E8%A9%A6%E7%85%89-%E5%8B%87%E8%80%85%E7%9A%84-100-%E9%81%93-swift-ios-app-%E8%AC%8E%E9%A1%8C/19-%E4%B8%B2%E6%8E%A5%E7%AC%AC%E4%B8%89%E6%96%B9-api-%E8%A7%A3%E6%9E%90-json-%E8%B3%87%E6%96%99%E9%A1%AF%E7%A4%BA-f1556e84daec"><strong>彼得</strong></a>與<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/24-%E8%A3%BD%E4%BD%9C%E8%87%BA%E7%81%A3%E9%A0%AD%E6%A2%9D%E6%96%B0%E8%81%9E-app-%E4%B8%B2%E6%8E%A5-news-api-fa87fe9a635f"><strong>同學</strong></a>(但他寫好複雜我幾乎沒有參考到 :P)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/289/1*668V2CRaTIjqpgpu2FFKzA.gif" /></figure><blockquote><strong>前置作業</strong></blockquote><p>▲ TableViewController + Navigation + ViewController (WebView)<br>▲新增 Swift File * 1 + Cocoa Touch Class * 3 ，取名後各自配好 + Cell ID<br>▲用客製 cell 才不會有滑動後才顯示圖片的不一致狀況產生<br>▲加 Label 與 ImageView 並用 AutoLayout / StackView<br>▲ Title Label 記得 Line 調 0 使其可變為多行<br>▲ cell 拉 segue 至 ViewController，並加 WebView 於 ViewController</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iypJLgd9FHqfAO0qhDDNXg.png" /></figure><blockquote><strong>依 JSON 對應自訂型別 (解碼 / 編碼)，遵從 protocol Codable 的型別</strong></blockquote><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/%E7%AC%AC%E4%B8%89%E6%96%B9-api-%E6%95%B4%E7%90%86-115868a56959"><strong>第 3 方 API 連結整理</strong></a>，從中取得 JSON API，<a href="https://newsapi.org/"><strong>新聞</strong></a>請選這<br>▲自訂型別：SearchResponse<br>▲遵從 Codable<strong> </strong>( 解碼：Decodable protocol + 編碼：Encodable protocol)<br>▲型別遵從，其屬性也要遵從如：String, Int, Date, URL, Array, Data 等<br>▲屬性名稱及其型別轉換不可打錯，想改名稱須用其他方法如：enum<br>▲ url 含特別字元，所以要用 String 特別處理才能轉成 URL <br>▲ JSON 物件不一定有的 key / 某 key 對應 value 可能為 null 時<br> ➞要宣告為 optional<br>▲更詳細解說請參考<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-swift-4-%E7%9A%84-jsondecoder-%E5%92%8C-codable-%E8%A7%A3%E6%9E%90-json-%E5%92%8C%E7%94%9F%E6%88%90%E8%87%AA%E8%A8%82%E5%9E%8B%E5%88%A5%E8%B3%87%E6%96%99-ee793622629e"><strong>彼得教學</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/748/1*9bip9eDNvOD8fXRb8fFmFw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/1*DJOy3y8frPIPV9h_8sS9JA.png" /></figure><blockquote><strong>NewsTableViewController.swift</strong></blockquote><p>▲ 生成 1 個 空白 Array，為顯示於表格上</p><pre><strong>var</strong> articles = [Article]()</pre><p>▲cell 數量 = Array 數量</p><pre><strong>override</strong> <strong>func</strong> tableView(<strong>_</strong> tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {<br><strong>return</strong> <strong>articles.count</strong><br>}</pre><blockquote><strong>NewsTableViewCell.swift：拉 Outlet</strong></blockquote><pre><strong>@IBOutlet</strong> <strong>weak</strong> <strong>var</strong> titleLabel: UILabel!<br><strong>@IBOutlet</strong> <strong>weak</strong> <strong>var</strong> dateLabel: UILabel!</pre><pre><strong>@IBOutlet</strong> <strong>weak</strong> <strong>var</strong> photoImageView: UIImageView!</pre><blockquote><strong>回 NewsTableViewController.swift</strong></blockquote><p>▲防止重覆利用的 cell 顯示之前照片<br> ➞未抓完 / 沒圖片時會顯示 SF Symbols 的 photo 圖<br>▲利用 URLSession 在背景抓 Data 不會卡住 UI<br>▲如有抓到資料便在 main thread 執行 UI 相關程式<br> ➞將抓到的 Data 轉成 UIImage<br>▲呼叫 resume() 才會開始執行任務 (啟動後才會開始抓資料)<br>☆其實抓圖可用套件 Kingfisher 簡化為 1 行不用 URLSession (<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/%E4%BD%BF%E7%94%A8-spm-%E5%AE%89%E8%A3%9D%E7%AC%AC%E4%B8%89%E6%96%B9%E5%A5%97%E4%BB%B6-xcode-11-%E6%96%B0%E5%8A%9F%E8%83%BD-2c4ffcf85b4b"><strong>彼得教學</strong></a>)<br> ➞ import <strong>Kingfisher</strong><br> ➞ cell.photoImageView<strong>.kf.setImage</strong>(with: <strong>url</strong>)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z6FPN8axWVVmlWbkxodVUQ.png" /></figure><p>❇️ 寫抓資料的 function：超長…要往下拉才看得到程式碼圖</p><p>▲將 API 網址轉成 URL，網址含非英文數字等特定符號時有 2 種方法解<br> ➞ addingPercentEncoding，在網址後加以下程式 ↓<br> <strong>.addingPercentEncodinf</strong>(withAllowedCharacters<strong>: .urlQueryAllowed</strong>)<br> ➞用 baseURL, URLComponents, URLQueryItem 產生 URL (<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/%E4%BD%BF%E7%94%A8-baseurl-urlcomponents-urlqueryitem-%E7%94%A2%E7%94%9F-url-1e4539a33a89"><strong>彼得教學</strong></a>)<br>▲要寫網路功能都和 <strong>URLSession</strong>有關 (連線 / 送出 request 的意思)<br> ➞用 shared 產生 1 共用的 session 並以此呼叫 dataTask func 抓 / 上傳資料<br> ➞抓完資料無論成功與否，會給 3 參數讓我們在 closure 內去執行<br> ➞加 <strong>resume()</strong>才會開始抓資料及執行任務<br>▲判斷有無抓到資料後可先印出 JSON 確認，utf8 為各語言符號(此省)<br> let content = String(data: data, encoding: <strong>.utf8</strong>) { print(content)}<br> ➞用 <strong>JSONDecoder()</strong>解碼 JSON 抓下來的資料<br> ➞ JSONDecoder 解析時間的 <strong>dateDecodingStrategy</strong>(<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/jsondecoder-%E8%A7%A3%E6%9E%90%E6%99%82%E9%96%93%E7%9A%84-datedecodingstrategy-a4095481f193"><strong>彼得教學</strong></a>)<br> ➞呼叫 decode func 解碼，此 func 會有 throws 要用 try?，簡短但無法印錯<br>let searchResponse = <strong>try?</strong> decoder.<strong>decode</strong>(SearchResponse.self, from: data)<br>▲可改用 <strong>do catch</strong>配合 <strong>try</strong>寫法便能印出錯誤，錯誤解析參考<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/%E6%89%BE%E5%87%BA-jsondecoder-%E7%9A%84-decode-%E9%8C%AF%E8%AA%A4-c9c6deae297"><strong>彼得教學</strong></a><strong><br> </strong>➞ do 裡做想嘗試 (try <strong>無問號</strong>) 之事，失敗時產生錯誤並執行 catch <br> ➞用 JSONDecoder 將型別 Data 的 JSON 資料變成型別 SearchResponse <br> decode func 的參數 Type 傳入想生成東西之型別，傳入型別的寫法：<strong>型別名字 + .self</strong><br> ➞存入原為空白的 Array，之前 struct 有設 optional 所以要加！<br> ➞ <strong>reloadData</strong>觸發 TableView DataSource 相關 function 後畫面才會更新</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aKhNiMSAepCy9VNAWvloaA.png" /></figure><blockquote><strong>ViewDidLoad：不用每次下全部圖，因此抓圖寫在 cell 裡 不在此 func 裡</strong></blockquote><pre><strong>override</strong> <strong>func</strong> viewDidLoad() {<br>   <strong>super</strong>.viewDidLoad()</pre><pre>  <strong> fetchArticles()</strong><br>}</pre><blockquote><strong>完整程式碼</strong></blockquote><blockquote><strong><em>SearchResponse.swif，NewsTableViewController</em></strong></blockquote><blockquote><strong><em>NewsTableViewCell，WebViewController</em></strong></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6f047d42638d2245b54e3a834398aa8/href">https://medium.com/media/f6f047d42638d2245b54e3a834398aa8/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dbd2a21af099" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E6%97%A5%E6%9C%AC%E5%A8%9B%E6%A8%82%E6%96%B0%E8%81%9Eapp-%E4%B8%8B%E8%BC%89%E8%B3%87%E6%96%99-dbd2a21af099">日本娛樂新聞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[照片牆 (Grid Photo Wall)]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/%E7%85%A7%E7%89%87%E7%89%86-grid-photo-wall-f5076abdbe05?source=rss-74745a788318------2</link>
            <guid isPermaLink="false">https://medium.com/p/f5076abdbe05</guid>
            <category><![CDATA[照片牆]]></category>
            <category><![CDATA[flowlayout]]></category>
            <category><![CDATA[collectionviewcontroller]]></category>
            <category><![CDATA[collectionview-cell]]></category>
            <dc:creator><![CDATA[Monica 的 iOS APP 程式設計入門]]></dc:creator>
            <pubDate>Thu, 09 Sep 2021 06:28:38 GMT</pubDate>
            <atom:updated>2021-09-09T09:18:54.604Z</atom:updated>
            <content:encoded><![CDATA[<h4>CollectionViewController, CollectionViewCell, FlowLayout</h4><blockquote><strong>成品：照片牆就是要用這種主題才美呀～時間又花在選圖排序上 😅</strong></blockquote><p>▲本想配合 IG API 作業練習，每個圖都開分頁來看卻都不夠美不想用…<br>▲只好去官網找圖，直圖且不同展的數量不多，全點開來看一晚就過了<br>▲最後仍需用橫圖湊量，不過結果算挺不錯的啦！Ya~<br>▲為了名稱好辨別全改 TeamLab，原為 teamLab★<br>▲參考來源：<a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84%E8%A9%A6%E7%85%89-%E5%8B%87%E8%80%85%E7%9A%84-100-%E9%81%93-swift-ios-app-%E8%AC%8E%E9%A1%8C/53-collection-view-%E7%85%A7%E7%89%87%E7%89%86-c03f156de0c"><strong>彼得</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/289/1*Oeh7lO-ITOH3kgoisMdFDA.gif" /></figure><blockquote><strong>前置作業</strong></blockquote><p>▲加 CollectionViewController 並設為首頁 + Navigation<br>▲新增 Cocoa Touch Class * 3，取名後各自配好，別忘了 Cell ID 也要<br>▲ ImageView (Aspect Fill) 入 Cell 小框，且 AutoLayout 上下左右設為 0<br>▲ Cell 拉 Segue 至 PhotoViewController ，ImageView 也加上去<br>▲ TeamLabCollectionViewController — ControllerView<br> ➞ Estimate Size 選 None 便不會自動計算高度<br> ➞ Layout 選 Flow 則為 1 格 1 格 照片牆模式</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_Hs-0_zd7ERF15cbs36eQg.png" /></figure><blockquote><strong>TeamLabCollectionViewCell.swift：ImageView 拉 Outlet</strong></blockquote><pre><strong>@IBOutlet</strong> <strong>weak</strong> <strong>var</strong> photoImageView: UIImageView!</pre><blockquote><strong>TeamLabCollectionViewController.swift</strong></blockquote><p>▲ <strong>刪除</strong>紅框處</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TgA7ZPX6JsW0hcJlcicXWQ.png" /></figure><p>▲我們只要 1 個 Section 預設也為 1 個，這 4 行便可安心<strong>刪除</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*moFGAv-4_okq-IEdzZa93Q.png" /></figure><p>▲ item 有 27 個</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FmdCTqj8uk189w5mQL_33A.png" /></figure><p>▲和 TableView 作法一樣，轉型成 Cell<br>▲ TableView 為 indexPath.row，這為 indexPath.item</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8NFot3wrR7MfJOMAp3XvuQ.png" /></figure><p>▲設定 1 排固定照片數量的 cell 大小：更多方法請參考<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"><strong>彼得教學</strong></a><br> ➞ itemSpace：間距大小，columnCount：1 排幾個項目<br> ➞ collectionViewLayout 是 CollectionViewController 的變數<br> ➞ collectionViewLayout 型別為 UICollectionViewLayout 所以要轉型<br> ➞計算 cell 寬度<br> ◇ floor 為無條件捨去，避免超出螢幕<br> ◇ collectionView.bounds.width 是螢幕寬度，此案 SE1 為 320<br> ➞ itemSize 設為正方形<br> ➞用自己尺寸來設非 AutoLayout 所以 estimatedItemSize = .zero<br> ➞將 Min Spacing For Cells &amp; Min Spacing For Lines 都設為 itemSpace</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j9TLnLf1UrhCkjrvhvSC9A.png" /></figure><blockquote><strong>PhotoViewController.swift</strong></blockquote><p>▲定義 init，由參數設定 property；拉 Outlet；設圖片</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MzUaCnR6z4dGTu1p3Xa8DQ.png" /></figure><blockquote><strong>回 TeamLabCollectionViewController.swift：拉 SegueAction</strong></blockquote><p>▲本來寫不出來，看彼得範例才學到<br> ➞ tableView 是 tableView.indexPathForSelectedRow?.row<br> ➞這是 collectionView.indexPathsForSelectedItems?.first?.item</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ick-HwZAAgf7CEyEKLo1hA.png" /></figure><blockquote><strong>完整程式碼：TeamLabCollectionViewController</strong></blockquote><blockquote><strong>TeamLabCollectionViewCell，PhotoViewController</strong></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d88bd4f69eeb7acd2ee27428260cc8be/href">https://medium.com/media/d88bd4f69eeb7acd2ee27428260cc8be/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f5076abdbe05" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E7%85%A7%E7%89%87%E7%89%86-grid-photo-wall-f5076abdbe05">照片牆 (Grid Photo Wall)</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[情歌對唱的聊天室畫面]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/%E6%83%85%E6%AD%8C%E5%B0%8D%E5%94%B1%E7%9A%84%E8%81%8A%E5%A4%A9%E5%AE%A4%E7%95%AB%E9%9D%A2-eed11935a6db?source=rss-74745a788318------2</link>
            <guid isPermaLink="false">https://medium.com/p/eed11935a6db</guid>
            <category><![CDATA[情歌對唱的聊天室畫面]]></category>
            <category><![CDATA[uitableviewdatasource]]></category>
            <category><![CDATA[uitextview]]></category>
            <category><![CDATA[cell的autolayout]]></category>
            <dc:creator><![CDATA[Monica 的 iOS APP 程式設計入門]]></dc:creator>
            <pubDate>Sat, 04 Sep 2021 14:35:39 GMT</pubDate>
            <atom:updated>2021-09-05T16:16:00.955Z</atom:updated>
            <content:encoded><![CDATA[<h4>cell 的 auto layout, UITextView 高度 = 文字內容高度, UITableViewDataSource</h4><blockquote><strong>成品：男女對唱歌很多，完全沒合唱還連句接唱的只想到這首 😆</strong></blockquote><p>▲有合唱的話聊天室就會看起來很怪嘛 =3=<br>▲私心覺得找香港男歌手唱整首粵語應該好聽百倍…<br>▲不過這下網誌就西洋+日本+台語+粵語+國語音樂全湊齊啦<br>▲手機聊天室裡圖文沒這麼大，但為了看清楚將就醜一下囉<br>▲參考來源：<a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84%E8%A9%A6%E7%85%89-%E5%8B%87%E8%80%85%E7%9A%84-100-%E9%81%93-swift-ios-app-%E8%AC%8E%E9%A1%8C/63-%E6%83%85%E6%AD%8C%E5%B0%8D%E5%94%B1%E7%9A%84%E8%81%8A%E5%A4%A9%E5%AE%A4%E7%95%AB%E9%9D%A2-cell-%E7%9A%84-auto-layout-53697c2ad80c"><strong>彼得</strong></a>和<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/unit-iv-uitableview-%E8%81%8A%E5%A4%A9%E6%83%85%E5%A2%83-%E6%83%85%E6%AD%8C%E5%B0%8D%E5%94%B1%E7%B8%BD%E6%9C%89%E5%A4%A9%E6%9C%83%E8%86%A9-%E4%BB%8A%E6%99%9A%E6%88%91%E6%83%B3%E4%BE%86%E9%BB%9E%E6%92%A9%E4%BA%BA%E9%87%91%E5%8F%A5-6f6b90b69699"><strong>學長</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*4E2FUsC09fyqgO5ScaV93Q.gif" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fv04DzjzSb5w%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dv04DzjzSb5w&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fv04DzjzSb5w%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/2392c481e3d78541a092335ec741a67b/href">https://medium.com/media/2392c481e3d78541a092335ec741a67b/href</a></iframe><blockquote><strong>前置作業</strong></blockquote><p>▲放底圖後 + TableView + TableViewCell * 2 ，2 者背景選無色<br>▲新增 Swift File * 1 + Cocoa Touch Class * 2，後 2 者取名後各自配好<br>▲ TableView 不要分線 Separator 調 None<br>▲ TableViewCell ID 取名，RowHeight 本有顯示，刪除後自動調整高度<br>▲加 ImageView，autoLayout 調 1 : 1 正方形<br>▲加 TextView，Scrolling Enabled 取消勾選<br> ➞ Content Compression Resistance Priority Horizontal 750 改 1000 使換行<br>▲ StackView : ImageView + TextView，水平軸，Alignment : Center<br> ➞ autoLayout 上下左右間距 10，1 頭 1 尾改 ≥ 10 ，長度自動調整<br>▲ TableView 拉至 ViewController 選 dataSource <strong>↓</strong><br><strong>✅ 指定 data source / delegate 是型別 A 生成的東⻄</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7bm910MHya5yHIlKp5sEWw.png" /></figure><blockquote><strong>Girl/BoyTableViewCell.swift：拉 Outlet 設圓角，調整文字上下左右間距</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L9sauZbG_3nMgFsWNibBlg.png" /></figure><blockquote><strong><em>用 </em></strong><strong><em>struct定義 </em>Lyric<em> 型別並寫其 Array 於 ViewController (未全列完)</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ilpO6AmkS_91VJQTGJfUfQ.png" /></figure><blockquote><strong>TableView 拉 Outlet 至 ViewController</strong></blockquote><pre><strong>@IBOutlet</strong> <strong>weak</strong> <strong>var</strong> tableView: UITableView!</pre><p><strong>✅ 定義型別 A 遵從 protocol：UITableViewDataSource，寫 extension</strong></p><pre><strong>extension</strong> ViewController: UITableViewDataSource {<br>}</pre><p><strong>✅ 在型別 A 裡定義 protocol 的 function</strong></p><p>▲ cell 數量等於 lyrics Array 數量</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L_Ro0RXqU0oO-Esw1wKgog.png" /></figure><p>▲依頭像照片辨別是男是女來套用不同 cell 內容<br>▲ withIdentifier 用 2 種不同方法寫，第 3 種請參考<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/%E5%AF%A6%E4%BD%9C-table-view-%E5%9F%BA%E6%9C%AC%E5%8A%9F%E8%83%BD-d9330e0381f8"><strong>上篇</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j0s1zjx8zsL7hz_KcQtiRA.png" /></figure><blockquote><strong>完整程式碼：Lyric.swift，ViewController</strong></blockquote><blockquote><strong>GirlTableViewCell，BoyTableViewCell</strong></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e1c90a48bfdda0a34359dee1fe4eb9b/href">https://medium.com/media/4e1c90a48bfdda0a34359dee1fe4eb9b/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eed11935a6db" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E6%83%85%E6%AD%8C%E5%B0%8D%E5%94%B1%E7%9A%84%E8%81%8A%E5%A4%A9%E5%AE%A4%E7%95%AB%E9%9D%A2-eed11935a6db">情歌對唱的聊天室畫面</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[實作 Table View 基本功能]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/%E5%AF%A6%E4%BD%9C-table-view-%E5%9F%BA%E6%9C%AC%E5%8A%9F%E8%83%BD-d9330e0381f8?source=rss-74745a788318------2</link>
            <guid isPermaLink="false">https://medium.com/p/d9330e0381f8</guid>
            <category><![CDATA[ibsegueaction]]></category>
            <category><![CDATA[ass]]></category>
            <category><![CDATA[guard-let]]></category>
            <category><![CDATA[uitableviewdatasource]]></category>
            <category><![CDATA[客製cell]]></category>
            <dc:creator><![CDATA[Monica 的 iOS APP 程式設計入門]]></dc:creator>
            <pubDate>Tue, 31 Aug 2021 02:59:09 GMT</pubDate>
            <atom:updated>2021-09-04T19:27:15.834Z</atom:updated>
            <content:encoded><![CDATA[<h4>UITableViewDataSource, <strong>客製 cell, as, guard let, </strong>IBSegueAction</h4><blockquote><strong>成品：恭喜許富凱終於得到金曲歌王頭銜啦～～～</strong></blockquote><p>▲主題決定很快但做超久～因為一直瘋狂聽歌不專心 XDD<br>▲設定 10 首，可是好歌太多一整個難挑到不行呀 &gt;_&lt;<br>▲本想仿 Youtube 但懶了… 科科 <br>▲參考來源：<a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84%E8%A9%A6%E7%85%89-%E5%8B%87%E8%80%85%E7%9A%84-100-%E9%81%93-swift-ios-app-%E8%AC%8E%E9%A1%8C/51-%E5%AF%A6%E4%BD%9C-table-view-%E7%9A%84%E5%9F%BA%E6%9C%AC%E5%8A%9F%E8%83%BD-8f52cc9d2bf1"><strong>彼得</strong></a>與<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/38table-view-%E7%9A%84%E5%9F%BA%E6%9C%AC%E5%8A%9F%E8%83%BD-e48cdb38b4a0"><strong>學姊</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/290/1*Y3lChHv2Tejkh-YHEtzczQ.gif" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F1r7eLP4703k%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D1r7eLP4703k&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F1r7eLP4703k%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/23c0dafa1d0148097969e2e34f987095/href">https://medium.com/media/23c0dafa1d0148097969e2e34f987095/href</a></iframe><blockquote><strong>前置作業</strong></blockquote><p>▲初始 TableViewController + Navigation + ViewController (WebView)<br>▲新增 Swift File * 1 + Cocoa Touch Class * 3 ，取名後各自配好<br>▲ TableViewController 選 Dynamic，不要分線 Separator 調 None<br>▲ cell 手動調整高度或 inspector 取消自動寫數值皆可<br> ➞確定要固定高度可至 TableView 改 Row Height，Estimate 值<br>▲ cell 選 Custom 後加 ImageView 與 Label，全用 AutoLayout 才可固定<br>▲ cell Identifier 取名，因不想點 cell 時有顏色，Selection 調 None<br>▲ cell 拉 Segue 至 ViewController，一開始非從 cell 拉結果沒反應</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZXPQrvhBXsuwpH8enc_r8w.png" /></figure><blockquote><strong>用 </strong><strong>struct定義 <em>Song</em> 型別並寫其 Array 於 TableViewController</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F3XMC54nQ85p-Mlr5DSvRw.png" /></figure><blockquote><strong>TableViewController 是 TableView 的 dataSource</strong></blockquote><blockquote><strong>其 function下面都有註解呈灰色可自由選擇使用</strong></blockquote><p>▲ Section 及 Row 預設為 0 呈空白，須自行調整<br>▲ Section 數量 func 為 optional ，不定義便為 1 個 Section<br>▲ cell 的數量等於 songs array 數量，只有 1 行其實 return 可省</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vXpVJffTrv4aydAhL03teA.png" /></figure><blockquote><strong>SongTableViewCell.swift：拉 IBOutlet 及定義 update</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9O0P27Yn9Ns62g2V3Q95wg.png" /></figure><blockquote><strong>回 SongTableViewController.swift</strong></blockquote><p>▲cell Id 定義成常數，避免之後打錯字，其餘方法請參考<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/%E6%90%AD%E9%85%8D%E5%9E%8B%E5%88%A5-self-%E5%92%8C-string-interpolation-%E5%AE%89%E5%85%A8%E5%BE%97%E5%88%B0%E5%9E%8B%E5%88%A5%E5%AD%97%E4%B8%B2-ed0a37b82877"><strong>彼得教學</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7eSYculLEX1xk7BAudDa9w.png" /></figure><p>▲ guard let 判斷 optional 是否有值和讀取它的內容，<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%92%8C-if-let-%E5%BE%88%E5%83%8F%E7%9A%84-guard-let-d37900352c37"><strong>彼得教學</strong></a><strong><br></strong>▲ dequeueReusableCell 是產生 cell 的 function<br> ➞為顯示下行 cell，將上端即將消失 cell 移除給下行重複使用<br> ➞ cell ID 如和之前 inspector 設定不同會閃退請注意<br> ➞ indexPath = cell 位置 (section &amp; row)，一定有值<br>▲用 as? 轉型成 cell 的型別後便可存取它的 outlet<br>▲設定 cell 有很多方法，此為呼叫 cell update func 並將 song 當參數傳入<br> ➞ 如 cell 使用內建樣式非客制，iOS 15 寫法請參考<a href="https://apppeterpan.medium.com/%E5%BE%9E-ios-15-%E9%96%8B%E5%A7%8B-%E4%BD%BF%E7%94%A8%E5%85%A7%E5%BB%BA-cell-%E6%A8%A3%E5%BC%8F%E5%BB%BA%E8%AD%B0%E6%90%AD%E9%85%8D-uilistcontentconfiguration-13d64eb317be"><strong>彼得教學</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_npM_kC-uqi6lauaVr5fPQ.png" /></figure><blockquote><strong>WebViewController：頁面間資料傳遞</strong></blockquote><p>▲定義 init，由參數設定 property<br>▲寫完出現紅色修正屬正常，按 fix 自動有<strong> </strong>required initializert</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zecU4PBNL4b5niT659RRIw.png" /></figure><blockquote><strong>回 SongTableViewController.swift：拉 IBSegueAction</strong></blockquote><p>▲ indexPathForSelectedRow<br> ➞ TableView 的 property，型別為 IndexPath?<br> ➞代表目前被選取的 cell 的 index path</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WR1aadK62Hq1wg3cgZzXNQ.png" /></figure><blockquote><strong>WebViewController：加入函式庫，拉 IBOutlet，載入網頁</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b5nGX1JXje1zc3wmwP6XWw.png" /></figure><blockquote><strong>完整程式碼</strong></blockquote><blockquote><strong>Song.swif，SongTableViewController</strong></blockquote><blockquote><strong>SongTableViewCell，WebViewController</strong></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/612775dc2e33b9833c4d0efc40bb9422/href">https://medium.com/media/612775dc2e33b9833c4d0efc40bb9422/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d9330e0381f8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E5%AF%A6%E4%BD%9C-table-view-%E5%9F%BA%E6%9C%AC%E5%8A%9F%E8%83%BD-d9330e0381f8">實作 Table View 基本功能</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[ScrollViewDelegate — 分頁與縮放]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/scrollviewdelegate-%E5%88%86%E9%A0%81%E8%88%87%E7%B8%AE%E6%94%BE-b7a6a3518d26?source=rss-74745a788318------2</link>
            <guid isPermaLink="false">https://medium.com/p/b7a6a3518d26</guid>
            <category><![CDATA[scrollviewdelegate]]></category>
            <dc:creator><![CDATA[Monica 的 iOS APP 程式設計入門]]></dc:creator>
            <pubDate>Tue, 24 Aug 2021 15:02:12 GMT</pubDate>
            <atom:updated>2021-08-24T16:26:43.217Z</atom:updated>
            <content:encoded><![CDATA[<h3>ScrollViewDelegate — 分頁與縮放</h3><h4>UIScrollViewDelegate, Stack View, Page Control, viewForZooming</h4><blockquote><strong>成品：選了這季比較喜歡的日劇</strong></blockquote><p>▲通常照片瀏覽用 collection view / UIPageViewController 效果較好<br>▲模擬器縮放請按 option + 滑鼠拉<br>▲心不靜，這作業 + 網誌寫好幾天才完成…<br>▲參考來源：<a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84%E8%A9%A6%E7%85%89-%E5%8B%87%E8%80%85%E7%9A%84-100-%E9%81%93-swift-ios-app-%E8%AC%8E%E9%A1%8C/scroll-view-delegate-%E7%B7%B4%E7%BF%92-60e185298513"><strong>彼得</strong></a><strong>(</strong><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/stack-view-scroll-view-%E8%A3%BD%E4%BD%9C%E5%88%86%E9%A0%81-tutorial-%E6%87%B6%E5%BE%97%E5%AF%AB%E7%A8%8B%E5%BC%8F%E7%89%88-7ff9021317a0"><strong>分頁</strong></a><strong>，</strong><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%9A%A8%E8%91%97%E6%8F%9B%E9%A0%81%E6%9B%B4%E6%96%B0%E7%9A%84-page-control-%E5%B0%8F%E5%9C%93%E9%BB%9E-24f631c1928c"><strong>小圓點</strong></a><strong>，</strong><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%B8%AE%E6%94%BE%E5%9C%96%E7%89%87-72960fb1b332"><strong>縮放</strong></a><strong>)</strong>與<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/37%E5%9C%96%E7%89%87%E6%9B%B4%E6%8F%9B-f0f6912138a6"><strong>學姊</strong></a>作品</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/513/1*Yx_JfXahBkgI4_V4q2P7ng.gif" /></figure><blockquote><strong>分頁步驟</strong></blockquote><p>▲將所需圖放至 Assets<br>▲加入佔滿螢幕的 Scroll View<br> ➞ Scroll View + cmd +View，設定上下左右對齊 (皆為 0) <br> ➞大機型不想被瀏海擋住可設 Scroll View &amp; Safe Area 上下左右對齊<br> ➞出現紅色錯誤因 Scroll View 不知捲動範圍</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jClmFU_q291gPD6NXHohjQ.png" /></figure><p>▲加入 Image View：想滿版設為 Aspect Fill<br>▲ Image View 加入 Stack View<br> ➞ Axis 選水平 Horizontal (依滑動方向而定)，Alignment 為 Fill<br> ➞ Distribution 選 Fill Equally， Spacing 為 0</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N9v2x3wMlc29Vu5k_3YKcA.png" /></figure><p>▲ Stack View 上下左右間距設為 0<br> ➞ Stack View + cmd + Content Layout Guide 設定上下左右對齊 (皆為 0)<br> ➞ Scroll View 被 Stack View 填滿，由 Stack View 大小決定<strong>捲動範圍</strong>大小<br> ➞ Scroll View 大小由裡面 Image View 決定<br> ➞ 每個 Image View 大小由當初圖片大小決定</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hiVivfkL0sRzk2UwN39LAw.png" /></figure><p>▲圖片寬高 = 螢幕寬高<br>➞ Image View + cmd + Frame Layout Guide： Equal Widths / Heights<br>➞ Frame Layout Guide 為 Scroll View 本身 frame<br>➞ 圖與 F 設一樣大時，圖和螢幕也會同大，因 Scroll View 和螢幕一樣大<br>▲ Frame Layout Guide &amp; Content Layout Guide 差異<br> ➞ Content Layout Guide 含整個捲動內容 (大)<br> ➞ Frame Layout Guide 為其中一部分，此指螢幕大小</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2yJWHRS_3lDYoo_LXYgg9A.png" /></figure><p>▲加入其他 Image View：左側複製貼上再改圖內容<br> ➞寬度為本來 3 倍 (藍框，Content Layout Guide)<br> ➞ Scroll View 勾選 Paging Enabled 使其顯示完整圖</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hmMz_COJ0zJr_qNIhjJuRA.png" /></figure><p>▲加入 Page Control<br> ➞切記不能加在 Scroll View 裡，否則滑動時會隨之移動<br> ➞選擇頁數，預設為 3 頁符合此案便不需調整<br> ➞設定 Page Control 的 Auto Layout：水平置中<br> ➞設定 Scroll View 的 delegate 為 View Controller<strong> ↓<br> ✅ 指定 data source / delegate 是型別 A 生成的東⻄</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*THwFHJCgQ4EIcAcghCTpCQ.png" /></figure><p><strong>✅ 定義型別 A 遵從 protocol：UIScrollViewDelegate，直接寫 extension</strong></p><pre><strong>extension</strong> ViewController: <strong>UIScrollViewDelegate</strong> {<br>}</pre><p><strong>✅ 在型別 A 裡定義 protocol 的 function</strong></p><p>▲cmd 點 UIScrollViewDelegate<strong> </strong>選 Jump to Definition 找功能<br>▲找到適合 function 後記下關鍵字 DidEndDecelerating<br>▲回 ViewController 打 didenddece 等會自動出現整個 function 可選<br>▲定義 function<br> ➞ scrollView.contentOffset.x 為目前滑動的水平距離<br> ◇ 因之前勾 Paging Enabled 一定停在某分頁，為分頁倍數(0, 568, 1136)<br> ➞ scrollView.bounds.width 是 scroll view 寬度 = 分頁寬度(568)<br> ➞ 2 者相除便可算出在位於第幾頁並更新小圓點顏色</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k1NPytfG4hVRRV_aECtm8w.png" /></figure><p>▲ Page Control 自己也要拉 Action，點選不同小圓點使圖更動<br> ➞依 currentPage 算出各頁起點座標(0,0) / (568,0) / (1136,0)<br> ➞各座標為 Scroll View 要移動的量(offset)<br> ➞ currentPage 原為 Int，而 CGPoint 要 CGFloat，所以要改</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kjakQsRgp0q6NNAIC6PXfg.png" /></figure><blockquote><strong>手勢縮放</strong></blockquote><p>▲ Inspector 設定 Scroll View — Zoom：Min 1，Max 5<br>▲剛 UIScrollViewDelegate 的<strong> </strong>Jump to Definition 裡有 viewForZooming <br>▲回 ViewController 打 viewforz 等會自動出現整個 function 可選<br>▲其實裡面執行任務我還無法理解為何要這樣寫…之後懂了再補解釋</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*67xTYgyouhd-O0G6g3reaQ.png" /></figure><blockquote><strong>完整程式碼</strong></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e776ed60fcce67d3b38ed40b76b93f0/href">https://medium.com/media/4e776ed60fcce67d3b38ed40b76b93f0/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b7a6a3518d26" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/scrollviewdelegate-%E5%88%86%E9%A0%81%E8%88%87%E7%B8%AE%E6%94%BE-b7a6a3518d26">ScrollViewDelegate — 分頁與縮放</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/%E7%85%A7%E7%89%87%E7%B7%A8%E8%BC%AF-app-705860803dab?source=rss-74745a788318------2</link>
            <guid isPermaLink="false">https://medium.com/p/705860803dab</guid>
            <category><![CDATA[extension]]></category>
            <category><![CDATA[delegate]]></category>
            <category><![CDATA[照片編輯app]]></category>
            <category><![CDATA[protocol]]></category>
            <dc:creator><![CDATA[Monica 的 iOS APP 程式設計入門]]></dc:creator>
            <pubDate>Fri, 20 Aug 2021 05:35:39 GMT</pubDate>
            <atom:updated>2021-08-22T16:31:09.948Z</atom:updated>
            <content:encoded><![CDATA[<h4>protocol, delegate, extension, UIImagePickerController, UIColorPickerViewController, UIGraphicsImageRenderer, UIActivityViewController</h4><blockquote><strong>半成品？：主要練習 delegate，其餘有空再補</strong></blockquote><p>▲刪除不少功能，想做濾鏡但耗時便空留提醒未來的自己<br>▲圖應設 Fill 卻因文字功能被我砍掉，為練習選色改 Fit 成醜背景<br>▲感恩大大們分享文尤其 <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/31-delegate-practice-photo-editor-app-3e3118ce33cb"><strong>Ivan</strong></a><strong> </strong>，其他太多直接貼<a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84%E8%A9%A6%E7%85%89-%E5%8B%87%E8%80%85%E7%9A%84-100-%E9%81%93-swift-ios-app-%E8%AC%8E%E9%A1%8C/95-%E7%85%A7%E7%89%87%E7%B7%A8%E8%BC%AF-app-335ef37cc707"><strong>彼得</strong></a>這大組頭啦<br>▲<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%80%8F%E9%81%8E-delegate-or-data-source-%E5%AF%A6%E7%8F%BE-app-%E5%8A%9F%E8%83%BD%E7%9A%84%E6%AD%A5%E9%A9%9F%E8%AA%AA%E6%98%8E-b57a948066da"><strong>彼得教學</strong></a>透過 delegate or data source 實現 App 功能的步驟說明<br>▲iOS SDK 選東西的 view controller &amp; delegate 範例也請參考<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-sdk-%E9%81%B8%E6%9D%B1%E8%A5%BF%E7%9A%84-view-controller-delegate-%E7%AF%84%E4%BE%8B-c3f0b5238933"><strong>彼得教學</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/289/1*JWuZkeN_odr7TLvYRKxJTA.gif" /></figure><blockquote><strong>前置作業</strong></blockquote><p>▲較特別的是為之後分享(存)照片，圖須放到 ContainerView 裡<br>▲ ContainerView 背景原設無色出來卻變白底？(右下圖) <br> ➞因為圖非設為 Aspect Fill 而是 Aspect Fit <br> ➞既然如此…只好改成和 View 一樣背景的黑色佯裝😝<br>▲除 EditImageView 還另增 OriginalImageView 來恢復原始照片<br> ➞ 隱藏 OriginalImageView</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GXDoeGwPbj1MbPmWoWfBLw.png" /></figure><p>▲請求使用者同意使用相機與存照片權限：入 info 加項目並打問句<br> ➞就是第 1 次使用 app 會問你要不要允許那玩意<br> ➞相機功能模擬器無法實測，唯有使用實機才行</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EjUTn0ZhvjA0KRZ97OyMrQ.png" /></figure><blockquote><strong>透過 data source / delegate 客製功能 ：拍照 ，選相簿照片</strong></blockquote><p><strong>✅ 指定 data source / delegate 是型別 A 生成的東⻄</strong></p><p>▲生成 UIImagePickerController<br>▲其內建 sourceType 裡有我們要的 album 和 camera (之後設為 alert 選項)<br>▲將此 ViewController (self) 設為 UIImagePickerController 的 delegate<br>▲打完出現紅色屬正常，按修正後 protocol 會在class 那行出現</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DXovPEm5_PCjzHv5mCtXfg.png" /></figure><p><strong>✅ 定義型別 A 遵從 protocol：2 個 protocol (黃框)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e7xzyDccwYpFgslNsmElrg.png" /></figure><p><strong>✅ 在型別 A 裡定義 protocol 的 function</strong></p><p>▲cmd 點 UIImagePickerControllerDelegate 選 Jump to Definition 找功能<br> ➞ protocol 的 function 如前面有 optional ，代表不一定要定義的功能<br> ➞ 反之，一定要定義<br>▲找到適合 function 後記下關鍵字 didFinish<br>▲回 ViewController 打 didfi 等會自動出現整個 function 可選</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XaAB3EsL-n0507BYfBIuLw.png" /></figure><p>▲ info: [UIImagePickerController.InfoKey : <strong>Any</strong>]<br> ➞ info 裡有照片，且為 dictionary 寫法 ( key &amp; value 用冒號連接)<br>▲ cmd 點 InfoKey 選 Jump to Definition 看究竟內建有何物？<br>▲適合者為 originalImage，因 key 型別一樣可省略只寫 . <br>▲ dictionary 含不同型別所以 value 型別為 Any，用 as 轉型為 UIImage<br>▲從相簿選照片後還想執行什麼任務自行撰寫<br><strong>☆</strong>至於設定 alert 照相或選取相簿照片與其任務請看最下面完整程式碼</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mgB-0kKG25lA1IBNBd2Uhg.png" /></figure><blockquote><strong>extension：使 class 乾淨，程式漂亮好維護</strong></blockquote><p>▲原最上面 class 那行後會加上此頁 ViewController 所有 protocol <br>▲ class 裡也有各個定義 protocol 的 function <br>▲使用越多畫面越混亂不易查詢觀看</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ulG0-GiHFQGdGanY3vPSgQ.png" /><figcaption>(黃框) protocol，(藍框) function</figcaption></figure><p>▲可於最底或另開 File 用 extension 將 protocol 與 function 寫入內</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TtTccJiUJNreUXv8D2eIWA.png" /></figure><blockquote><strong>透過 data source / delegate 客製功能 ：選取顏色</strong></blockquote><p><strong>✅ 指定 data source / delegate 是型別 A 生成的東⻄</strong></p><p>▲生成 UIColorPickerViewController<br>▲將此 Controller 設為 UIColorPickerViewController 的 delegate<br>▲出現紅色屬正常，按修正後 protocol 會在class 那行出現</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o_jy3fXV_GrcgJkyezwyWg.png" /></figure><p><strong>✅ 定義型別 A 遵從 protocol：1 個 protocol (黃框)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qgDxQdrmhX6lByfWTLeOCg.png" /></figure><p><strong>✅ 在型別 A 裡定義 protocol 的 function</strong></p><p>▲cmd 點 UIColorPickerViewControllerDelegate 選 Jump to Definition <br>▲找到適合 function 後記下關鍵字 didSelect<br>▲回 ViewController 打 didSe 等會自動出現整個 function 可選</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yKgayHtVIeH85Uhug9J63w.png" /></figure><p>▲剛 Jump to Definition 內提到選取顏色可用 .<strong>selectedColor<br></strong>▲ iOS 15 寫 editImageView.backgroundColor = color 即可</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0qI-IjnQBEly82CcqsERvw.png" /></figure><blockquote><strong>extension：將 protocol (黃框) 與 function (藍框) 拉至最底寫入內</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GG8VBtsPyKf6km3mnLxUTA.png" /></figure><blockquote><strong>用 UIGraphicsImageRenderer 將 view 變成 UIImage：</strong><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-uigraphicsimagerenderer-%E5%B0%87-view-%E8%AE%8A%E6%88%90%E5%9C%96%E7%89%87-41d00c568903"><strong>彼得完整教學</strong></a></blockquote><blockquote><strong>用 UIActivityViewController 分享 (儲存) 圖片</strong></blockquote><p>▲產生 UIGraphicsImageRenderer<br>▲欲將 containerView 畫面變圖片，尺寸便設為 containerView 的 size<br>▲呼叫 UIGraphicsImageRenderer 的 function image(actions:) 產生圖片<br>▲呼叫 function drawHierarchy(in:afterScreenUpdates:)<br> ➞將 containerView 內容製成圖片<br> ➞讓 containerView 和它的 subview 出現在圖片裡</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TeSSXI6ykBZc3d5l1WTd-w.png" /></figure><blockquote><strong>完整程式碼</strong></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f1e8743a4373cce5c4e037bdaf88bd0/href">https://medium.com/media/4f1e8743a4373cce5c4e037bdaf88bd0/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=705860803dab" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E7%85%A7%E7%89%87%E7%B7%A8%E8%BC%AF-app-705860803dab">照片編輯 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[Library-AVPlayerViewController 配 IBSegueAction 播影片]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/library-avplayerviewcontroller-%E9%85%8D-ibsegueaction-%E6%92%AD%E5%BD%B1%E7%89%87-e9f5e6d97814?source=rss-74745a788318------2</link>
            <guid isPermaLink="false">https://medium.com/p/e9f5e6d97814</guid>
            <category><![CDATA[ibsegueaction]]></category>
            <dc:creator><![CDATA[Monica 的 iOS APP 程式設計入門]]></dc:creator>
            <pubDate>Thu, 12 Aug 2021 08:28:09 GMT</pubDate>
            <atom:updated>2021-08-12T08:37:57.529Z</atom:updated>
            <content:encoded><![CDATA[<h4>IBSegueAction</h4><blockquote><strong>成品：沿用翔太主題之兩大才子合作～～～ ☺️</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*wnDKqFlX7-R2GNgW8cKq6A.gif" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FJqmlNApoAcA%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DJqmlNApoAcA&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FJqmlNApoAcA%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/919ea73305da51920de5cb9b57a34954/href">https://medium.com/media/919ea73305da51920de5cb9b57a34954/href</a></iframe><blockquote><strong>前置作業</strong></blockquote><p>▲ Project navigator 匯入影片檔案 (左側黃框)<br> ➞如選貼影片網址便不用檔案<br>▲ Library 加入 AVPlayerViewController (紅框)<br>▲ 用 button 先拉個 segue (右側黃框)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A_m61DVO5l55C-QDIMDt-A.png" /></figure><blockquote><strong>加入函式庫</strong></blockquote><pre>import AVKit</pre><blockquote><strong>拉 IBSegueAction：將 segue 右鍵拉至 ViewController 會自動出現這樣 ↓</strong></blockquote><pre><strong>@IBSegueAction</strong> <strong>func</strong> playVideo(<strong>_</strong> coder: NSCoder) -&gt; AVPlayerViewController? {</pre><pre><strong>    return</strong> &lt;#AVPlayerViewController(coder: coder)#&gt;</pre><pre>}</pre><blockquote><strong>2 種程式寫法：除來源不同其餘一樣且皆設定自動播放</strong></blockquote><p>▲匯入檔案寫法</p><pre><strong>@IBSegueAction</strong> <strong>func</strong> playVideo(<strong>_</strong> coder: NSCoder) -&gt; AVPlayerViewController? {</pre><pre><strong>    let</strong> controller = <strong>AVPlayerViewController</strong>(coder: coder)</pre><pre><strong>    let url = Bundle.main.url(/service/forresource: %E2%80%9Ccurtain call%E2%80%9D, <br>    withExtension: %E2%80%9Cmp4%E2%80%9D)</strong></pre><pre>    controller?.player = AVPlayer(url: url!)<br>   <strong> controller?.player?.play()</strong></pre><pre><strong>    return</strong> controller<br>}</pre><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%8F%96%E5%BE%97-youtube-%E5%BD%B1%E7%89%87%E7%9A%84%E7%B6%B2%E5%9D%80-6fc9f69f1fec"><strong>彼得教學</strong></a></p><pre><strong>@IBSegueAction</strong> <strong>func</strong> playVideo(<strong>_</strong> coder: NSCoder) -&gt; AVPlayerViewController? {</pre><pre><strong>let</strong> controller = <strong>AVPlayerViewController</strong>(coder: coder)</pre><pre><strong>let url = URL(string: &quot;https://bit.ly/2VIDLp5&quot;)</strong></pre><pre>controller?.player = AVPlayer(url: url!)<br>   <strong> </strong>controller?.player?.play()</pre><pre><strong>return</strong> controller<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e9f5e6d97814" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/library-avplayerviewcontroller-%E9%85%8D-ibsegueaction-%E6%92%AD%E5%BD%B1%E7%89%87-e9f5e6d97814">Library-AVPlayerViewController 配 IBSegueAction 播影片</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[透過 present 顯示 iOS SDK 內建的 controller]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/%E9%80%8F%E9%81%8E-present-%E9%A1%AF%E7%A4%BA-ios-sdk-%E5%85%A7%E5%BB%BA%E7%9A%84-controller-df5e1785320f?source=rss-74745a788318------2</link>
            <guid isPermaLink="false">https://medium.com/p/df5e1785320f</guid>
            <category><![CDATA[show]]></category>
            <category><![CDATA[present]]></category>
            <dc:creator><![CDATA[Monica 的 iOS APP 程式設計入門]]></dc:creator>
            <pubDate>Thu, 12 Aug 2021 08:24:12 GMT</pubDate>
            <atom:updated>2021-08-12T08:36:01.169Z</atom:updated>
            <content:encoded><![CDATA[<h4>Present, Show</h4><blockquote><strong>成品：翔太出歌必有保證，這首超好聽 !!! Aimer 音色真很特殊</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*V5zHKYj7R_iZt6YVnRodsQ.gif" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fi8qcU9JIprA%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Di8qcU9JIprA&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fi8qcU9JIprA%2Fhqdefault.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/ff10216403dade948830b533b7c698c6/href">https://medium.com/media/ff10216403dade948830b533b7c698c6/href</a></iframe><blockquote><strong>前置作業：因為要用 present，Storyboard 很單純只有 lael &amp; button</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/294/1*hVhCAf0GgPZtviODp-QVcA.png" /></figure><blockquote><strong>加入函式庫：想出現網頁和播影片，所以要加這 2 個</strong></blockquote><pre><strong>import</strong> SafariServices<br><strong>import</strong> AVKit</pre><blockquote><strong>顯示網頁 Controller</strong></blockquote><p>▲網頁 Controller 是<strong>SFSafariViewController<br></strong>▲ present 設定顯示什麼，有無轉場動畫 / 執行任務</p><pre><strong>@IBAction</strong> <strong>func</strong> connectToWeb(<strong>_</strong> sender: <strong>Any</strong>) {<br>   <strong>if</strong> <strong>let</strong> url = URL(string: “https://www.shimizushota.com&quot;) {</pre><pre>      <strong>let</strong> controller = <strong>SFSafariViewController</strong>(url: url)</pre><pre>       <strong>present(controller, animated: true, completion: nil)</strong><br>   }<br>}</pre><blockquote><strong>顯示影片 Controller</strong></blockquote><p>▲影片 Controller 是 <strong>AVPlayerViewController<br></strong>▲是影片網址不是網頁網址喔! 第 1 次我很天真地貼了 youtube 網址 😂<br>▲present 的 completion 本來會顯示 (() -&gt; Void)?)，按 Enter 會變大括號<br> ➞此設定自動播放影片</p><pre><strong>@IBAction</strong> <strong>func</strong> playVideo(<strong>_</strong> sender: <strong>Any</strong>) {<br>    <strong>if</strong> <strong>let</strong> url = URL(string: “https://bit.ly/2U6EoIl&quot;){<br>       <strong>let</strong> player = AVPlayer(url: url)<br>       <strong>let</strong> controller = <strong>AVPlayerViewController()</strong></pre><pre>        controller.player = player</pre><pre>        <strong>present(controller, animated: true) {</strong><br>           player.play()<br>        }<br>    }<br>}<strong> </strong></pre><blockquote><strong>不用 present 選用 show 也可以</strong></blockquote><p>▲ present 是 present modally 的 segue 效果，show 是 show 的 segue 效果<br>▲ 沒 navigation controller 就都會是 present modally 效果</p><pre><strong>show(controller, sender: nil)</strong></pre><blockquote>其餘更多顯示 iOS SDK 內建的 controller請參考<a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84%E8%A9%A6%E7%85%89-%E5%8B%87%E8%80%85%E7%9A%84-100-%E9%81%93-swift-ios-app-%E8%AC%8E%E9%A1%8C/62-%E9%80%8F%E9%81%8E-present-animated-completion-%E9%A1%AF%E7%A4%BA-ios-sdk-%E5%85%A7%E5%BB%BA%E7%9A%84-10-%E7%A8%AE-controller-5b178f792359"><strong>彼得教學</strong></a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=df5e1785320f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E9%80%8F%E9%81%8E-present-%E9%A1%AF%E7%A4%BA-ios-sdk-%E5%85%A7%E5%BB%BA%E7%9A%84-controller-df5e1785320f">透過 present 顯示 iOS SDK 內建的 controller</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[心理測驗-你把自己隱藏多深?]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/%E5%BF%83%E7%90%86%E6%B8%AC%E9%A9%97-%E4%BD%A0%E6%8A%8A%E8%87%AA%E5%B7%B1%E9%9A%B1%E8%97%8F%E5%A4%9A%E6%B7%B1-dcf25c2cb16b?source=rss-74745a788318------2</link>
            <guid isPermaLink="false">https://medium.com/p/dcf25c2cb16b</guid>
            <category><![CDATA[ibsegueaction]]></category>
            <dc:creator><![CDATA[Monica 的 iOS APP 程式設計入門]]></dc:creator>
            <pubDate>Sun, 08 Aug 2021 14:41:52 GMT</pubDate>
            <atom:updated>2021-08-11T20:59:30.714Z</atom:updated>
            <content:encoded><![CDATA[<h4>IBSegueAction 2 種寫法, popToRootViewController</h4><blockquote><strong>成品：進度落後太多想說不管美觀可做很快，結果還是搞了挺久…</strong></blockquote><p>▲已和心理測驗不熟，挑很久才用這但它圖超醜就當練習功能啦🤷🏻‍♀️<br>▲發現自己問了彼得白癡問題還問錯… 學越多腦袋越當機😵<br>▲參考來源為此<a href="https://mamaclub.com/learn/%E3%80%90%E5%9C%96%E7%89%87%E6%B8%AC%E9%A9%97%E3%80%91%E4%BD%A0%E6%8A%8A%E8%87%AA%E5%B7%B1%E9%9A%B1%E8%97%8F%E7%9A%84%E6%9C%89%E5%A4%9A%E6%B7%B1%EF%BC%9F/"><strong>網站</strong></a>，有興趣的人可以測看看！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/289/1*_Nia1N5dIBjyxzbWYsze-Q.gif" /></figure><blockquote><strong>前置作業</strong></blockquote><p>▲找心理測驗與圖，配置 Storyboard，首頁於此完成無寫程式<br>▲新增檔案： 1 swift，2 ViewController (左側藍 / 黃部分)<br>▲將由 Library 新增之 ViewController 與其各自配合 (右側上紅框 Class)<br>▲跳頁需求因此最後一頁 Storyboard ID 需取名 (右側下紅框)<br>▲加 Navigation Controller，拉Segue<br>▲ 3 個綠色 button Tag 依序設定 1，2，3</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*83wqqIlQcHs6YgkmGaBc-Q.png" /></figure><p><strong>🟣 QNR.swift</strong></p><blockquote><strong>新增 Swift File <em>用 </em></strong><strong><em>struct定義 </em>Question /<em> Result 型別：options 用 Array</em></strong></blockquote><pre><strong>import</strong> Foundation</pre><pre><strong>struct</strong> Question {<br>    <strong>let</strong> question: String!<br>   <strong> let options: [String]!   </strong><br>    <strong>let</strong> image: String!<br>}</pre><pre><strong>struct</strong> Result {<br>    <strong>let</strong> type: String!<br>    <strong>let</strong> image: String!<br>    <strong>let</strong> analysis: String!<br>}</pre><p><strong>🟣 QuestionsViewController</strong></p><blockquote><strong>拉 IBOutlet： 3 選項拉 Outlet Collection</strong></blockquote><pre><strong>@IBOutlet</strong> <strong>weak</strong> <strong>var</strong> questionLabel: UILabel!</pre><pre><strong>@IBOutlet</strong> <strong>weak</strong> <strong>var</strong> picImageView: UIImageView!</pre><pre><strong>@IBOutlet var optionsButton: [UIButton]!</strong></pre><blockquote><strong>宣告：題目此只列舉第 1 題寫法</strong></blockquote><pre><strong>var</strong> index = 0<br><strong>var</strong> point = 0</pre><pre><strong>var</strong> questions = [<br>    Question(question: “女人為何掩面？她的情緒為何？”, options: [“悲傷，<br>       發現丈夫的婚外情”, “憂慮，丈夫酒醉在床上”, “關心，丈夫病重可能即將死<br>       去”], image: “01”),<br>    ....<br>]</pre><blockquote><strong>定義 updateUI()：依 index 更換 UI，用迴圈與標題設定顯示 button 字樣</strong></blockquote><pre><strong>func</strong> updateUI() {<br>   questionLabel.text = questions[<strong>index</strong>].question<br>   picImageView.image = UIImage(named: questions[<strong>index</strong>].image)<br>   <strong>for i in 0…2 {<br>       optionsButton[i].setTitle(questions[index].options[i], for: <br>       .normal)<br>   }</strong><br>}</pre><blockquote><strong>預設畫面：第一題 UI</strong></blockquote><pre><strong>override</strong> <strong>func</strong> viewDidLoad() {<br>    <strong>super</strong>.viewDidLoad()<br>    <strong>updateUI()</strong><br>}</pre><blockquote><strong>拉 IBAction</strong></blockquote><p>▲用button sender.tag 值來看分數加 1 / 2 / 3，依 index 變換而更動題目<br>▲每次有 Array 絕對 out of range 很多次才成功 🤦🏻‍♀️<br>▲Storyboard ID 取名為最後 1 題選擇完後可用 performSegue 自動跳畫面<br>▲或許有人最後不按「再玩一次」按 Back，此處加上恢復預設畫面設定</p><pre><strong>@IBAction</strong> <strong>func</strong> optionChosen(<strong>_</strong> sender: <strong>UIButton</strong>) {<br>    <strong>if</strong> index != 6 {<br>       <strong>switch</strong> <strong>sender.tag</strong> {<br>       <strong>case</strong> 1:<br>           point += 1<br>       <strong>case</strong> 2:<br>           point += 2<br>       <strong>default</strong>:<br>           point += 3<br>       }<br>       index += 1<br>       updateUI()</pre><pre>    } <strong>else</strong> {<br>        <strong>switch</strong> sender.tag {<br>        <strong>case</strong> 1:<br>            point += 1<br>        <strong>case</strong> 2:<br>            point += 2<br>        <strong>default</strong>:<br>            point += 3<br>        }<br>        <strong>performSegue(withIdentifier: “result”, sender: nil)</strong><br>        <strong>index = 0<br>        updateUI()<br>        point = 0</strong><br>    }<br>}</pre><blockquote><strong>拉 IBSegueAction：從 segue 右鍵拉至 QuestionsViewController</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QegWyqZmLOpdfXFyKVjB2w.png" /></figure><p>▲拉完後會自動出現這個 ↓</p><pre><strong>@IBSegueAction</strong> <strong>func</strong> showResult(<strong>_</strong> coder: NSCoder, sender: <strong>Any</strong>?,   segueIdentifier: String?) -&gt; ResultViewController? {<br>    <strong>return</strong> &lt;#ResultViewController(coder: coder)#&gt;<br>}</pre><p>★去 ResultViewController 透過 property 儲存傳送的資料</p><pre><strong>var sum = 0</strong></pre><p>▲再回 QuestionsViewController 把這程式寫完<br> ➞把這頁的 point 分數資料傳到下頁的 sum</p><pre><strong>@IBSegueAction</strong> <strong>func</strong> showResult(<strong>_</strong> coder: NSCoder, sender: <strong>Any</strong>?,   segueIdentifier: String?) -&gt; ResultViewController? {<br>    <strong>let</strong> controller = ResultViewController(coder: coder)<br>    <strong>controller?.sum = point</strong><br>    <strong>return</strong> controller<br>}</pre><p><strong>🟣 ResultViewController：基本 UI 變化和之前方法一樣便不提</strong></p><blockquote><strong>分數判斷：因 sum 分數測出不同結果，而 UI 也依 index 改變</strong></blockquote><pre><strong>override</strong> <strong>func</strong> viewDidLoad() {<br>    <strong>super</strong>.viewDidLoad()</pre><pre>    <strong>switch</strong> <strong>sum</strong> {<br>    <strong>case</strong> 7, 8, 9, 10, 11:<br>        updateUI()<br>    <strong>case</strong> 12, 13, 14, 15, 16:<br>        index = 1<br>        updateUI()<br>    <strong>default</strong>:<br>        index = 2<br>        updateUI()<br>    }<br>}</pre><blockquote><strong>再玩一次</strong></blockquote><p>▲因有 Navigation 用 dismiss(animated: true, Completion: nil) 會失敗<br>▲友人教我用 popToRootViewController 回首頁</p><pre><strong>@IBAction</strong> <strong>func</strong> playAgain(<strong>_</strong> sender: UIButton) {<br>   <strong>self.navigationController?.popToRootViewController(animated:true)</strong><br>}</pre><blockquote><strong>完整程式碼 (第 1 種，較好第 2 種寫法請繼續往下看 )</strong></blockquote><blockquote><strong>QNR.swit，QuestionsViewController，ResultViewController</strong></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f01aaf756b773dbf19fc114e82b02374/href">https://medium.com/media/f01aaf756b773dbf19fc114e82b02374/href</a></iframe><p><strong>✅✅✅ 較好寫法：定義 init，由參數設定 property：只列須改之處</strong></p><p><strong>🟣 QuestionsViewController</strong></p><blockquote><strong>將原先 stored property 改掉</strong></blockquote><pre>var point = 0   改成  <strong>var</strong> point: Int!</pre><blockquote><strong>ViewDidLoad 加上 point = 0：本來沒有這行</strong></blockquote><pre><strong>override</strong> <strong>func</strong> viewDidLoad() {<br>    <strong>super</strong>.viewDidLoad()</pre><pre><strong>point = 0</strong></pre><pre>updateUI()<br>}</pre><p><strong>🟣 ResultViewController</strong></p><blockquote><strong>⭐️ 定義 init，由參數設定 property</strong></blockquote><p>▲使用常數並將原本的 var sum = 0刪除<br> ➞其實可一樣設 point 代表同個值，這只是為了區分才另取 sum<br>▲包含型別 NSCoder 的參數，並在 init 裡呼叫 super.init(coder: coder)<br> ➞因 controller 是在 storyboard 設計，controller 畫面存在 coder 裡<br> ➞初始便必須經過 init(coder:)<br>▲寫完出現紅色修正標示屬正常，請直接按 fix</p><pre><strong>let</strong> sum: Int</pre><pre><strong>required init?(coder: NSCoder, sum: Int) {<br>self.sum = sum<br>super.init(coder: coder)</strong><br>}</pre><blockquote><strong>required initializer：防止父類別的 initializer 失傳</strong></blockquote><p>▲修正後會自動出現以下要讓程式閃退的 function<br>▲但此 APP 流程永遠不會呼叫這 function，所以可以放心<br>▲相關解說請參考<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%98%B2%E6%AD%A2%E7%B5%95%E6%8A%80%E5%A4%B1%E5%82%B3%E7%9A%84-required-initializer-d3ff78f72e45"><strong>彼得教學</strong></a></p><pre><strong>required</strong> <strong>init</strong>?(coder: NSCoder) {</pre><pre><strong>fatalError(“init(coder:) has not been implemented”)</strong></pre><pre>}</pre><p><strong>🟣 再回到 QuestionsViewController</strong></p><blockquote><strong>建立要傳的資料，因</strong>只有 <strong>1 行便可省略 return 不寫</strong></blockquote><pre><strong>@IBSegueAction</strong> <strong>func</strong> showResult(<strong>_</strong> coder: NSCoder) -&gt; ResultViewController? {</pre><pre><strong>     ResultViewController(coder: coder, sum: point)</strong></pre><pre>}</pre><blockquote><strong>完整程式碼：第 2 種較好寫法</strong></blockquote><blockquote><strong>QNR.swit，QuestionsViewController，ResultViewController</strong></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/394198d3e1d0e6c6515e95c6182d77b9/href">https://medium.com/media/394198d3e1d0e6c6515e95c6182d77b9/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dcf25c2cb16b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E5%BF%83%E7%90%86%E6%B8%AC%E9%A9%97-%E4%BD%A0%E6%8A%8A%E8%87%AA%E5%B7%B1%E9%9A%B1%E8%97%8F%E5%A4%9A%E6%B7%B1-dcf25c2cb16b">心理測驗-你把自己隱藏多深?</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[仿 iOS Music App (基本題)]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/%E4%BB%BF-ios-music-app-%E5%9F%BA%E6%9C%AC%E9%A1%8C-74d545849744?source=rss-74745a788318------2</link>
            <guid isPermaLink="false">https://medium.com/p/74d545849744</guid>
            <category><![CDATA[ios-music-app]]></category>
            <dc:creator><![CDATA[Monica 的 iOS APP 程式設計入門]]></dc:creator>
            <pubDate>Sat, 31 Jul 2021 07:10:15 GMT</pubDate>
            <atom:updated>2021-09-04T19:35:52.090Z</atom:updated>
            <content:encoded><![CDATA[<h4>播放, 暫停, 上 / 下首, 調整音量</h4><blockquote><strong>成品：Gif / 影片，因 #FreeBritney 朋友提議選我們懶妮做主題</strong></blockquote><p>▲這幾年西音全是莫名怪 Rap 便聽日文為主，這次回歸馬上陷音樂漩渦<br>▲我們那年代真的超全盛爆多 icon 的啦！一回味半天就掰了😅<br>▲最後精選和懶妮合演 / 作過 4 大咖 / 金曲及她出道曲<br>▲本想挑戰進階題但搞不懂 addPeriodicTimeObserver() 只好放棄 <br>▲沒音樂時間顯示和 Slider 軌跡有夠怪還不能拉區段，但我太嫩沒辦法<br>▲音樂時間等程式很多也很難，看蘋果 Documentation 時會瞬間變文盲</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/290/1*7UvN_0CohXO10QK5_F9UPg.gif" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FLRYGa4bh230%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DLRYGa4bh230&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FLRYGa4bh230%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/aa1375d58d034d2556d17af47e8bb4fb/href">https://medium.com/media/aa1375d58d034d2556d17af47e8bb4fb/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FC-u5WLJ9Yk4%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DC-u5WLJ9Yk4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FC-u5WLJ9Yk4%2Fhqdefault.jpg&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/4fbbc8f0bb29af34eb7c146d66b8dc96/href">https://medium.com/media/4fbbc8f0bb29af34eb7c146d66b8dc96/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FclwLKJ294u4%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DclwLKJ294u4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FclwLKJ294u4%2Fhqdefault.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/e3f0c1f582f0360241bf283b1c531569/href">https://medium.com/media/e3f0c1f582f0360241bf283b1c531569/href</a></iframe><blockquote><strong>前置作業</strong></blockquote><p>▲找歌與專輯封面，我有 CD 但 Mac 沒光碟機無法直接放檔案也是好笑<br>▲ Storyboard 版面配置，因只有基本題其他功能 (圖) 就先刪掉不放<br>▲ iphone 原圖背景是封面其中幾色漸層，但彼得說那要另套件再寫程式<br>▲背景一樣放封面選擇毛玻璃效果 Visual Effect View with Blur (Library)<br>▲ button 怕之後做進階題有需要，皆把圖放 background 非一般 image</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JKJ9YZCARpUGWHrnTQgEVA.png" /></figure><blockquote><strong>有音樂所以加入 AVFoundation 函式庫 (framework)</strong></blockquote><pre><strong>import</strong> AVFoundation</pre><blockquote><strong><em>新增 Swift File </em>用 </strong><strong>struct定義 Single 型別</strong></blockquote><pre><strong>struct</strong> Single {<br>    <strong>let</strong> song: String!<br>    <strong>let</strong> singer: String!<br>    <strong>let</strong> albumCover: String!<br>}</pre><blockquote><strong>拉 IBOutlet</strong></blockquote><pre><strong>@IBOutlet</strong> <strong>weak</strong> <strong>var</strong> backgroundImageView: UIImageView!<br><strong>@IBOutlet</strong> <strong>weak</strong> <strong>var</strong> albumCoverImageView: UIImageView!</pre><pre><strong>@IBOutlet</strong> <strong>weak</strong> <strong>var</strong> songLabel: UILabel!<br><strong>@IBOutlet</strong> <strong>weak</strong> <strong>var</strong> singerLabel: UILabel!</pre><pre><strong>@IBOutlet</strong> <strong>weak</strong> <strong>var</strong> volumeSlider: UISlider!</pre><pre><strong>@IBOutlet</strong> <strong>weak</strong> <strong>var</strong> playPauseButton: UIButton!</pre><blockquote><strong>宣告：單曲資訊 Array， AVPlayer 型別的 property player</strong></blockquote><pre><strong>var</strong> singles = [Single]()<br><strong>var</strong> index = 0 <br><strong>var</strong> player = AVPlayer()</pre><blockquote><strong>初始畫面：封面圓角，Slider 指標圖 (自己做小圓球)，單曲各資訊</strong></blockquote><pre><strong>override</strong> <strong>func</strong> viewDidLoad() {<br>    <strong>super</strong>.viewDidLoad()</pre><pre>    albumCoverImageView.<strong>layer.cornerRadius</strong> = 13</pre><pre>    volumeSlider.<strong>setThumbImage</strong>(UIImage(named: “<strong>thumb2</strong>”), for: <br>    .<strong>normal</strong>)</pre><pre>    singles = [<br>        Single(song: “<strong>…Baby One More Time</strong>”, singer: “<strong>Britney</strong> <br>        <strong>Spears</strong>”,albumCover: “<strong>britney</strong>”),<br>        Single(song: “<strong>The Way You Make Me Feel</strong>”, singer: “<strong>Michael <br>        Jackson</strong>”, albumCover: “<strong>MJ</strong>”),<br>        Single(song: “<strong>Hollywood</strong>”, singer: “<strong>Madonna</strong>”, albumCover: <br>        “<strong>madonna</strong>”),<br>        Single(song: “<strong>I Want It That Way</strong>”, singer: “<strong>Backstreet <br>        Boys</strong>”,albumCover: “<strong>backstreet boys</strong>”),<br>        Single(song: “<strong>S&amp;M</strong>”, singer: “<strong>Rihanna</strong>”, albumCover:    <br>        “<strong>rihanna</strong>”)<br>  ]<br>}</pre><blockquote><strong>定義 playMusic()：單曲資訊 / 播放歌曲依 index 改變，按鍵為 pause</strong></blockquote><pre><strong>func</strong> playMusic(){<br>   backgroundImageView.image = UIImage(named: <br>   <strong>singles[index]</strong>.<strong>albumCover</strong>)<br>   albumCoverImageView.image = UIImage(named: <br>   <strong>singles[index]</strong>.<strong>albumCover</strong>)</pre><pre>   songLabel.text = <strong>singles[index].song<br>   </strong>singerLabel.text = <strong>singles[index].singer</strong></pre><pre><strong>   let fileUrl = Bundle.main.url(/service/forresource: singles[index].singer, <br>   withExtension: %E2%80%9Cmp3%E2%80%9D)!<br>   let playerItem = AVPlayerItem(url: fileUrl)</strong><br>  <strong> player.replaceCurrentItem(with: playerItem)<br>   player.play()</strong></pre><pre>   playPauseButton.<strong>setBackgroundImage</strong>(UIImage(systemName: <br>   “<strong>pause.fill</strong>”), for: <strong>.normal</strong>)<br>}</pre><blockquote><strong>拉 IBAction</strong></blockquote><pre><strong>@IBAction</strong> <strong>func</strong> playNPause(<strong>_</strong> sender: <strong>Any</strong>) {<br>}<br><strong>@IBAction</strong> <strong>func</strong> nextSong(<strong>_</strong> sender: <strong>Any</strong>) {<br>}<br><strong>@IBAction</strong> <strong>func</strong> PreviousSong(<strong>_</strong> sender: <strong>Any</strong>) {<br>}<br><strong>@IBAction</strong> <strong>func</strong> changeVolume(<strong>_</strong> sender: <strong>UISlider</strong>) {<br>}</pre><blockquote><strong>播放 / 暫停</strong></blockquote><p>▲首個條件一直想不出來直到彼得幫忙，player.currentItem 我記得你了<br>▲除 player.rate = 0 也可用 player.timeControlStatus = .paused / .playing<br>▲續播不用 player.play() 也可用 player.seek(to: player.currentTime())<br>▲有 player.play() 當然也有 player.pause()，1 個播 1 個停</p><pre><strong>@IBAction</strong> <strong>func</strong> playNPause(<strong>_</strong> sender: <strong>Any</strong>) {<br>   <strong>if player.currentItem == nil</strong> {<br>       playMusic()</pre><pre>   } <strong>else</strong> <strong>if</strong> <strong>player.rate == 0</strong> {<br>       playPauseButton.setBackgroundImage(UIImage(systemName: <br>       “<strong>pause.fill</strong>”), for: .normal)</pre><pre>       <strong>player.play()</strong></pre><pre>   } <strong>else</strong> {<br>       playPauseButton.setBackgroundImage(UIImage(systemName: <br>       “<strong>play.fill</strong>”), for: .normal)</pre><pre>       <strong>player.pause()</strong><br>   }<br>}</pre><blockquote><strong>下一首：簡單寫法是使用餘數，也可用下面上一首類似寫法</strong></blockquote><pre><strong>@IBAction</strong> <strong>func</strong> nextSong(<strong>_</strong> sender: <strong>Any</strong>) {<br>   index = <strong>(index + 1) % singles.count</strong></pre><pre>   playMusic()</pre><pre>}</pre><blockquote><strong>上一首：注意 Array.count 是總數，和陣列裡編號不一樣，所以要 -1</strong></blockquote><pre><strong>@IBAction</strong> <strong>func</strong> PreviousSong(<strong>_</strong> sender: <strong>Any</strong>) {<br>   index <strong>-=</strong> 1</pre><pre><strong>   if</strong> <strong>index &lt; 0 </strong>{<br>     index = <strong>singles.count — 1</strong><br>  <br>     playMusic()</pre><pre>   } <strong>else</strong> {<br>       playMusic()<br>   }<br>}</pre><blockquote><strong>調整音量：2 種方法</strong></blockquote><p>▲直接用 slider value 代入，手法粗糙</p><pre><strong>@IBAction</strong> <strong>func</strong> changeVolume(<strong>_</strong> sender: UISlider) {<br>   <br>   <strong>player.volume</strong> = sender.value</pre><pre>}</pre><p>▲實際調整系統音量 (下為彼得例子)<br> ➞先加 import MediaPlayer，然後在畫面上加入 MPVolumeView<br> ➞只能在實機測試，模擬器無法</p><pre>let volumeView = MPVolumeView(frame: CGRect(x: 10, y: 100, width: 300, height: 40))view.addSubview(volumeView)</pre><blockquote><strong>完整程式碼：<em>上半部為 Single (struct)，下半部為 ViewController</em></strong></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b2ca1085a3fde957b7bd92e07b824fc2/href">https://medium.com/media/b2ca1085a3fde957b7bd92e07b824fc2/href</a></iframe><blockquote><strong>參考來源</strong></blockquote><ul><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84%E8%A9%A6%E7%85%89-%E5%8B%87%E8%80%85%E7%9A%84-100-%E9%81%93-swift-ios-app-%E8%AC%8E%E9%A1%8C/94-%E6%A8%A1%E4%BB%BF-ios-%E7%9A%84-music-app-%E8%A3%BD%E4%BD%9C%E6%83%85%E6%AD%8C%E9%BB%9E%E5%94%B1%E6%A9%9F-acacd22d8928">#94 模仿 iOS 的 Music 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%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></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=74d545849744" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E4%BB%BF-ios-music-app-%E5%9F%BA%E6%9C%AC%E9%A1%8C-74d545849744">仿 iOS Music 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>