<?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 LukeWu on Medium]]></title>
        <description><![CDATA[Stories by LukeWu on Medium]]></description>
        <link>https://medium.com/@wuchiwei17?source=rss-71a5a5ee26a7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*i_hRkqU81MW0DiWRRxfAHQ.jpeg</url>
            <title>Stories by LukeWu on Medium</title>
            <link>https://medium.com/@wuchiwei17?source=rss-71a5a5ee26a7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 03:19:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@wuchiwei17/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[Auto Layout System’s Life Cycle]]></title>
            <link>https://medium.com/appworks-school/auto-layout-systems-life-cycle-e2578d3539e4?source=rss-71a5a5ee26a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/e2578d3539e4</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[autolayout]]></category>
            <dc:creator><![CDATA[LukeWu]]></dc:creator>
            <pubDate>Thu, 06 May 2021 03:17:36 GMT</pubDate>
            <atom:updated>2021-05-06T03:17:36.520Z</atom:updated>
            <content:encoded><![CDATA[<p>Apple 為了解決 iOS 多種手機尺寸的頁面設計問題，早在多年前就已經開發了一套佈局工具 : Auto Layout System。不過有一些 UI 設計，需要 Auto Layout 與其他設定搭配的時候，比如說單純調整 layer 的大小，或是切圓角，就會有一些問題，這是使用 Auto Layout 時，最常發生異常的狀況，之所以寫這邊文章，就是希望協助新手對 Auto Layout 的運作機制，有更充分的理解！</p><p>首先，讓我們透過建立一個有紅色圓形的畫面開始，產生圓形的條件有：</p><ol><li>必須是一個長寬相同的 View，並把它加到畫面上。</li><li>UIView 的 layer.cornerRadius 值剛好要跟 view 的 frame 長寬的一半</li><li>UIView 的 clipsToBounds property 必須是 <strong>true</strong></li></ol><h3>透過 Frame 的計算來實現</h3><p>我們先產生一個 testView，並指定它的 Frame，最後根據 testView 的 <strong>Width </strong>來設定 cornerRadius 的 value。</p><p>透過以下的程式碼，我們可以在畫面上，產生一個紅色的小圓形。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/caab63d7bba5b0a7e608be89e9fd759f/href">https://medium.com/media/caab63d7bba5b0a7e608be89e9fd759f/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/284/1*7UqpCunxLeIIH5zt3ctfgg.png" /></figure><h3>透過 Auto Layout 來實現</h3><p>我們一樣先產生一個 testView，並指定他的 <strong>Auto Layout Constraint</strong>，最後一樣根據 testView 的 <strong>width </strong>來設定 cornerRadius 的 value。這一次畫面不會出現圓形，反而出現了一個正方形的 View。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e6bcf2c41786ced75cabf3507b67a10/href">https://medium.com/media/3e6bcf2c41786ced75cabf3507b67a10/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/365/1*npM3k-RWhGqEEUgiKQqSaA.png" /></figure><h3><strong>釐清問題</strong></h3><p>看來我們遇上了 Bug，讓我們回過頭來看剛剛產生圓形的三個條件：</p><ol><li>必須是一個長寬相同的 View，並把它加到畫面上。</li><li>UIView 的 layer.cornerRadius 值剛好要跟 View 的長寬的一半</li><li>UIView 的 clipsToBounds property 必須是 <strong>true</strong></li></ol><p>1、3 項在前後兩個做法，都沒有做到修改，眼前比較有可能出現問題的，就是第 2 點，讓我們來查一下這一行：</p><p>testView.layer.cornerRadius = testView.frame.width / 2</p><p>如果你將 testView.frame.width / 2 印出來看：在 Frame 的寫法，他會是 50，但在 Auto Layout 的寫法，卻會是 0，也就是 testView.frame.width 的數值是 0。</p><p>看來問題就出在這了：</p><blockquote>我們明明有下 Auto Layout Constraints，為什麼寬不是 100 ?</blockquote><h3>為什麼 Auto Layout 不 Work ?</h3><p>看到這個結果，我們第一個想法通常是：為什麼 Auto Layout 不 work ?</p><p>但讓我們靜下心來看一下，最後我們的 testView 其實有出現在畫面上，長寬跟位置基本上跟我們 Auto Layout 的設定差不多，感覺 Auto Layout 是有 Work 啊，為什麼我會拿到一個 Width 為 0 的 Frame?</p><p>事實上，Auto Layout 的確有 Work，你也確實拿到了一個 Width 是 0 的 Frame。</p><blockquote>那到底是誰在搞鬼?</blockquote><h3>Auto Layout 的機制</h3><p>事實上當我們在設定跟規劃 Auto Layout 的時候，<strong>我們是在描述一些位置跟大小的關係</strong>，比如說 testView 的 Leading 要跟 view 的 Leading 距離多少， testView的 top 距離 view 的 top 要多少；<strong>我們並沒有真正的指定 x, y, width, height 任何一個屬性。</strong></p><p>這些位置跟大小的關係(Constraints)，會在適當的時候被 Auto Layout System 計算，並轉換成 Frame，套用到每一個有使用 Constraint 的 View 上面。</p><p>所以上面產生正方形紅色 View 的例子，我們讀取到 frame.width 為 0，<strong>是因為我們在 Auto Layout System 還沒作用前就去存取 frame</strong>，拿到的當然就不會是最終的結果。</p><h3>Auto Layout System 什麼時候會計算完畢</h3><p>那到底我們什麼時候去 Access View Frame 才會拿到計算完的結果？iOS 在 UIViewController Class 裡面留了兩個跟 Auto Layout System 有關的 Method：</p><ul><li><strong>viewWillLayoutSubviews()</strong> - <a href="https://developer.apple.com/documentation/uikit/uiviewcontroller/1621437-viewwilllayoutsubviews">Apple Doc Reference</a></li></ul><blockquote>在整個 Auto Layout System 開始運作之前，iOS System 會先執行 viewWillLayoutSubviews，如果在 auto layout 開始之前想做一些設定，可以 override 這個 method，原生的 implementation 內沒有做任何事情。在這個 method 裡面，Auto Layout System 是<strong>即將</strong>開始計算，也就是在這邊拿到的 Frame，<strong>並不是最終的結果</strong>。</blockquote><ul><li><strong>viewDidLayoutSubviews()</strong> - <a href="https://developer.apple.com/documentation/uikit/uiviewcontroller/1621398-viewdidlayoutsubviews">Apple Doc Reference</a></li></ul><blockquote>當 Auto Layout System 完成了計算，並把結果套到每一個 View 上面後，iOS 系統會來執行這個 method。也就是在這個 method 裡面，我們去 Access View 的 Frame，會拿到計算完的結果。</blockquote><blockquote>如果你希望藉由 Auto Layout System 算完的結果，來做一些運算或是設計，就可以把相關的程式碼寫在 <strong>viewDidLayoutSubviews</strong> method 裡面。</blockquote><p>另外還有一個 View Life Cycle 的 method 你也可以拿到 Auto Layout 計算完的結果：</p><ul><li><strong>viewDidAppear(_ animated: Bool) - </strong><a href="https://developer.apple.com/documentation/uikit/uiviewcontroller/1621423-viewdidappear">Apple Doc Reference</a></li></ul><blockquote>在整個 UIViewController 所管理的 View 已經出現在畫面上之後，Auto Layout 一定也算好了，所以在這個 Method 裡面，我們也可以拿到 Auto Layout 計算完的結果。</blockquote><blockquote>原來一切都是時機不對</blockquote><h3><strong>viewDidLayoutSubviews 與 viewDidAppear 的取捨</strong></h3><p>這兩個 method 都可以拿到 Auto Layout 計算完的結果，那我們要把程式碼寫在哪一個好？讓我們先從這兩個 Method 的特性說起：</p><ul><li><strong>viewDidLayoutSubviews :</strong></li></ul><ol><li><strong>執行次數：</strong></li></ol><p>只要畫面有更新，Layout 有變化，就有可能會被執行；一個畫面從無到有的過程，<strong>基本上這個 method 通常會被系統執行很多次</strong>。</p><p><strong>影響：</strong>如果在這裡寫了一些運算時間比較長的程式碼，基於 method 會被執行很多次，要有心裡預期這可能會影響效能。</p><p>2. <strong>畫面出現與否：</strong></p><p>在一個畫面從無到有出現的過程，viewDidLayoutSubviews 被執行的時候，view 基本上都尚未出現在螢幕上，也就是如果你在這個 method 去調整畫面的設定，使用者都不會發現（因為畫面還沒出現)。</p><p>但必須要注意的是，畫面出現之後，<strong>只要畫面需要更新，則這個 Method 會再次被呼叫</strong>。</p><p>ex: 使用者旋轉手機導致畫面旋轉。</p><ul><li><strong>viewDidAppear：</strong></li></ul><ol><li><strong>執行次數</strong>：</li></ol><p>一個畫面從無到有的過程，基本上這個 method 通常只會被執行一次。但在不同 Scene 之間來回，則這個 Method 一樣會再次被執行。</p><p>ex：切換 Tab, Navigate 到下一個 Scene 再 Back 回來。</p><p>2. <strong>畫面出現與否：</strong></p><p>基本上這個 Method 被執行的時候，畫面已經出現在手機上，使用者也可以看得到畫面。</p><p><strong>影響：</strong>在這個 Method 內去調整畫面，<strong>使用者有機會觀察到畫面的變化</strong>。</p><p>以一般的應用來說，我們通常會將畫面的調整寫在 <strong>viewDidLayoutSubviews </strong>裡面，避免讓使用者看到畫面閃爍。但有時候也可以透過一些手法，比如果說一開始將畫面藏起來，<strong>viewDidAppear</strong> 再去調整畫面，搭配淡入的動畫，也可以達到同樣的效果。</p><h3>相關的 Method</h3><h4>In UIView Class:</h4><ul><li><strong>layoutSubviews - </strong><a href="https://developer.apple.com/documentation/uikit/uiview/1622482-layoutsubviews">Apple Reference Doc</a></li></ul><p>當 Auto Layout System 安排好這個 Method 的 Receiver 底下的 Subviews 時，系統會來執行這個 Method，我們在裡面可以拿到 SubViews 計算完的 Frame。</p><ul><li><strong>layoutIfNeeded</strong> - <a href="https://developer.apple.com/documentation/uikit/uiview/1622507-layoutifneeded">Apple Reference Doc</a></li></ul><p>強制 Auto Layout System 立刻根據 Constraints 更新 Receiver 底下 SubViews 的 Frame。</p><ul><li><strong>setNeedsLayout</strong> - <a href="https://developer.apple.com/documentation/uikit/uiview/1622601-setneedslayout">Apple Reference Doc</a></li></ul><p>Invalid 現有的 Constraints, 並在下一個 Layout Cycle 以新的 Constraints 更新畫面。</p><p>最後有一點值得提出來說明的是：</p><p><strong>viewDidLayoutSubviews</strong> 被執行並不代表底下 View 的個別 SubViews 都被給予最終 Frame 了，<strong>各自的 View 底下的 SubView 還是由他們自身的 Layout Cycle 控制，也就是在 layoutSubviews 裡面才能拿到最終的大小。</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kWT_3AsgbFy2JjmYodFcNQ.png" /><figcaption>Snapshot From Apple Doc</figcaption></figure><h3>結論</h3><p>使用 Auto Layout 常常讓人有已經安排好畫面的錯覺，事實上必須要在對的時機點去 Access View Frame 才能拿到正確的結果，也就是 <strong>viewDidLayoutSubviews </strong>以及 <strong>viewDidAppear </strong>這兩個 Method。有時候你希望他先把 Auto Layout 算完，則可以透過 UIView 的 <strong>layoutIfNeed</strong>，強迫 Auto Layout System 立刻計算。</p><p>一但了解了 Auto Layout 的機制，在一些特殊畫面控制上，就會更加得心應手，也不會再對於畫面的失控，百思不得其解囉。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e2578d3539e4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/appworks-school/auto-layout-systems-life-cycle-e2578d3539e4">Auto Layout System’s Life Cycle</a> was originally published in <a href="https://medium.com/appworks-school">Aiworks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[OOP: Reference Type And Value Type in Memory]]></title>
            <link>https://medium.com/appworks-school/oop-reference-type-and-value-type-in-memory-b74d4df4bb06?source=rss-71a5a5ee26a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/b74d4df4bb06</guid>
            <category><![CDATA[value-type]]></category>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[memories]]></category>
            <category><![CDATA[reference-type]]></category>
            <dc:creator><![CDATA[LukeWu]]></dc:creator>
            <pubDate>Thu, 21 Mar 2019 01:28:15 GMT</pubDate>
            <atom:updated>2019-04-22T15:08:36.089Z</atom:updated>
            <content:encoded><![CDATA[<p>物件導向程式設計 (OOP, Objective-Oriented-Programming) 是軟體開發其中一個重要的概念，而了解 Reference Type 的特性則是接觸 OOP 的起手式，這是在學習程式開發的過程，其中一個新手比較難跨過越的門檻。</p><p>本文試著從記憶體的角度出發，比較 Reference Type 與 Value Type 在記憶體中行為上的差異，希望能夠幫助大家更了解 Reference Type 與 Value Type。</p><h3>Memory</h3><p>要談 Reference type 跟 Value type 之前，應該要先從了解實際的記憶體 (Random Access Memory，簡稱 RAM) 長什麼樣子開始：</p><p>記憶體（RAM）是一個一個的儲存空間，功能為儲存資料，每一個記憶體都有自己的記憶體位址 (Memory Address)，電腦就可以透過這個 Memory Address 找到特定的記憶體，拿出裡面所儲存的資料；你可以把記憶體想像成是一個一個的貨櫃，可以儲存貨物，每一個貨櫃，都有特定的編號，方便尋找與紀錄。</p><p>而現實世界中的各種資料，在電腦的世界都會被轉化成 1010…0101 的形式儲存，也就是所謂的 Binary data ，需要的時候再透過既定的方法，將 Binary data 轉成現實世界中，人類看得懂的資料格式。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*ji3Ul5JloMAq81F3nElMtg.jpeg" /></figure><h3>Value Type</h3><p>以 iOS 所使用的 Swift 語言為例，當我們寫下這樣的程式碼，到底電腦做了哪些事：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10a51ccf039d75f0d104741fe32bf7a2/href">https://medium.com/media/10a51ccf039d75f0d104741fe32bf7a2/href</a></iframe><blockquote><strong>=</strong> 這個符號，在 Swift 中稱為 Assignment Operator，它會將位於 Operator 右邊的資料，儲存到左邊的變數中。如果在右手邊的是一個運算式 (Expression)，它則會將右邊運算式的結果，儲存到左邊的變數中。</blockquote><p>而當電腦執行到 var price = 4，會做以下的事情：</p><p>1. 先在 RAM 中，找到一個閒置的 memory。</p><p>2. 將 price 這個變數，與我們剛剛找到的 memory 之 memory address 關聯起來。</p><p>3. 將 4 這個數字，轉成 Binary data，並儲存在這個 memory 中。</p><p>4. 下次我們再使用到 price 這個變數的時候，電腦就會知道我們指的是這個 memory address，繼而去找到這個 memory，將其儲存的資料取出來使用。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*5J2imLZBTCbndwg_FbHMog.jpeg" /></figure><p>接著，var anotherPrice = price</p><p>1. 先在 RAM 中，找到一個閒置的 memory。</p><p>2. 將 anotherPrice 這個變數，與我們剛剛找到的 memory 之 address 關聯起來。</p><p>3. 找到變數 price 所對應到的 memory，將其儲存的資料複製一份，放到 anotherPrice 變數所對應到的 memory。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*cKjtTG9-8RaMSSV3LWXwDg.jpeg" /></figure><p>最後，anotherPrice = 10</p><p>1. 找到 anotherPrice 所對應的 memory。</p><p>2. 將 10 轉成 binary data 並取代原先的資料。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*G4uov4pp2eljkIVDoelDiw.jpeg" /></figure><p><strong>price</strong> 與 <strong>anotherPrice</strong> 擁有各自的資料，存放在各自相對應的記憶體內，兩變數並不是共用一份資料，因此執行完 anotherPrice = 10 之後，我們將兩個變數各自 print 出來，會發現 <strong>price</strong> 所儲存的內容並沒有被改變，這種資料的 <strong>獨立性</strong>，就是 <strong>Value Type</strong> 的特性。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/015db01901583f82769aa12d06dc8880/href">https://medium.com/media/015db01901583f82769aa12d06dc8880/href</a></iframe><p>Console 將會印出：</p><pre>Price is 4.<br>AnotherPrice is 10.</pre><h3>Reference Type</h3><p>接著，讓我們來看看 Reference Type 是什麼樣子，以 iOS 所使用的 Swift 為例：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/78547dbef76df1e433c8310817d62651/href">https://medium.com/media/78547dbef76df1e433c8310817d62651/href</a></iframe><blockquote>UILabel 為 iOS 很常使用的 Class，通常用來顯示文字內容。</blockquote><p>而當電腦執行到 <strong>var label = UILabel()</strong>，會做以下的事情：</p><p>1. 先在 RAM 中，找到 <strong>一塊</strong> 足夠大的，閒置的 memory 區塊。</p><p>2. 將 UILabel() 這個 instance，儲存在這個 memory 區塊中。</p><p>3. 再找到 <strong>一個</strong> 閒置的記憶體，將 label 這個變數與此記憶體位址關聯起來。</p><p>4. 將先前存放 instance 的 memory 區塊中第一個 <strong>memory address</strong> 儲存到 label 變數所對應的記憶體內。</p><p>5. 下次我們再使用到 label 這個變數的時候，電腦就會知道我們指的是這個 memory address 為起點的<strong>記憶體區塊</strong>，繼而去找到這個區塊以及其內所處存的 instance。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*H6lxsgLAdXfWK9WX9_Ovpw.jpeg" /></figure><p>接著，<strong>var anotherLabel = label</strong> :</p><p>1. 找到 <strong>一個</strong> 閒置的記憶體。</p><p>2. 將 anotherLabel 這個變數與此記憶體位址關聯起來。</p><p>3. 將 label variable 中所儲存的資料，複製一份到 anotherLabel 中。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*DMyWDINOKfV010OCx4IbIA.jpeg" /></figure><p>當我們使用一個變數，且此變數儲存的是 <strong>Reference Type</strong> 時，記憶體內儲存的是 <strong>記憶體區塊的 memory address。</strong></p><blockquote>因為 Class 描述的是一個物件，而一個物件的資料會超過單一記憶體所能容納的大小，因此我們需要的是 <strong>一塊</strong> 記憶體，而不是 <strong>一個</strong> 記憶體。這與 Value Type 只需要 <strong>一個</strong> 記憶體，且儲存 <strong>具體資料</strong>，有所不同。</blockquote><p>而當我們需要這個 instance 的時候：</p><p>1. 將變數所對應之記憶體內，所儲存的 memory address 取出來。</p><p>2. 透過此 memory address，找到 instance，並對之做操作。</p><p>讓我們將兩個變數所存之 instance 的 text 印出來看看：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c016680c800c150f7a13b6afdf18051e/href">https://medium.com/media/c016680c800c150f7a13b6afdf18051e/href</a></iframe><p>Console 將會印出：</p><pre>Text in label variable: AppWorks School<br>Text in anotherLabel variable: AppWorks School<br>---<br>Text in label variable: Awesome swift!<br>Text in anotherLabel variable: Awesome swift!</pre><p><strong>label</strong> 與 <strong>anotherLabel</strong> 所儲存的記憶體位址 (0001230078901234) 相同，也就是透過這兩個變數，我們會找到同樣的 instance。<strong>anotherLabel</strong> 對 instance 做的行為，是可以透過 <strong>label</strong> 觀察到的。這與 Value Type，每個變數各自擁有獨立的資料，互不影響，是不一樣的運作模式。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*n8IesGVu327WYcAUfW3Irw.jpeg" /></figure><h3>Conclusion</h3><p>Reference Type 與 Value Type 的差別，就是在記憶體內所儲存的內容，Reference Type 儲存 instance 的 <strong>記憶體位址</strong>，而 Value Type 儲存 <strong>資料本身</strong>，於是 Reference Type 的變數，會修改到同一個 instance，而 Value Type 不會。</p><p>Reference Type 的觀念，是學習寫程式的過程一定會碰到的東西，而記憶體與 Reference Type 的關聯，也是我寫程式一段時間之後，才有比較深刻的體悟。透過這篇文章，希望能給一樣剛接觸這領域的人一點幫助。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b74d4df4bb06" width="1" height="1" alt=""><hr><p><a href="https://medium.com/appworks-school/oop-reference-type-and-value-type-in-memory-b74d4df4bb06">OOP: Reference Type And Value Type in Memory</a> was originally published in <a href="https://medium.com/appworks-school">Aiworks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Google Analytics in iOS — 2]]></title>
            <link>https://medium.com/appworks-school/google-analysis-in-ios-2-4fd6de5671c8?source=rss-71a5a5ee26a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/4fd6de5671c8</guid>
            <category><![CDATA[appworks-school]]></category>
            <category><![CDATA[google-analytics]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[LukeWu]]></dc:creator>
            <pubDate>Tue, 14 Nov 2017 09:21:22 GMT</pubDate>
            <atom:updated>2017-11-14T09:27:06.616Z</atom:updated>
            <content:encoded><![CDATA[<p>在<a href="https://medium.com/appworks-school/google-analysis-in-ios-1-d4f1b89497aa">上一篇</a>介紹了 Google Analytics iOS SDK 裡面的 Screens，Events，E-commerce，Sessions。接著這篇會再談到 Advertise，Crashes &amp; Exceptions，Social Interactions，User ID，User Timing。</p><h3>Advertise</h3><p>目標對象報表提供非常詳細的，關於產品訪客的資訊，包含訪客的性別，年齡，國家/地區等，透過目標對象報表你可以描繪出訪客族群，以及他們的特徵，甚至我們可以做出完整的客群分析。如果你想獲得使用者的年齡，性別，興趣等資料，必須要開啟 GA 的 IDFA 功能，並在 App 送審的時候，勾選有使用 IDFA（Advertising Identifier，Apple 用於追蹤用戶的廣告識別碼。）。但必須要注意的是這些顧客資料都是 Google 推算出來的，並非真實資料，使用上要小心。</p><pre>tracker = [[GAI sharedInstance] trackerWithTrackingId:@&quot;UA-1234-2&quot;];</pre><pre>tracker.allowIDFACollection = YES;</pre><p>Advertise 的報表可以在 <strong>Google Console → 目標對象</strong> 裡面找到相關的紀錄。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0kDy3Yyt6q-m8e2H2GCxkw.png" /></figure><h3>Crashes &amp; Exceptions</h3><p>如果使用者在使用我們開發的 App 遇到閃退，肯定會對這個 App 有不好的印象，甚至就不使用這款產品了，所以 Developer 必須要在第一時間知道這個問題並修復它，這時候就需要 Crash log 來輔助。Google Analytics 提供兩種不同的方式來記錄 Crash 跟 Exception，先看 Exception 的定義：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/991/1*QXfTtAattYRJgcn1xF1VRw.png" /></figure><p>發送 Exception 到 GA 時，有兩個欄位可以讓我們填寫，分別是 description 與 isFatal，他們都有各自定義好的 key。GA API 裡面的 GAIDictionaryBuilder Class 也有專門為製作 Exception 的 method 可以使用。</p><pre>[self.tracker send:[[GAIDictionaryBuilder createExceptionWithDescription:description withFatal:@NO] build]];</pre><p>App Crash 的搜集則是另外一種方法，透過設定 GAI 的 singleton 的 trackUncaughtExceptions property 為 YES，當App 發生非預期的錯誤，系統就會自動發送 crash 報告到 Google Analysis。</p><pre>gai.trackUncaughtExceptions = YES;</pre><p>所有被 Google 自動捕捉到的 Crash 在報告中都會標註成 fatal 屬性 ( fatal = YES )。Description 欄位會包含 exception type, class name, method name 還有 thread name。可以在 <strong>Google Console → 行為 → 當機和例外狀況 </strong>裡面找到 Crash &amp; Exception 的紀錄。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9v74mGEB2-ujz3CYciRffw.png" /></figure><h3>Social Interactions</h3><p>Social Interactions 可以幫助我們紀錄每一筆的分享，或是 App 提供的任何分享管道。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/995/1*J8IsXNk4mDz11qJvRMqhTA.png" /></figure><pre>id tracker = [[GAI sharedInstance] defaultTracker];</pre><pre>NSString *targetUrl = @&quot;https://developers.google.com/analytics&quot;;</pre><pre>[tracker send:[[GAIDictionaryBuilder <br>                createSocialWithNetwork:@&quot;Twitter&quot; <br>                                 action:@&quot;Tweet&quot; <br>                                 target:targetUrl] build]];</pre><p><strong>目前這個項目的資料只能在客製化的報告中獲得，本文沒有特別去嘗試這個部分。</strong></p><h3>User ID</h3><p>透過 GA 裡面 UserID 的特性，可以將同一個使用者，在不同裝置上的行為都記錄起來，形成更具意義的資料。</p><pre>[self.tracker set:kGAIUserId value:@&quot;am_luke&quot;];</pre><h3>User Timings</h3><p>紀錄 App 內的讀取資源時間，比如可以拿來量測每一個 request 拿到 response 所需的時間。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/998/1*VdyFp0uRpG0adYSXVMKmnw.png" /></figure><p>可以在 G<strong>oogle Console → 行為 → 應用程式速度 </strong>裡面找到相關的報表。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*79MJ2koXAGXK6Qb_CjTyQA.png" /></figure><p>這一個系列有關 Google Analytics 的兩篇文章，是我自身練習使用 Google Analytics iOS SDK 的紀錄，如果內容有誤，還請各位不吝指教，感謝你。</p><p>上一篇：<a href="https://goo.gl/rb28aa">Google Analytics in iOS — 1</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4fd6de5671c8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/appworks-school/google-analysis-in-ios-2-4fd6de5671c8">Google Analytics in iOS — 2</a> was originally published in <a href="https://medium.com/appworks-school">Aiworks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Google Analytics in iOS — 1]]></title>
            <link>https://medium.com/appworks-school/google-analysis-in-ios-1-d4f1b89497aa?source=rss-71a5a5ee26a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/d4f1b89497aa</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[google-analytics]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[appworks-school]]></category>
            <dc:creator><![CDATA[LukeWu]]></dc:creator>
            <pubDate>Tue, 14 Nov 2017 09:19:12 GMT</pubDate>
            <atom:updated>2017-11-14T09:25:38.763Z</atom:updated>
            <content:encoded><![CDATA[<p>當我們想要在 App 裡面追蹤使用者行為時，就需要第三方服務的支援，像是 <a href="https://developers.google.com/analytics/devguides/collection/ios/v3/">GoogleAnalysis</a>，<a href="https://mixpanel.com/">Mixpanel</a> 等。本文介紹 Google Analytics iOS SDK 及相關報表，包括 Screens，Events，E-commerce，Sessions。第二篇則會介紹 Advertise，Crashes &amp; Exceptions，Social Interactions，User ID，User Timing。</p><p>這個系列適合對 Objective-c 及 iOS 開發有初步熟悉度的讀者。</p><h3>Dispatch</h3><p>GA 的資料收集，當天的資料必須在隔天的 4 a.m. 以前送到 GA，否則就不會出現在當天報表上。iOS 裡面預設的 dispatch time 是 2 分鐘。</p><pre>[GAI sharedInstance].dispatchInterval = 120.0;</pre><p>如果把這個 property 設定比 1 還小，則會 disable auto dispatch，資料會等待上傳：</p><pre>[GAI sharedInstance].dispatchInterval = 0;<br>//Call this method to manually send data<br>[[GAI sharedInstance] dispatch];</pre><h3>Event Tracking</h3><p>App 裡面會有各式各樣使用者所觸發的事件，GA 也提供了相關的 API 來監控這些事件，先來看看有哪些參數：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kWvPCJ-C7goR7DmmFjU8pA.png" /></figure><p>Category 是 Event 的大分類，每個 Category 裡面再由 Action 來區分事件。</p><p>你可以透過 GAITracker send method，把參數個別填入之後，送出事件。</p><pre>id&lt;GAITracker&gt; tracker = [[GAI sharedInstance] defaultTracker];</pre><pre>[tracker send:@{kGAIEventCategory: @”Game”,</pre><pre>                  kGAIEventAction: @”Attack”}];</pre><p>或者透過 GA 的 <strong>GAIDictionaryBuilder</strong> class 所建立的 method 來送出 event。</p><pre>[tracker send:[[GAIDictionaryBuilder    <br>                createEventWithCategory:@”ui_action” <br>                                 action:@”button_press”<br>                                  label:@”play”<br>                                  value:nil] build]];</pre><p>透過上述的兩種方法，Google 就可以幫我們紀錄 User 與 App 的互動事件。Event tracking 的報表可以在 G<strong>oogle Console → 行為 → 事件</strong> 裡面找到相關的紀錄。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9x6i_By8Ptkhbi2OXW3SIg.png" /></figure><h3>Screens</h3><p>Screens 在 GA 裡面代表使用者在看的畫面，透過收集這個數據，可以知道使用者停留最長的頁面是哪一個，以及使用者的瀏覽 app 的狀態。先來看一下 Screens 的 key：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SqgOreTyc7TwASAirO3AJQ.png" /></figure><p>GA 有提供兩個方法來送出 Screen hit，一個是手動送出，一個是繼承 <strong>GAITrackedViewController</strong>：</p><p>手動送出：</p><pre>id&lt;GAITracker&gt; tracker = [[GAI sharedInstance] defaultTracker];</pre><pre>[tracker set:kGAIScreenName value:@”Home Screen”];</pre><pre>[tracker send:[[GAIDictionaryBuilder createScreenView] build]];</pre><p>繼承 GAITrackedViewController：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5dff5ea2deec28f4b8163fca99ee2395/href">https://medium.com/media/5dff5ea2deec28f4b8163fca99ee2395/href</a></iframe><p>Screen tracking 的報表可以在 <strong>Google Console → 行為 → 畫面</strong> 裡面找到相關的紀錄。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U7ASvVQixUAN-ag4tmfAlA.png" /></figure><h3>Commerce Tracking</h3><p>這個部分介紹如何運用 Google Analysis API 來監控 In app purchase 及相關事件。在 Google Analysis 裡面，e-commerce tracking 由兩部分構成，分別是 Transaction 還有 Item，兩者由共用的 `Transaction ID` 關聯起來。</p><p>Transactions 參數定義：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Yk91fQF3VsdzH9LgUp86Yg.png" /></figure><p>Item 參數定義：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*541HSeqGvzgJA75QksSo-g.png" /></figure><p>下列是 Sample Code，要注意 Transaction 與 Item 的 Transaction Id 要使用相同的內容：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*ItqC-S27lzBFeGDHf7q0Sg.png" /></figure><p>Commerce Tracking 的數據可以在 <strong>Google Console → 轉換 → 電子商務</strong> 裡面找到。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6ycPYVyCsEL_oSZeHxmCFw.png" /></figure><h3><strong>Session</strong></h3><p>Session 在 GA 裡面代表一個使用者與應用程式互動的時間區間。Session 是一個很好的 Container，可以將 screen views, events, e-commerce transaction 都包含在內。</p><p>手動建立 session：</p><pre>GAIDictionaryBuilder *builder = [GAIDictionaryBuilder createScreenView];</pre><pre>[builder set:@”start” forKey:kGAISessionControl];</pre><pre>[tracker set:kGAIScreenName value:@”My Screen”];</pre><pre>[tracker send:[builder build]];</pre><p>你並不需要真的去結束一個 session，如果你有需要，可由下列方法達成：</p><pre>[builder set:@”end” forKey:kGAISessionControl];</pre><p>以上簡短介紹了 Google Analytics 的部分 API，剩餘部分會在下一篇文章做完介紹，如果內容有誤，還請各位不吝指教，感謝你。</p><p>下一篇：<a href="https://goo.gl/gvSCmQ">Google Analytics in iOS — 2</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d4f1b89497aa" width="1" height="1" alt=""><hr><p><a href="https://medium.com/appworks-school/google-analysis-in-ios-1-d4f1b89497aa">Google Analytics in iOS — 1</a> was originally published in <a href="https://medium.com/appworks-school">Aiworks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[iOS Parallax View On The Top Of TableView]]></title>
            <link>https://medium.com/appworks-school/ios-parallax-view-on-the-top-of-tableview-1a954c4d262d?source=rss-71a5a5ee26a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/1a954c4d262d</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[appworks-school]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[LukeWu]]></dc:creator>
            <pubDate>Wed, 28 Jun 2017 02:34:40 GMT</pubDate>
            <atom:updated>2017-12-14T01:02:48.397Z</atom:updated>
            <content:encoded><![CDATA[<p>本篇文章將按步驟詳細說明，如何在 iOS TableView 中實作 Parallax 的其中一個視覺效果，Demo Project 使用 <strong>XCode 8、Swift 3</strong>，適合讀者為：</p><ol><li>了解 ScrollView 的 ContentOffset Property 以及 Delegate Design Patten。</li><li>熟悉 ImageView 的 ContentMode Property。</li><li>對 AutoLayout 有基本了解。</li></ol><p>下圖為想要實現的效果，上方的 ImageView 會跟著下拉的動作產生相對應的變化。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*tmcGxqUKUHiFb2eiBXoWTw.gif" /></figure><p><strong>Step 1 : Download Start Project From </strong><a href="https://www.dropbox.com/sh/oczmixcsr7yyd7j/AAA6cwnw6Vp8Zb6MbgmCd9bBa?dl=0"><strong>Dropbox</strong></a><strong>. (按此下載)</strong></p><p>下載後，打開 <em>ParallaxDemo.xcodeproj</em>，裡面已經有一個寫好的 TableView，在每一個 Cell 裡面顯示每一個各自的 Row Number。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*9cLp_iCoH2iN0TKlVByKrw.jpeg" /></figure><p><strong>Step 2 : 透過 TableView 的 ContentInset property，把 TableView 往下位移，騰出上方的空間，給後續要置入的 ImageView 使用。</strong></p><p>在 DemoTableViewController 裡新增一個 imageViewLength property :</p><pre>class DemoTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {</pre><pre>//1<br>    let imageViewHeight: CGFloat = 200.0<br>    <br>    ...</pre><pre>}</pre><p>在 DemoTableViewController 裡面的 setUpTableView() function 加入下列程式碼：</p><pre>func setUpTableView() {</pre><pre>//2<br>    tableView.contentInset = UIEdgeInsets(top: imageViewHeight, left: 0, bottom: 0, right: 0)</pre><pre>//3<br>    tableView.contentOffset = CGPoint(x: 0, y: -imageViewHeight)</pre><pre>    <br>    ...<br>}</pre><p>以下說明每一段程式碼的作用：</p><ol><li>設定要留給 ImageView 的高度，將之存在 ImageViewHeight property 中，以供後續使用。</li><li>設定 TableView 的 ContentInset，這個屬性的資料型態是 UIEdgeInsets，透過分別設定 top, left, bottom, right，可以調整 TableView 的 Content view 與底下 ScrollView 的相對距離。這裡我們把 ImageViewHeight 作為UIEdgeInset initializer 的 top 參數，並將這個新生成的 UIEdgeInset 物件指定給 TableView 的 ContentInset Property。</li><li>設定好 ContentInset 後，為了讓一進來 App 的時候，顯示上方的空白，我們還必須設定 TableView 的 ContentOffset，讓上方的空白可以被看見。</li></ol><p>設定完這些指令之後，跑模擬器就可以看到下方的畫面：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/332/1*n94-R9EJA6bNfW13MS--cA.jpeg" /></figure><p><strong>Step 3 : 新增 ImageView</strong></p><p>在 DemoTableViewController 裡新增 setUpImageView function :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5eaa20538f7919d5cd9586199a6702d/href">https://medium.com/media/b5eaa20538f7919d5cd9586199a6702d/href</a></iframe><p>並修改 setUp() function：</p><pre>func setUp() {</pre><pre>    ...</pre><pre>    //Add Statement</pre><pre>    setUpImageView()<br>}</pre><p>說明：setUpImageView() 裡面做的事情就是指定 ImageView 所放的 Image，並設定 AutoLayout Constraint。其中重要的是要將 ContentMode 設定成 ScaleAspectFill，這會讓系統自動調整圖片大小，直到圖片的長短邊都充滿整個 ImageView 為止，如果沒有設定這個步驟，Parallax 的效果有可能會失敗，這部分要特別小心。而 ClipToBounds 就是將超出 ImageView 的部分隱藏起來，這樣就不會遮住畫面上的其它元件。</p><p>新增完 setUpImageView function 後，跑模擬器就可以看到下方的畫面：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/332/1*SojognGyZQph7TWKkatu5w.jpeg" /></figure><p>接下來就進到更重要的部分，UIScrollView 有相對應的 Protocol: UIScrollViewProtocol，會在發生一些事件的時候通知委派物件，執行相對應的 method，而 UITableViewDelegate Protocol 繼承自 UIScrollViewDelegate，自然也繼承了這些 method。我們將會用到 scrollViewDidScroll() 這個 function。</p><p>將下列的程式碼新增到 DemoTableViewController 中：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8577752b37ed48e1b0acc24bea68d1dd/href">https://medium.com/media/8577752b37ed48e1b0acc24bea68d1dd/href</a></iframe><p>以下分別解釋三個 Function :</p><ol><li>scrollViewDidScroll</li></ol><p>這個 method 是在每次 ScrollView 觸發 scroll 事件，都會被執行的 delegate method，在這個 method 裡面，我們去讀取現在 ScrollView，也就是 tableView 被滑到了哪裡，並執行相對應的動作。</p><p>2. changeImageViewTopConstraint(contentOffset: CGPoint)</p><p>當 TableView 被往上滑，row 0, row 1 被滑出畫面，row 10, row 11 以及更下方的 row 進入畫面，此時 ContentOffset 是逐漸變大的，那在 TableView 上方的 ImageView 也要搭配一起消失在畫面中，所以在這個 method 裡面，我們去修改 imageViewTopConstraint，讓它隨著 ContentOffset 的變大而逐漸往畫面上方移動，直到整個移出畫面後就不再修改它，在這過程中我們必須確保 imageViewHeightConstraint 不改變，image 的顯示才不會跑掉。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*WC1Uhhv_sIJz5Dhr3yCdsA.gif" /></figure><p>3. changeImageViewHeightConstraint(contentOffset: CGPoint)</p><p>當 TableView 已經被滑到最上層的畫面，還繼續被下拉的時候，我們希望 imageView 變大，來填補空白，所以我們去修改的 imageViewHeightConstraint，讓 imageView 隨著下拉的動作高度變大，而因為 contentMode 設定為 ScaleAspectFill 的關係， imageView 顯示的內容就會有所改變，達到我們想要的效果。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*qCSwICa8UmqeyJL56LlYcQ.gif" /></figure><p>進行到這邊就完成了 Parallax 的效果，最後附上 Final Project 的 <a href="https://github.com/Wuchiwei/ParallaxDemo">Github 連結</a>，如果本文有任何錯誤或是未詳盡的地方，還請不吝到 Github 發 issue 給我，感謝你/妳。</p><p>【AppWorks School 新班 — Android Class #1 與 iOS Class #6 限時招生中】遠距 4 週、駐點集訓 16 週，全程免費，幫助你成為新時代的軟體工程師。</p><p>Android Class #1 &gt;&gt; <a href="https://goo.gl/Efi23t">https://goo.gl/Efi23t</a><br>iOS Class #6 &gt;&gt; <a href="https://goo.gl/Xz27kK">https://goo.gl/Xz27kK</a></p><p>歡迎申請加入，來讓我們幫助你加速學習！(申請只到 01/07/2018)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1a954c4d262d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/appworks-school/ios-parallax-view-on-the-top-of-tableview-1a954c4d262d">iOS Parallax View On The Top Of TableView</a> was originally published in <a href="https://medium.com/appworks-school">Aiworks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>