tag:blogger.com,1999:blog-5913615924819192222024-03-16T00:23:32.083-07:00阿勇的blog阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.comBlogger152125tag:blogger.com,1999:blog-591361592481919222.post-54185993959261555982023-07-30T21:26:00.001-07:002023-07-30T21:26:08.238-07:00iOS測試IPv6環境<p> 簡述:分享Mac的WiFi給手機。打開分享時,需按住Option,分享才會有NAT64。</p><p>詳細方法,參考這篇的<a href="https://developer.apple.com/library/archive/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/UnderstandingandPreparingfortheIPv6Transition/UnderstandingandPreparingfortheIPv6Transition.html#//apple_ref/doc/uid/TP40010220-CH213-DontLinkElementID_4">Test for IPv6 DNS64/NAT64 Compatibility Regularly</a>。</p><p>值得注意,用USB線分享的話,會是IPv4+IPv6</p>阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-62043572005202547292022-06-15T03:54:00.007-07:002022-06-15T03:54:48.080-07:00Swift Decodable 找不到key時,使用預設值<p>使用JSONDecoder時,如果找不到Key值,錯誤如下</p><pre>▿ DecodingError
▿ keyNotFound : 2 elements
- .0 : CodingKeys(stringValue: "b", intValue: nil)
▿ .1 : Context
- codingPath : 0 elements
- debugDescription : "No value associated with key CodingKeys(stringValue: \"b\", intValue: nil) (\"b\")."
- underlyingError : nil</pre>
<br /><p>其實只要利用KeyedDecodingContainer的decode(type:forKey)就可以解決這問題</p><pre>protocol<span style="color: white;"> </span><span style="color: #5dd8ff;">Init</span><span style="color: white;"> {</span>
<span style="color: #f2248c;">init</span>()
}
<span style="color: #f2248c;">extension</span><span style="color: white;"> </span>KeyedDecodingContainer<span style="color: white;"> {</span>
<span style="color: #f2248c;">func</span> <span style="color: #41a1c0;">decode</span><T: <span style="color: #e5c17c;">Codable</span> & Init>(<span style="color: #41a1c0;">_</span> type: T.<span style="color: #f2248c;">Type</span>,
<span style="color: #41a1c0;">forKey</span> key: <span style="color: #e5c17c;">Key</span>) <span style="color: #f2248c;">throws</span> -> T {
<span style="color: #f2248c;">try</span> <span style="color: #61aeef;">decodeIfPresent</span>(type, <span style="color: #61aeef;">forKey</span>: key) ?? .<span style="color: #61aeef;">init</span>()
}
}</pre><br /><p>之後只要將Codable後面,加上Init這個protocal就可以了</p><pre><span style="color: #f2248c;">enum</span><span style="color: white;"> </span><span style="color: #5dd8ff;">B</span><span style="color: white;">: </span>Int<span style="color: white;">, </span>Codable<span style="color: white;"> {</span>
<span style="color: #f2248c;">case</span> <span style="color: #41a1c0;">one</span> = <span style="color: #d2945d;">1</span>
<span style="color: #f2248c;">case</span> <span style="color: #41a1c0;">two</span> = <span style="color: #d2945d;">2</span>
}
<br />
<span style="color: #f2248c;">extension</span> <span style="color: #5dd8ff;">B</span>: Init {
<span style="color: #f2248c;">init</span>() {
<span style="color: #f2248c;">self</span> = .<span style="color: #df6a73;">one</span>
}
}</pre>阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-25889995405114920832022-03-17T00:04:00.009-07:002022-03-17T00:21:57.483-07:00用swift讀寫NTag<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEhJBJ8peE2-AtfPXDtL7wPRfVBHTBeaC4ETVj5j4tsyYYHN5e0qkRNuzao74mWzwLPkqtudHGaLEPixcglo6UkIVJXFwz9JubCmjgIxYfK6k8gHAtJ3bbrRrJtY7IKdqbI22UbAOBeNMGNE4lsmnKo3Gblb7x-yXzWhTcX5LlfbA24bWegflntxHr_U=s1624" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1624" data-original-width="750" height="320" src="https://blogger.googleusercontent.com/img/a/AVvXsEhJBJ8peE2-AtfPXDtL7wPRfVBHTBeaC4ETVj5j4tsyYYHN5e0qkRNuzao74mWzwLPkqtudHGaLEPixcglo6UkIVJXFwz9JubCmjgIxYfK6k8gHAtJ3bbrRrJtY7IKdqbI22UbAOBeNMGNE4lsmnKo3Gblb7x-yXzWhTcX5LlfbA24bWegflntxHr_U=s320" width="148" /></a><a href="https://blogger.googleusercontent.com/img/a/AVvXsEg6EJAw0XyKgtrQPX97L4_0cbCorN1Q5fATToSJII1UbCuvBHO2puKJH4u-A5nIQUMx64eeQMe4mLTf6BiXlvoyXrOvkEJBN2TrxNHv9He8qBwOa0RzLXBAHvnvovb923gkSwtA9NvyIf8ROSZoF_8srWPISehq1Qtpe6oGKUOx6cf60-n9362ddW5p=s1624" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1624" data-original-width="750" height="320" src="https://blogger.googleusercontent.com/img/a/AVvXsEg6EJAw0XyKgtrQPX97L4_0cbCorN1Q5fATToSJII1UbCuvBHO2puKJH4u-A5nIQUMx64eeQMe4mLTf6BiXlvoyXrOvkEJBN2TrxNHv9He8qBwOa0RzLXBAHvnvovb923gkSwtA9NvyIf8ROSZoF_8srWPISehq1Qtpe6oGKUOx6cf60-n9362ddW5p=s320" width="148" /></a><a href="https://blogger.googleusercontent.com/img/a/AVvXsEhlHmIUGClqyUifWLWcT8ttzuenvAzXXT3WIscc61fF4ELxNN71xSQBxmWBojey0vLJY524HPAD_5bzlY3v5YCBcI5_JkcFN6aONzrwiTK9FVyeME1n12ts9Lj9qDilMZyYk8MjAEtzWFkIxWqRrPiP-76NLtgxQkefiO-eBLxO61vHzqui02FpRo58=s1624" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1624" data-original-width="750" height="320" src="https://blogger.googleusercontent.com/img/a/AVvXsEhlHmIUGClqyUifWLWcT8ttzuenvAzXXT3WIscc61fF4ELxNN71xSQBxmWBojey0vLJY524HPAD_5bzlY3v5YCBcI5_JkcFN6aONzrwiTK9FVyeME1n12ts9Lj9qDilMZyYk8MjAEtzWFkIxWqRrPiP-76NLtgxQkefiO-eBLxO61vHzqui02FpRo58=s320" width="148" /></a></div><br /><ol style="text-align: left;"><li>在<i>info.plist</i>中,加入<i>Privacy - NFC Scan Usage Description</i></li><li>在<i>Capabilities</i> 加入<i>Near Field Communication Tag Reading</i><br /></li><li>掃描<p style="background-color: #18171b; color: #41a1c0; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">func</span><span style="color: white;"> </span>startScan<span style="color: white;">() {</span></p><p style="background-color: #18171b; color: #61aeef; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span><span style="color: #f2248c;">let</span><span style="color: white;"> session = </span><span style="color: #5cdfbf;">NFCNDEFReaderSession</span><span style="color: white;">(</span>delegate<span style="color: white;">: </span><span style="color: #f2248c;">self</span><span style="color: white;">, </span>queue<span style="color: white;">: </span><span style="color: #f2248c;">nil</span><span style="color: white;">, </span>invalidateAfterFirstRead<span style="color: white;">: </span><span style="color: #f2248c;">false</span><span style="color: white;">)</span></p><p style="background-color: #18171b; color: #90c378; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> session.</span><span style="color: #df6a73;">alertMessage</span><span style="color: white;"> = </span>"Hold your iPhone near an NFC transit card."</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> session.<span style="color: #61aeef;">begin</span>()</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">}</p></li><li>連線,更新payload<p style="background-color: #18171b; color: #5cdfbf; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">func</span><span style="color: white;"> </span><span style="color: #41a1c0;">readerSession</span><span style="color: white;">(</span><span style="color: #41a1c0;">_</span><span style="color: white;"> session: </span>NFCNDEFReaderSession<span style="color: white;">, </span><span style="color: #41a1c0;">didDetect</span><span style="color: white;"> tags: [</span>NFCNDEFTag<span style="color: white;">]) {</span></p><p style="background-color: #18171b; color: #f2248c; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>guard</p><p style="background-color: #18171b; color: #61aeef; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span><span style="color: #f2248c;">let</span><span style="color: white;"> sms = </span><span style="color: #90c378;">"場所代碼:111111111111111 本次實聯簡訊限防疫目的使用。"</span><span style="color: white;">.</span>addingPercentEncoding<span style="color: white;">(</span>withAllowedCharacters<span style="color: white;">: .</span><span style="color: #df6a73;">urlHostAllowed</span><span style="color: white;">),</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> payload = <span style="color: #5cdfbf;">NFCNDEFPayload</span>.<span style="color: #61aeef;">wellKnownTypeURIPayload</span>(<span style="color: #61aeef;">string</span>: <span style="color: #90c378;">"sms:1922&body=</span>\(sms)<span style="color: #90c378;">"</span>),</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> tag = tags.<span style="color: #61aeef;">first</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">else</span> {</p><p style="background-color: #18171b; color: #90c378; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> session.</span><span style="color: #61aeef;">invalidate</span><span style="color: white;">(</span><span style="color: #61aeef;">errorMessage</span><span style="color: white;">: </span>"Could not process tag."<span style="color: white;">)</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">return</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> session.<span style="color: #61aeef;">connect</span>(<span style="color: #61aeef;">to</span>: tag) { error <span style="color: #f2248c;">in</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">guard</span> error == <span style="color: #f2248c;">nil</span> <span style="color: #f2248c;">else</span> {</p><p style="background-color: #18171b; color: #90c378; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> session.</span><span style="color: #61aeef;">invalidate</span><span style="color: white;">(</span><span style="color: #61aeef;">errorMessage</span><span style="color: white;">: </span>"Could not connect to tag."<span style="color: white;">)</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">return</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> tag.<span style="color: #61aeef;">queryNDEFStatus</span> { status, capacity, error <span style="color: #f2248c;">in</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">guard</span> error == <span style="color: #f2248c;">nil</span> <span style="color: #f2248c;">else</span> {</p><p style="background-color: #18171b; color: #90c378; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> session.</span><span style="color: #61aeef;">invalidate</span><span style="color: white;">(</span><span style="color: #61aeef;">errorMessage</span><span style="color: white;">: </span>"Could not query status of tag."<span style="color: white;">)</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">return</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px; min-height: 18px;"> </p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">switch</span> status {</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">case</span> .<span style="color: #c679dd;">notSupported</span>:</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> session.<span style="color: #61aeef;">invalidate</span>(<span style="color: #61aeef;">errorMessage</span>: <span style="color: #90c378;">"Tag is not supported."</span>)</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">case</span> .<span style="color: #c679dd;">readOnly</span>:</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> session.<span style="color: #61aeef;">invalidate</span>(<span style="color: #61aeef;">errorMessage</span>: <span style="color: #90c378;">"Tag is only readable."</span>)</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">case</span> .<span style="color: #c679dd;">readWrite</span>:</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> messge = <span style="color: #5cdfbf;">NFCNDEFMessage</span>.<span style="color: #61aeef;">init</span>(<span style="color: #61aeef;">records</span>: [payload])</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> tag.<span style="color: #61aeef;">writeNDEF</span>(messge) { error <span style="color: #f2248c;">in</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">if</span> error != <span style="color: #f2248c;">nil</span> {</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> session.<span style="color: #61aeef;">invalidate</span>(<span style="color: #61aeef;">errorMessage</span>: <span style="color: #90c378;">"Failed to write message."</span>)</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> } <span style="color: #f2248c;">else</span> {</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> session.<span style="color: #df6a73;">alertMessage</span> = <span style="color: #90c378;">"Successfully configured tag."</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> session.<span style="color: #61aeef;">invalidate</span>()</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px; min-height: 18px;"> </p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">@unknown</span> <span style="color: #f2248c;">default</span>: session.<span style="color: #61aeef;">invalidate</span>(<span style="color: #61aeef;">errorMessage</span>: <span style="color: #90c378;">"Unknown status of tag."</span>)</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">}</p></li></ol><div><i><br /></i></div>PS.找不到 <i>Near Field Communication Tag Reading ,</i>請切換開發者帳號到付費帳號<p></p>阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-43769073440113824052022-03-05T18:50:00.001-08:002022-03-05T18:50:20.421-08:00ARKit(SpriteKit) 臉部角度追蹤<span class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEg-i10IGF5idvVjtdPr2RABbIsg4EbK0WXfY5KuNAYLwuyQsQkkU3tiow7M_k7AdNmJgEEI3iGjsstlwjbHqTsvd0IDsPAuQ6Bj0twTZtIEJDd8GoyGcQs-8S1hLyqD3Vm02drH_44NYc2J-P3Yl8dINpj5Yx5dKsom8TporD13FQo0796914f_eXOz=s1624" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1624" data-original-width="750" height="320" src="https://blogger.googleusercontent.com/img/a/AVvXsEg-i10IGF5idvVjtdPr2RABbIsg4EbK0WXfY5KuNAYLwuyQsQkkU3tiow7M_k7AdNmJgEEI3iGjsstlwjbHqTsvd0IDsPAuQ6Bj0twTZtIEJDd8GoyGcQs-8S1hLyqD3Vm02drH_44NYc2J-P3Yl8dINpj5Yx5dKsom8TporD13FQo0796914f_eXOz=s320" width="148" /></a></span><span class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEizNMots-U0vGkEPdxfbm6b-M-TduS7ZJjm3tZ5nUH1qnP9KmBQSgYXpoWtJLUExBxUL-U3_2sLzhcAMkzbCqRKz-YiRHo1D6-IwVYpLmkcadPss92Iz4hwuKXcT0Xa9X9U2nHriKPclttw5owfhvr9hTReve-QmrnhD4jYoihxXGHb1ppe4lg_WtM0=s1624" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1624" data-original-width="750" height="320" src="https://blogger.googleusercontent.com/img/a/AVvXsEizNMots-U0vGkEPdxfbm6b-M-TduS7ZJjm3tZ5nUH1qnP9KmBQSgYXpoWtJLUExBxUL-U3_2sLzhcAMkzbCqRKz-YiRHo1D6-IwVYpLmkcadPss92Iz4hwuKXcT0Xa9X9U2nHriKPclttw5owfhvr9hTReve-QmrnhD4jYoihxXGHb1ppe4lg_WtM0=s320" width="148" /></a></span><br /><p></p><div><ol style="text-align: left;"><li>建立😘 。由於前鏡頭左右相反,所以X軸要反向。<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">let</span> labelNode = <span style="color: #5cdfbf;">SKLabelNode</span>(<span style="color: #61aeef;">text</span>: <span style="color: #90c378;">"😘"</span>)</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">labelNode.<span style="color: #df6a73;">horizontalAlignmentMode</span> = .<span style="color: #c679dd;">center</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">labelNode.<span style="color: #df6a73;">verticalAlignmentMode</span> = .<span style="color: #c679dd;">center</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #c679dd;"></span></p><p style="background-color: #18171b; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;">labelNode.<span style="color: #df6a73;">xScale</span> = -</span><span style="color: #d2945d;"><span style="caret-color: rgb(210, 148, 93);">1</span></span></p></li><li>須將😘放入SKTransformNode,做後續的3D轉換。<br /><p style="background-color: #18171b; color: #5cdfbf; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">let</span><span style="color: white;"> face = </span>SKTransformNode<span style="color: white;">()</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">face.<span style="color: #61aeef;">addChild</span>(labelNode)</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">self</span>.<span style="color: #df6a73;">face</span> = face</p></li><li>更新時,SKTransformNode再依據ARFaceAnchor旋轉<br /><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">let</span> rotate = faceAnchor.<span style="color: #df6a73;">transform</span>.<span style="color: #df6a73;">rotate</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #df6a73;">face</span>?.<span style="color: #61aeef;">setRotationMatrix</span>(rotate)</p></li><li>最後,記得將node轉回來,否則貼圖會是反的</li></ol></div><div><br /></div>完整程式<span><a name='more'></a></span><br /><p style="background-color: #18171b; color: #5dd8ff; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">extension</span><span style="color: white;"> </span>matrix_float4x4<span style="color: white;"> {</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">var</span> <span style="color: #41a1c0;">rotate</span>: matrix_float3x3 {</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> (a,b,c,_) = <span style="color: #df6a73;">columns</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">return</span> <span style="color: #e5c17c;">float3x3</span>(a.<span style="color: #df6a73;">xyz</span>, b.<span style="color: #df6a73;">xyz</span>, c.<span style="color: #df6a73;">xyz</span>)</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">}</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px; min-height: 18px;"><br /></p>
<p style="background-color: #18171b; color: #f2248c; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">extension<span style="color: white;"> </span><span style="color: #5dd8ff;">SIMD4</span><span style="color: white;"> {</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">var</span> <span style="color: #41a1c0;">xyz</span>: <span style="color: #e5c17c;">SIMD3</span><Scalar> {</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">return</span> <span style="color: #e5c17c;">SIMD3</span>(<span style="color: #df6a73;">x</span>, <span style="color: #df6a73;">y</span>, <span style="color: #df6a73;">z</span>)</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">}</p>
<p style="background-color: #18171b; color: #5cdfbf; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">var</span><span style="color: white;"> </span><span style="color: #41a1c0;">face</span><span style="color: white;">: </span>SKTransformNode<span style="color: white;">?</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">func</span> <span style="color: #41a1c0;">view</span>(<span style="color: #41a1c0;">_</span> view: <span style="color: #5cdfbf;">ARSKView</span>, <span style="color: #41a1c0;">nodeFor</span> anchor: <span style="color: #5cdfbf;">ARAnchor</span>) -> <span style="color: #5cdfbf;">SKNode</span>? {</p>
<p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Create and configure a node for the anchor added to the view's session.</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">if</span> anchor <span style="color: #f2248c;">is</span> <span style="color: #5cdfbf;">ARFaceAnchor</span> {</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> labelNode = <span style="color: #5cdfbf;">SKLabelNode</span>(<span style="color: #61aeef;">text</span>: <span style="color: #90c378;">"😘"</span>)</p>
<p style="background-color: #18171b; color: #df6a73; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> labelNode.</span>horizontalAlignmentMode<span style="color: white;"> = .</span><span style="color: #c679dd;">center</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> labelNode.<span style="color: #df6a73;">verticalAlignmentMode</span> = .<span style="color: #c679dd;">center</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> labelNode.<span style="color: #df6a73;">xScale</span> = -<span style="color: #d2945d;">3</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> labelNode.<span style="color: #df6a73;">yScale</span> = <span style="color: #d2945d;">3</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px; min-height: 18px;"> </p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> face = <span style="color: #5cdfbf;">SKTransformNode</span>()</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> face.<span style="color: #61aeef;">addChild</span>(labelNode)</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">self</span>.<span style="color: #df6a73;">face</span> = face</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px; min-height: 18px;"> </p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> node = <span style="color: #5cdfbf;">SKNode</span>()</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> node.<span style="color: #61aeef;">addChild</span>(face)</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">return</span> node</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p>
<p style="background-color: #18171b; color: #f2248c; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>return<span style="color: white;"> </span>nil</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">}</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px; min-height: 18px;"><br /></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">func</span> <span style="color: #41a1c0;">view</span>(<span style="color: #41a1c0;">_</span> view: <span style="color: #5cdfbf;">ARSKView</span>, <span style="color: #41a1c0;">didUpdate</span> node: <span style="color: #5cdfbf;">SKNode</span>, <span style="color: #41a1c0;">for</span> anchor: <span style="color: #5cdfbf;">ARAnchor</span>){</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">if</span> <span style="color: #f2248c;">let</span> faceAnchor = anchor <span style="color: #f2248c;">as</span>? <span style="color: #5cdfbf;">ARFaceAnchor</span> {</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> rotate = faceAnchor.<span style="color: #df6a73;">transform</span>.<span style="color: #df6a73;">rotate</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #df6a73;">face</span>?.<span style="color: #61aeef;">setRotationMatrix</span>(rotate)</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> node.<span style="color: #df6a73;">xScale</span> *= -<span style="color: #d2945d;">1</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">}</p>阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-87862207712410938312022-02-28T19:22:00.003-08:002022-03-04T18:16:31.618-08:00ARKit(SceneKit) 更新模型臉部表情<span class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEgu4NVQgBUpLkFBIyUqB0v8M9-lAydH645bOB3_12NCejEvP9Nhvnb8lY_jBuSqlgwGDuWMKlUjDS1V-52KbhNOeayZJvNhhe4T4Tqim1gZJqLktMG9A7v3ezFwHa3GUUoeN696H3WwRL_NBO1Kom7_yUgyKqTmHoHnITGQqOq11BeH-VetDs2GKfO4=s1624" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1624" data-original-width="750" height="320" src="https://blogger.googleusercontent.com/img/a/AVvXsEgu4NVQgBUpLkFBIyUqB0v8M9-lAydH645bOB3_12NCejEvP9Nhvnb8lY_jBuSqlgwGDuWMKlUjDS1V-52KbhNOeayZJvNhhe4T4Tqim1gZJqLktMG9A7v3ezFwHa3GUUoeN696H3WwRL_NBO1Kom7_yUgyKqTmHoHnITGQqOq11BeH-VetDs2GKfO4=s320" width="148" /></a></span><span class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEie-RGr_MZSs7Vd9sOTsuCI-8XXKX4zd4rITbFug6fRuvTSLbuht4XJlfoW3cscrgmgaAHrIcS5HTmB_X6-l5PqxsInJ0gj0nQuFlzDL_kKRCzJvh7bNfMLryGUq75-9LzAXoh2G1dHLsQts35T7F6NTy8NL8ZUHoYB5qIzNJxamH00D2EJipnuHsAe=s1624" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1624" data-original-width="750" height="320" src="https://blogger.googleusercontent.com/img/a/AVvXsEie-RGr_MZSs7Vd9sOTsuCI-8XXKX4zd4rITbFug6fRuvTSLbuht4XJlfoW3cscrgmgaAHrIcS5HTmB_X6-l5PqxsInJ0gj0nQuFlzDL_kKRCzJvh7bNfMLryGUq75-9LzAXoh2G1dHLsQts35T7F6NTy8NL8ZUHoYB5qIzNJxamH00D2EJipnuHsAe=s320" width="148" /></a></span><br /><span><a name='more'></a></span><p><br /></p><p></p><ol style="text-align: left;"><li>使用包含BlendShapes的模型。如<a href="https://www.polywink.com/9-automatic-expressions-blendshapes-on-demand.html">sample</a>的fbx模型</li><li>在Blender中匯入fbx模型,確認Object Data Properties有外形鍵<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEhujg_pRjrpUavIIscJ9I7gSrXhglR6joAcH-Z9gc8wfkoX3KUTkBr5nZZR2DUSzWGZpoHv2cuhmLgujmkO2aF4PX1IfNA6wSQGj_8AspzUn6taFrazozakJEAIR7uFfPgjavw4tG5vKP38vbRFF6IxnyFVRo_XnTpR-047sPcIMG3J-z0ErqerTqXx=s334" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="206" data-original-width="334" height="197" src="https://blogger.googleusercontent.com/img/a/AVvXsEhujg_pRjrpUavIIscJ9I7gSrXhglR6joAcH-Z9gc8wfkoX3KUTkBr5nZZR2DUSzWGZpoHv2cuhmLgujmkO2aF4PX1IfNA6wSQGj_8AspzUn6taFrazozakJEAIR7uFfPgjavw4tG5vKP38vbRFF6IxnyFVRo_XnTpR-047sPcIMG3J-z0ErqerTqXx=s320" width="320" /></a></div><br /></li><li>匯出成dae檔,確認有勾選外形鍵<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEghTCdOyFvto6yfaQ79T427_eLg0uFSlVlg_HojKWJRdLLU9slgxPd_yjpanvYz9pT3XAljDZvWFYmvaii0prXZTRPE9bs0D66UUJBxLnEJTs766qIdO9Q7QVYtwZi49HwPX-54cHOsWY5yWhfqewdCtW7uR9g-q4P66yHtr0xBqoUXaKC23_IBpK0z=s226" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="160" data-original-width="226" height="160" src="https://blogger.googleusercontent.com/img/a/AVvXsEghTCdOyFvto6yfaQ79T427_eLg0uFSlVlg_HojKWJRdLLU9slgxPd_yjpanvYz9pT3XAljDZvWFYmvaii0prXZTRPE9bs0D66UUJBxLnEJTs766qIdO9Q7QVYtwZi49HwPX-54cHOsWY5yWhfqewdCtW7uR9g-q4P66yHtr0xBqoUXaKC23_IBpK0z" width="226" /></a></div><br /></li><li>使用<a href="https://github.com/JakeHoldom/ColladaMorphAdjuster">ColladaMorphAdjuster</a>將剛剛的dae檔,轉成SceneKit支援的dae檔</li><li>在XCode中,可以看到模型的Attributes裡,Geometry Morphers中會有類似臉部細心的key<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEhUSW4jJKCYIDDLdYH9cAcSJGhLrM-vBBvjbjFUal_TDwN0mDY3kDbydzIVuLQ_f2BWvXo9N9juy5iaTAdXeYG-QvVhwNINjg0VDT4GysmObOi65_HiQeXfEm3Uz8qK0c4RoVcPJUUe1Qzem1ZCll-7EAC4FnUjMj8i1odka10U_gFg4AUzi0n8216l=s350" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="350" data-original-width="303" height="320" src="https://blogger.googleusercontent.com/img/a/AVvXsEhUSW4jJKCYIDDLdYH9cAcSJGhLrM-vBBvjbjFUal_TDwN0mDY3kDbydzIVuLQ_f2BWvXo9N9juy5iaTAdXeYG-QvVhwNINjg0VDT4GysmObOi65_HiQeXfEm3Uz8qK0c4RoVcPJUUe1Qzem1ZCll-7EAC4FnUjMj8i1odka10U_gFg4AUzi0n8216l=s320" width="277" /></a></div><br /></li><li>Editor -> convert to SceneKit file format</li><li>載入模型<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">func</span> <span style="color: #41a1c0;">renderer</span>(<span style="color: #41a1c0;">_</span> renderer: <span style="color: #5cdfbf;">SCNSceneRenderer</span>, <span style="color: #41a1c0;">nodeFor</span> anchor: <span style="color: #5cdfbf;">ARAnchor</span>) -> <span style="color: #5cdfbf;">SCNNode</span>? {</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">if</span> anchor <span style="color: #f2248c;">is</span> <span style="color: #5cdfbf;">ARFaceAnchor</span> {</p><p style="background-color: #18171b; color: #90c378; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span><span style="color: #f2248c;">let</span><span style="color: white;"> scene = </span><span style="color: #5cdfbf;">SCNScene</span><span style="color: white;">(</span><span style="color: #61aeef;">named</span><span style="color: white;">: </span>"art.scnassets/POLYWINK_BOD_157_SAMPLE.scn"<span style="color: white;">)!</span></p><p style="background-color: #18171b; color: #61aeef; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span><span style="color: #df6a73;">face</span><span style="color: white;"> = scene.</span><span style="color: #df6a73;">rootNode</span><span style="color: white;">.</span>childNode<span style="color: white;">(</span>withName<span style="color: white;">: </span><span style="color: #90c378;">"POLYWINK_Louise"</span><span style="color: white;">, </span>recursively<span style="color: white;">: </span><span style="color: #f2248c;">true</span><span style="color: white;">)</span></p><p style="background-color: #18171b; color: #df6a73; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>face<span style="color: white;">?.</span>geometry<span style="color: white;">?.</span>firstMaterial<span style="color: white;">?.</span>lightingModel<span style="color: white;"> = .</span>physicallyBased</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">return</span> scene.<span style="color: #df6a73;">rootNode</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> } <span style="color: #f2248c;">else</span> {</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">return</span> <span style="color: #f2248c;">nil</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">}</p></li><li>將BlendShapesLocation對照到模型的外形鍵<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">let</span> <span style="color: #41a1c0;">blendShapeMap</span>: [<span style="color: #5cdfbf;">ARFaceAnchor</span>.BlendShapeLocation: <span style="color: #e5c17c;">String</span>] = [</p><p style="background-color: #18171b; color: #90c378; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> .</span><span style="color: #df6a73;">eyeBlinkLeft</span><span style="color: white;">: </span>"Eye_Closed_Left"<span style="color: white;">,</span></p><p style="background-color: #18171b; color: #90c378; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> .</span><span style="color: #df6a73;">eyeBlinkRight</span><span style="color: white;">: </span>"Eye_Closed_Right"</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">]</p><p></p><p></p></li><li>將faceAnchor的blendShapes對照到node的morpher<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">func</span> <span style="color: #41a1c0;">renderer</span>(<span style="color: #41a1c0;">_</span> renderer: <span style="color: #5cdfbf;">SCNSceneRenderer</span>, <span style="color: #41a1c0;">didUpdate</span> node: <span style="color: #5cdfbf;">SCNNode</span>, <span style="color: #41a1c0;">for</span> anchor: <span style="color: #5cdfbf;">ARAnchor</span>) {</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">if</span> <span style="color: #f2248c;">let</span> faceAnchor = anchor <span style="color: #f2248c;">as</span>? <span style="color: #5cdfbf;">ARFaceAnchor</span> {</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> blendShapes = faceAnchor.<span style="color: #df6a73;">blendShapes</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #df6a73;">blendShapeMap</span>.<span style="color: #61aeef;">forEach</span>{ key, name <span style="color: #f2248c;">in</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">if</span> <span style="color: #f2248c;">let</span> weight = blendShapes[key] <span style="color: #f2248c;">as</span>? <span style="color: #e5c17c;">CGFloat</span> {</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #df6a73;">face</span>?.<span style="color: #df6a73;">morpher</span>?.<span style="color: #61aeef;">setWeight</span>(weight, <span style="color: #61aeef;">forTargetNamed</span>: name)</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">}</p></li></ol><p></p>阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-5750017154247150572022-02-26T00:42:00.003-08:002022-03-04T18:16:19.906-08:00ARKit(SceneKit) 臉部貼圖<p></p><div class="separator" style="clear: both;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEjw0ft-UQtLupN3TqQCrMCNkKdPJDyFnDIlJ7HJMYmK9XLpwcv_DTilY6ZxcvRRREtApA5EolJmgGxofVtpiY1Mo-AL3a__1MJ9nGTHMzUvdMLVTCWxmfkbDZ50TOF6O3ZKY-WrHPuWnfiJtJPxW9H5hmBqW8BiuXAZzzLMV9w7tvhpW4sTiE5g7qLg=s433" style="display: block; padding: 1em 0px; text-align: center;"><img alt="" border="0" data-original-height="433" data-original-width="200" height="320" src="https://blogger.googleusercontent.com/img/a/AVvXsEjw0ft-UQtLupN3TqQCrMCNkKdPJDyFnDIlJ7HJMYmK9XLpwcv_DTilY6ZxcvRRREtApA5EolJmgGxofVtpiY1Mo-AL3a__1MJ9nGTHMzUvdMLVTCWxmfkbDZ50TOF6O3ZKY-WrHPuWnfiJtJPxW9H5hmBqW8BiuXAZzzLMV9w7tvhpW4sTiE5g7qLg=s320" /></a></div><p></p><ol style="text-align: left;">
<li>修改<a href="https://developer.apple.com/documentation/arkit/content_anchors/tracking_and_visualizing_faces">Tracking and Visualizing Faces</a>範例中的貼圖『wireframeTexture.png』</li>
<li>指定ARSCNFaceGeometry的貼圖<pre><p style="background-color: #18171b; color: #df6a73; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;">faceMesh?.</span>firstMaterial<span style="color: white;">?.</span>diffuse<span style="color: white;">.</span>contents<span style="color: white;"> = </span><span style="color: #5cdfbf;">UIImage</span><span style="color: white;">(</span><span style="color: #61aeef;">named</span><span style="color: white;">: </span><span style="color: #90c378;">"wireframeTexture"</span><span style="color: white;">)</span></p></pre></li>
</ol><p></p>阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-79296875743757700212022-02-26T00:17:00.005-08:002022-03-04T18:16:02.197-08:00ARKit(SceneKit) 臉部表情追蹤<p></p><div class="separator" style="clear: both;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEgkVedX462S9dBdxFeblrRGeLtuO5jpfDXQPpUCVzogGoLzqIrIIhNLo_4Zrxo7vYDzZBYzQrcXdrCxCb-FmmKpopeqqOgo4UMdYPy5e-Q84Q7Ey-VwZ0mj43AML_IZWxUFFwbaatM2VCuur7TH1ZjySfMgQq2K4joq8J11twlsTaWCbmIC_WnFnxgS" style="display: block; padding: 1em 0px; text-align: center;"><img alt="" border="0" data-original-height="433" data-original-width="200" src="https://blogger.googleusercontent.com/img/a/AVvXsEgkVedX462S9dBdxFeblrRGeLtuO5jpfDXQPpUCVzogGoLzqIrIIhNLo_4Zrxo7vYDzZBYzQrcXdrCxCb-FmmKpopeqqOgo4UMdYPy5e-Q84Q7Ey-VwZ0mj43AML_IZWxUFFwbaatM2VCuur7TH1ZjySfMgQq2K4joq8J11twlsTaWCbmIC_WnFnxgS" /></a></div><p></p><ol style="text-align: left;">
<li> 建立臉部追蹤</li>
<p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">// Create a session configuration</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> configuration = <span style="color: #5cdfbf;">ARFaceTrackingConfiguration</span>()</p>
<p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">// Run the view's session</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #df6a73;">sceneView</span>.<span style="color: #df6a73;">session</span>.<span style="color: #61aeef;">run</span>(configuration)</p><li> 建立臉部模型</li>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">func</span> <span style="color: #41a1c0;">renderer</span>(<span style="color: #41a1c0;">_</span> renderer: <span style="color: #5cdfbf;">SCNSceneRenderer</span>, <span style="color: #41a1c0;">nodeFor</span> anchor: <span style="color: #5cdfbf;">ARAnchor</span>) -> <span style="color: #5cdfbf;">SCNNode</span>? {</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">if</span> anchor <span style="color: #f2248c;">is</span> <span style="color: #5cdfbf;">ARFaceAnchor</span> {</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> faceMesh = <span style="color: #5cdfbf;">ARSCNFaceGeometry</span>(<span style="color: #61aeef;">device</span>: <span style="color: #df6a73;">sceneView</span>.<span style="color: #df6a73;">device</span>!)</p>
<p style="background-color: #18171b; color: #df6a73; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> faceMesh?.</span>firstMaterial<span style="color: white;">?.</span>lightingModel<span style="color: white;"> = .</span>physicallyBased</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> node = <span style="color: #5cdfbf;">SCNNode</span>(<span style="color: #61aeef;">geometry</span>: faceMesh)</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">return</span> node</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> } <span style="color: #f2248c;">else</span> {</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">return</span> <span style="color: #f2248c;">nil</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">}</p><li> 更新臉部表情</li>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">func</span> <span style="color: #41a1c0;">renderer</span>(<span style="color: #41a1c0;">_</span> renderer: <span style="color: #5cdfbf;">SCNSceneRenderer</span>, <span style="color: #41a1c0;">didUpdate</span> node: <span style="color: #5cdfbf;">SCNNode</span>, <span style="color: #41a1c0;">for</span> anchor: <span style="color: #5cdfbf;">ARAnchor</span>) {</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">if</span> <span style="color: #f2248c;">let</span> faceAnchor = anchor <span style="color: #f2248c;">as</span>? <span style="color: #5cdfbf;">ARFaceAnchor</span>, <span style="color: #f2248c;">let</span> faceGeometry = node.<span style="color: #df6a73;">geometry</span> <span style="color: #f2248c;">as</span>? <span style="color: #5cdfbf;">ARSCNFaceGeometry</span> {</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> faceGeometry.<span style="color: #61aeef;">update</span>(<span style="color: #61aeef;">from</span>: faceAnchor.<span style="color: #df6a73;">geometry</span>)</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">}</p></ol><p></p>阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-47463029416659804762022-02-11T00:31:00.005-08:002022-02-26T00:18:09.196-08:00RealityKit 判斷手指觸摸物件<ol><li>用Reality Composer編輯物件『屬性』,勾選『物理效果』『加入』</li><li>將ARFrame.capturedImage<a href="https://developer.apple.com/documentation/arkit/content_anchors/recognizing_and_labeling_arbitrary_objects">轉成CVPixelBuffer</a><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">func</span> <span style="color: #41a1c0;">session</span>(<span style="color: #41a1c0;">_</span> session: <span style="color: #5cdfbf;">ARSession</span>, <span style="color: #41a1c0;">didUpdate</span> frame: <span style="color: #5cdfbf;">ARFrame</span>) {</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">guard</span> <span style="color: #f2248c;">let</span> arView = <span style="color: #df6a73;">arView</span> <span style="color: #f2248c;">else</span> {<span style="color: #f2248c;">return</span>}</p><p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Do not enqueue other buffers for processing while another Vision task is still running.</p><p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// The camera stream has only a finite amount of buffers available; holding too many buffers for analysis would starve the camera.</p><p style="background-color: #18171b; color: #df6a73; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span><span style="color: #f2248c;">guard</span><span style="color: white;"> </span>currentBuffer<span style="color: white;"> == </span><span style="color: #f2248c;">nil</span><span style="color: white;">, </span><span style="color: #f2248c;">case</span><span style="color: white;"> .</span><span style="color: #c679dd;">normal</span><span style="color: white;"> = frame.</span>camera<span style="color: white;">.</span>trackingState<span style="color: white;"> </span><span style="color: #f2248c;">else</span><span style="color: white;"> {</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">return</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p><p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Retain the image buffer for Vision processing.</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> currentBuffer = frame.<span style="color: #df6a73;">capturedImage</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">}</p></li><li>利用Vision<a href="https://developer.apple.com/documentation/vision/detecting_hand_poses_with_vision">偵測食指</a><br /><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">let</span> requestHandler = <span style="color: #5cdfbf;">VNImageRequestHandler</span>(<span style="color: #5cdfbf;">cvPixelBuffer</span>: currentBuffer, <span style="color: #5cdfbf;">orientation</span>: orientation)</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #df6a73;">visionQueue</span>.<span style="color: #61aeef;">async</span> {</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">do</span> {</p><p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Perform VNDetectHumanHandPoseRequest</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">try</span> requestHandler.<span style="color: #61aeef;">perform</span>([<span style="color: #f2248c;">self</span>.<span style="color: #df6a73;">handPoseRequest</span>])</p><p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Continue only when a hand was detected in the frame.</p><p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Since we set the maximumHandCount property of the request to 1, there will be at most one observation.</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">guard</span> <span style="color: #f2248c;">let</span> observation = <span style="color: #f2248c;">self</span>.<span style="color: #df6a73;">handPoseRequest</span>.<span style="color: #df6a73;">results</span>?.<span style="color: #61aeef;">first</span> <span style="color: #f2248c;">else</span> {</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">return</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p><p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Get points for index finger.</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> indexFingerPoints = <span style="color: #f2248c;">try</span> observation.<span style="color: #61aeef;">recognizedPoints</span>(.<span style="color: #df6a73;">indexFinger</span>)</p><p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Look for tip points.</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">guard</span> <span style="color: #f2248c;">let</span> indexTipPoint = indexFingerPoints[.<span style="color: #df6a73;">indexTip</span>] <span style="color: #f2248c;">else</span> {</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">return</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p><p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Ignore low confidence points.</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">guard</span> indexTipPoint.<span style="color: #df6a73;">confidence</span> > <span style="color: #d2945d;">0.3</span> <span style="color: #f2248c;">else</span> {</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">return</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px; min-height: 18px;"> </p><p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> } <span style="color: #f2248c;">catch</span> {</p><p style="background-color: #18171b; color: #90c378; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span><span style="color: #61aeef;">print</span><span style="color: white;">(</span>"Error: Vision request failed with error \"<span style="color: white;">\(</span><span style="color: #c679dd;">error</span><span style="color: white;">)</span>\""<span style="color: white;">)</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">}</p></li><li>將Vision的座標轉成ARView的座標<br /><p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">// Convert points from Vision coordinates to ARView coordinates.</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">let</span> point = indexTipPoint.<span style="color: #df6a73;">location</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">let</span> tip = <span style="color: #e5c17c;">CGPoint</span>(<span style="color: #61aeef;">x</span>: point.<span style="color: #df6a73;">x</span> * width, <span style="color: #61aeef;">y</span>: (<span style="color: #d2945d;">1</span> - point.<span style="color: #df6a73;">y</span>) * height)</p></li></ol><div><br /></div>sample code<br /><a name='more'></a><div><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">import</span> ARKit</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">import</span> Vision</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">import</span> RealityKit</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px; min-height: 18px;"><br /></p>
<p style="background-color: #18171b; color: #5cdfbf; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">class</span><span style="color: white;"> </span><span style="color: #5dd8ff;">ARSessionHelper</span><span style="color: white;">: NSObject, </span>ARSessionDelegate<span style="color: white;"> {</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">weak</span> <span style="color: #f2248c;">var</span> <span style="color: #41a1c0;">arView</span>: <span style="color: #5cdfbf;">ARView</span>?</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">weak</span> <span style="color: #f2248c;">var</span> <span style="color: #41a1c0;">box</span>: Experience.Box?</p>
<p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// The pixel buffer being held for analysis; used to serialize Vision requests.</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">private</span> <span style="color: #f2248c;">var</span> <span style="color: #41a1c0;">currentBuffer</span>: CVPixelBuffer?</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px; min-height: 18px;"> </p>
<p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Queue for dispatching vision classification requests</p>
<p style="background-color: #18171b; color: #90c378; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span><span style="color: #f2248c;">private</span><span style="color: white;"> </span><span style="color: #f2248c;">let</span><span style="color: white;"> </span><span style="color: #41a1c0;">visionQueue</span><span style="color: white;"> = DispatchQueue(label: </span>"com.example.apple-samplecode.ARKitVision.serialVisionQueue"<span style="color: white;">)</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px; min-height: 18px;"> </p>
<p style="background-color: #18171b; color: #5cdfbf; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span><span style="color: #f2248c;">private</span><span style="color: white;"> </span><span style="color: #f2248c;">var</span><span style="color: white;"> </span><span style="color: #41a1c0;">handPoseRequest</span><span style="color: white;"> = </span>VNDetectHumanHandPoseRequest<span style="color: white;">()</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px; min-height: 18px;"> </p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">func</span> <span style="color: #41a1c0;">session</span>(<span style="color: #41a1c0;">_</span> session: <span style="color: #5cdfbf;">ARSession</span>, <span style="color: #41a1c0;">didUpdate</span> frame: <span style="color: #5cdfbf;">ARFrame</span>) {</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">guard</span> <span style="color: #f2248c;">let</span> arView = <span style="color: #df6a73;">arView</span> <span style="color: #f2248c;">else</span> {<span style="color: #f2248c;">return</span>}</p>
<p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Do not enqueue other buffers for processing while another Vision task is still running.</p>
<p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// The camera stream has only a finite amount of buffers available; holding too many buffers for analysis would starve the camera.</p>
<p style="background-color: #18171b; color: #df6a73; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span><span style="color: #f2248c;">guard</span><span style="color: white;"> </span>currentBuffer<span style="color: white;"> == </span><span style="color: #f2248c;">nil</span><span style="color: white;">, </span><span style="color: #f2248c;">case</span><span style="color: white;"> .</span><span style="color: #c679dd;">normal</span><span style="color: white;"> = frame.</span>camera<span style="color: white;">.</span>trackingState<span style="color: white;"> </span><span style="color: #f2248c;">else</span><span style="color: white;"> {</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">return</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p>
<p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Retain the image buffer for Vision processing.</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> currentBuffer = frame.<span style="color: #df6a73;">capturedImage</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px; min-height: 18px;"> </p>
<p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Most computer vision tasks are not rotation agnostic so it is important to pass in the orientation of the image with respect to device.</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> orientation = CGImagePropertyOrientation(<span style="color: #61aeef;">isUsingFrontFacingCamera</span>: <span style="color: #f2248c;">false</span>)</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> width = arView.<span style="color: #df6a73;">frame</span>.<span style="color: #df6a73;">width</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> height = arView.<span style="color: #df6a73;">frame</span>.<span style="color: #df6a73;">height</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px; min-height: 18px;"> </p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">self</span>.<span style="color: #df6a73;">currentBuffer</span> = currentBuffer</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> requestHandler = <span style="color: #5cdfbf;">VNImageRequestHandler</span>(<span style="color: #5cdfbf;">cvPixelBuffer</span>: currentBuffer, <span style="color: #5cdfbf;">orientation</span>: orientation)</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #df6a73;">visionQueue</span>.<span style="color: #61aeef;">async</span> {</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">do</span> {</p>
<p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Release the pixel buffer when done, allowing the next buffer to be processed.</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">defer</span> { <span style="color: #f2248c;">self</span>.<span style="color: #df6a73;">currentBuffer</span> = <span style="color: #f2248c;">nil</span> }</p>
<p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Perform VNDetectHumanHandPoseRequest</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">try</span> requestHandler.<span style="color: #61aeef;">perform</span>([<span style="color: #f2248c;">self</span>.<span style="color: #df6a73;">handPoseRequest</span>])</p>
<p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Continue only when a hand was detected in the frame.</p>
<p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Since we set the maximumHandCount property of the request to 1, there will be at most one observation.</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">guard</span> <span style="color: #f2248c;">let</span> observation = <span style="color: #f2248c;">self</span>.<span style="color: #df6a73;">handPoseRequest</span>.<span style="color: #df6a73;">results</span>?.<span style="color: #61aeef;">first</span> <span style="color: #f2248c;">else</span> {</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">return</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p>
<p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Get points for index finger.</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> indexFingerPoints = <span style="color: #f2248c;">try</span> observation.<span style="color: #61aeef;">recognizedPoints</span>(.<span style="color: #df6a73;">indexFinger</span>)</p>
<p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Look for tip points.</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">guard</span> <span style="color: #f2248c;">let</span> indexTipPoint = indexFingerPoints[.<span style="color: #df6a73;">indexTip</span>] <span style="color: #f2248c;">else</span> {</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">return</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p>
<p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Ignore low confidence points.</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">guard</span> indexTipPoint.<span style="color: #df6a73;">confidence</span> > <span style="color: #d2945d;">0.3</span> <span style="color: #f2248c;">else</span> {</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">return</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px; min-height: 18px;"> </p>
<p style="background-color: #18171b; color: #59626f; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>// Convert points from Vision coordinates to ARView coordinates.</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> point = indexTipPoint.<span style="color: #df6a73;">location</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> tip = <span style="color: #e5c17c;">CGPoint</span>(<span style="color: #61aeef;">x</span>: point.<span style="color: #df6a73;">x</span> * width, <span style="color: #61aeef;">y</span>: (<span style="color: #d2945d;">1</span> - point.<span style="color: #df6a73;">y</span>) * height)</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px; min-height: 18px;"> </p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> hitTestResult = arView.<span style="color: #61aeef;">entity</span>(<span style="color: #61aeef;">at</span>: tip)</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">guard</span> hitTestResult == <span style="color: #f2248c;">self</span>.<span style="color: #df6a73;">box</span>?.steelBox <span style="color: #f2248c;">else</span> { <span style="color: #f2248c;">return</span> }</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px; min-height: 18px;"> </p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> DispatchQueue.<span style="color: #df6a73;">main</span>.<span style="color: #61aeef;">async</span> {</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">self</span>.<span style="color: #df6a73;">box</span>?.<span style="color: #61aeef;">notifications</span>.notificationTrigger.post()</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> } <span style="color: #f2248c;">catch</span> {</p>
<p style="background-color: #18171b; color: #90c378; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span><span style="color: #61aeef;">print</span><span style="color: white;">(</span>"Error: Vision request failed with error \"<span style="color: white;">\(</span><span style="color: #c679dd;">error</span><span style="color: white;">)</span>\""<span style="color: white;">)</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">}</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px; min-height: 18px;"><br /></p>
<p style="background-color: #18171b; color: #5dd8ff; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">extension</span><span style="color: white;"> </span>CGImagePropertyOrientation<span style="color: white;"> {</span></p>
<p style="background-color: #18171b; color: #41a1c0; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span><span style="color: #f2248c;">init</span><span style="color: white;">(</span>isUsingFrontFacingCamera<span style="color: white;">: </span><span style="color: #e5c17c;">Bool</span><span style="color: white;">, </span>deviceOrientation<span style="color: white;">: </span><span style="color: #e5c17c;">UIDeviceOrientation</span><span style="color: white;"> = </span><span style="color: #5cdfbf;">UIDevice</span><span style="color: white;">.</span><span style="color: #61aeef;">current</span><span style="color: white;">.</span><span style="color: #61aeef;">orientation</span><span style="color: white;">) {</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">switch</span> deviceOrientation {</p>
<p style="background-color: #18171b; color: #c679dd; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span><span style="color: #f2248c;">case</span><span style="color: white;"> .</span>portraitUpsideDown<span style="color: white;">:</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">self</span> = .<span style="color: #df6a73;">left</span></p>
<p style="background-color: #18171b; color: #c679dd; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span><span style="color: #f2248c;">case</span><span style="color: white;"> .</span>landscapeLeft<span style="color: white;">:</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">self</span> = isUsingFrontFacingCamera ? .<span style="color: #c679dd;">down</span> : .<span style="color: #c679dd;">up</span></p>
<p style="background-color: #18171b; color: #c679dd; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span><span style="color: #f2248c;">case</span><span style="color: white;"> .</span>landscapeRight<span style="color: white;">:</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">self</span> = isUsingFrontFacingCamera ? .<span style="color: #c679dd;">up</span> : .<span style="color: #c679dd;">down</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">default</span>:</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">self</span> = .<span style="color: #df6a73;">right</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> }</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">}</p></div><div><br /></div>阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-71436068184725199832022-02-10T18:55:00.001-08:002022-02-26T00:18:01.159-08:00RealityKit 判斷物件點擊方法2<p></p><ol style="text-align: left;"><li>用Reality Composer編輯物件『屬性』,勾選『物理效果』『加入』</li><li>替arView加上手勢功能<br /><p style="background-color: #18171b; color: #61aeef; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;">arView.</span>addGestureRecognizer<span style="color: white;">(</span></p>
<p style="background-color: #18171b; color: #5cdfbf; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> </span>UITapGestureRecognizer<span style="color: white;">(</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #5cdfbf;">target</span>: <span style="color: #df6a73;">xxxx</span>,</p>
<p style="background-color: #18171b; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: white;"> <span style="color: #5cdfbf;">action</span>: <span style="color: #f2248c;">#selector</span>(</span><span style="color: #5cdfbf;"><span style="caret-color: rgb(92, 223, 191);">XXXX</span></span><span style="color: white;">.</span><span style="color: #61aeef;">handleTap</span><span style="color: white;">(</span><span style="color: #61aeef;">recognizer</span><span style="color: white;">:))</span></p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> )</p>
<p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">)</p></li><li><br /><p style="background-color: #18171b; color: #5cdfbf; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"><span style="color: #f2248c;">@objc</span><span style="color: white;"> </span><span style="color: #f2248c;">func</span><span style="color: white;"> </span><span style="color: #41a1c0;">handleTap</span><span style="color: white;">(</span><span style="color: #41a1c0;">recognizer</span><span style="color: white;">: </span>UITapGestureRecognizer<span style="color: white;">) {</span></p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> tapLocation = recognizer.<span style="color: #61aeef;">location</span>(<span style="color: #61aeef;">in</span>: arView)</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">let</span> entity = arView.<span style="color: #61aeef;">entity</span>(<span style="color: #61aeef;">at</span>: tapLocation)</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> <span style="color: #f2248c;">guard</span> entity == boxAnchor?.steelBox <span style="color: #f2248c;">else</span> { <span style="color: #f2248c;">return</span> }</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;"> // entity 即點擊物件</p><p style="background-color: #18171b; color: white; font-family: Helvetica; font-size: 15px; font-stretch: normal; line-height: normal; margin: 0px;">}</p></li></ol><p></p>阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-34565546449225746392022-02-10T18:19:00.001-08:002022-02-26T00:17:46.181-08:00RealityKit 判斷物件點擊方法1<p> 直接用Reality Composer編輯物件行為</p><p></p><ol style="text-align: left;"><li>選取物件</li><li>顯示行為(<span style="background-color: white; caret-color: rgb(51, 51, 51); color: #333333; font-family: "SF Pro TC", "SF Pro Text", "SF Pro Icons", "PingFang TC", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 17px;">⌥⌘B</span>)</li><li>加入行為『點按與反轉』</li></ol><p></p>阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-83038177632551304422021-08-25T02:19:00.005-07:002021-08-25T02:20:07.048-07:00建立一個空的MapView<div class="separator" style="clear: both;"><span></span><a href="https://i.stack.imgur.com/r3vPQ.png" style="display: block; padding: 1em 0px; text-align: center;"><img alt="" border="0" data-original-height="312" data-original-width="381" src="https://i.stack.imgur.com/r3vPQ.png" width="320" /></a></div><ol style="text-align: left;">
<li>使用<a href="https://developer.apple.com/documentation/mapkit/mkmaptype">satellite</a>,除了地形圖,沒有多餘的文字。</li>
<li>建一個MKTileOverlay,利用<a href="https://developer.apple.com/documentation/mapkit/mktileoverlay/1452604-canreplacemapcontent">canReplaceMapContent</a>隱藏地圖。</li>
</ol><a name='more'></a>
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: "Andale Mono", "Lucida Console", Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>var mapView: MKMapView = {
let view = MKMapView()
view.mapType = .satellite
view.delegate = self
let overlay = MKTileOverlay()
overlay.canReplaceMapContent = true
view.addOverlay(overlay)
return view
}()
</code></pre>
PS. 記得重畫MKTileOverlay
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: "Andale Mono", "Lucida Console", Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>extension ContentViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
return MKOverlayRenderer(overlay: overlay)
}
}
</code></pre>阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-42326659917457131102019-05-02T21:07:00.000-07:002019-05-02T23:51:18.900-07:00在Heroku上發佈MochiWeb<script src="https://gist.github.com/yycking/5a96b14f705aeef2974c3e3c40622d99.js"></script>
注意:<br>
<ol>
<li>官方https://github.com/heroku/heroku-buildpack-erlang.git過舊(OTP和cebar),所以需要替代方案</li>
<li>Heroku的port是亂數產生,所以需要動態拿到port</li>
</ol>
<a href="http://roberto-aloi.com/blog/2013/07/13/create-deploy-erlang-cowboy-application-heroku/">參考</a>
阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-89717131711557651082018-07-20T20:55:00.002-07:002018-07-20T20:55:27.732-07:00SWIProlog on TextMate因TextMate提供的Plugin有問提,只好自己再改一下<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-lbLmtuEzvgs/W1Kucyy-ymI/AAAAAAAAB74/x4M7dDqsjoYv_ufpWwabzP2n2kZhM8bbACLcBGAs/s1600/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-07-21%2B%25E4%25B8%258A%25E5%258D%258811.52.43.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="635" data-original-width="1293" height="314" src="https://3.bp.blogspot.com/-lbLmtuEzvgs/W1Kucyy-ymI/AAAAAAAAB74/x4M7dDqsjoYv_ufpWwabzP2n2kZhM8bbACLcBGAs/s640/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-07-21%2B%25E4%25B8%258A%25E5%258D%258811.52.43.png" width="640" /></a></div>
<br />
<br />
<script src="https://gist.github.com/yycking/1b61302def9dee73ebf8ea765938af28.js"></script>阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-12270997169265140922018-06-07T22:45:00.003-07:002018-06-07T22:45:51.547-07:00iOS hot reloading1. 安裝
<a href="https://github.com/johnno1962/InjectionIII">InjectionIII</a> 並啟動<br />
2. 打開 XCode 和 專案<br />
3. 在appDelegate加入<br />
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px 'Fira Code'; color: #2e2e2e; background-color: #d0efd1}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px 'Fira Code'; color: #949494; background-color: #d0efd1}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px 'Fira Code'; color: #822d0f; background-color: #d0efd1}
p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px 'Fira Code'; color: #d75d1d; background-color: #d0efd1}
p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px 'Fira Code'; color: #1391d0; background-color: #d0efd1}
span.s1 {color: #1391d0}
span.s2 {color: #734092}
span.s3 {color: #8f2ac3}
span.s4 {color: #2e2e2e}
span.s5 {color: #822d0f}
span.s6 {color: #7c23aa}
</style>
<br />
<div class="p1">
<span class="s1">func</span> application(<span class="s1">_</span> application: <span class="s2">UIApplication</span>, didFinishLaunchingWithOptions launchOptions: [<span class="s3">UIApplicationLaunchOptionsKey</span>: <span class="s1">Any</span>]?) -> <span class="s3">Bool</span> {</div>
<div class="p2">
<span class="s4"><span class="Apple-converted-space"> </span></span>// Override point for customization after application launch.z</div>
<div class="p1">
<span class="Apple-converted-space"> </span><span class="s5">#if</span> <span class="s5">DEBUG</span></div>
<div class="p3">
<span class="s4"><span class="Apple-converted-space"> </span></span>#if<span class="s4"> </span>targetEnvironment<span class="s4">(</span>simulator<span class="s4">)</span></div>
<div class="p1">
<span class="Apple-converted-space"> </span><span class="s5">#if</span> <span class="s5">os</span>(<span class="s5">iOS</span>)</div>
<div class="p4">
<span class="s4"><span class="Apple-converted-space"> </span></span><span class="s2">Bundle</span><span class="s4">(path: </span>"/Applications/InjectionIII.app/Contents/Resources/iOSInjection.bundle"<span class="s4">)?.</span><span class="s6">load</span><span class="s4">()</span></div>
<div class="p3">
<span class="s4"><span class="Apple-converted-space"> </span></span>#elseif<span class="s4"> </span>os<span class="s4">(</span>tvOS<span class="s4">)</span></div>
<div class="p4">
<span class="s4"><span class="Apple-converted-space"> </span>Bundle(path: </span>"/Applications/InjectionIII.app/Contents/Resources/tvOSInjection.bundle"<span class="s4">)?.load()</span></div>
<div class="p1">
<span class="Apple-converted-space"> </span><span class="s5">#endif</span></div>
<div class="p3">
<span class="s4"><span class="Apple-converted-space"> </span></span>#elseif<span class="s4"> </span>os<span class="s4">(</span>macOS<span class="s4">)</span></div>
<div class="p4">
<span class="s4"><span class="Apple-converted-space"> </span>Bundle(path: </span>"/Applications/InjectionIII.app/Contents/Resources/macOSInjection.bundle"<span class="s4">)?.load()</span></div>
<div class="p1">
<span class="Apple-converted-space"> </span><span class="s5">#endif</span></div>
<div class="p1">
<span class="Apple-converted-space"> </span><span class="s5">#endif</span></div>
<div class="p5">
<span class="s4"><span class="Apple-converted-space"> </span></span>return<span class="s4"> </span>true</div>
<div class="p1">
<span class="Apple-converted-space"> </span>}</div>
<br /><br />
PS.<br />
已經秀出來的view並未改變。但是,新present出的view確實已經改變<br />
<br />
如需要改變已秀出來的view,需在每個controller上實作<br />
<div class="p1">
<span class="s1">@objc</span> <span class="s1">func</span> injected() {</div>
<div class="p2">
<span style="color: #2e2e2e;"> // 可馬上更新的code</span></div>
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px 'Fira Code'; color: #2e2e2e; background-color: #d0efd1}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px 'Fira Code'; color: #7c23aa; background-color: #d0efd1}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px 'Fira Code'; color: #2e2e2e; background-color: #d0efd1; min-height: 24.0px}
p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px 'Fira Code'; color: #934a82; background-color: #d0efd1}
span.s1 {color: #1391d0}
span.s2 {color: #2e2e2e}
</style>
<br />
<div class="p1">
}</div>
<br />
建議的懶人法<br />
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px 'Fira Code'; color: #2e2e2e; background-color: #d0efd1}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px 'Fira Code'; color: #7c23aa; background-color: #d0efd1}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px 'Fira Code'; color: #2e2e2e; background-color: #d0efd1; min-height: 24.0px}
p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px 'Fira Code'; color: #934a82; background-color: #d0efd1}
p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px 'Fira Code'; color: #949494; background-color: #d0efd1}
p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px 'Fira Code'; color: #8f2ac3; background-color: #d0efd1}
span.s1 {color: #1391d0}
span.s2 {color: #2e2e2e}
span.s3 {color: #d75d1d}
span.s4 {color: #734092}
span.s5 {color: #8f2ac3}
span.s6 {color: #7c23aa}
span.s7 {color: #934a82}
</style>
<br />
<div class="p1">
<span class="s1"><span style="color: #2e2e2e;"> </span><span style="color: #2e2e2e;"> </span>@objc</span> <span class="s1">func</span> injected() {</div>
<div class="p1">
// 移除全部vc/view</div>
<div class="p1">
<span class="Apple-converted-space"> </span><span class="s1">if</span> <span class="s1">self</span>.childViewControllers.count > <span class="s2">0</span>{</div>
<div class="p1">
<span class="Apple-converted-space"> </span><span class="s1">let</span> viewControllers:[<span class="s3">UIViewController</span>] = <span class="s1">self</span>.childViewControllers</div>
<div class="p1">
<span class="Apple-converted-space"> </span><span class="s1">for</span> viewContoller <span class="s1">in</span> viewControllers{</div>
<div class="p1">
<span class="Apple-converted-space"> </span>viewContoller.<span class="s4">willMove</span>(toParentViewController: <span class="s1">nil</span>)</div>
<div class="p1">
<span class="Apple-converted-space"> </span>viewContoller.<span class="s5">view</span>.<span class="s4">removeFromSuperview</span>()</div>
<div class="p1">
<span class="Apple-converted-space"> </span>viewContoller.<span class="s4">removeFromParentViewController</span>()</div>
<div class="p1">
<span class="Apple-converted-space"> </span>}</div>
<div class="p1">
<span class="Apple-converted-space"> </span>}</div>
<div class="p2">
<span class="Apple-converted-space"> </span></div>
<div class="p3">
<span class="s6"><span class="Apple-converted-space"> </span></span>configureView<span class="s6">()</span></div>
<div class="p1">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px 'Fira Code'; color: #2e2e2e; background-color: #d0efd1}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px 'Fira Code'; color: #2e2e2e; background-color: #d0efd1; min-height: 24.0px}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px 'Fira Code'; color: #934a82; background-color: #d0efd1}
span.s1 {color: #1391d0}
span.s2 {color: #d75d1d}
span.s3 {color: #734092}
span.s4 {color: #7c23aa}
span.s5 {color: #8f2ac3}
span.s6 {color: #2e2e2e}
</style>
</div>
<div class="p1">
<span class="Apple-converted-space"> </span>}</div>
<div class="p3">
<span class="Apple-converted-space"> </span></div>
<div class="p1">
<span class="Apple-converted-space"> </span><span class="s1">func</span> configureView() {</div>
<div class="p4">
<span class="s2"><span class="Apple-converted-space"> </span></span></div>
<div class="p1">
<span class="Apple-converted-space"> </span>}</div>
<div class="p3">
<span class="Apple-converted-space"> </span></div>
<div class="p1">
<span class="Apple-converted-space"> </span><span class="s1">override</span> <span class="s1">func</span> viewDidLoad() {</div>
<div class="p1">
<span style="color: #d75d1d;"> super.</span>viewDidLoad()</div>
<div class="p4">
<span class="s2"><span class="Apple-converted-space"> </span></span>configureView<span class="s2">()</span></div>
<div class="p1">
<span class="Apple-converted-space"> </span>}</div>
<br />阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-45308120405273064772018-03-18T20:30:00.002-07:002018-03-18T20:30:45.789-07:00Redux特調最近花了些時間,寫了一個redux的簡易範例,希望可以幫大家快速入門<br>
<iframe sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin" src="https://codesandbox.io/embed/7mlz5xxk70" style="border-radius: 4px; border: 0; height: 500px; overflow: hidden; width: 100%;"></iframe><br>原始碼<br><script src="https://gist.github.com/yycking/41073d801ad7541f19c2204b97e4abee.js"></script>阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-46050138085683352622017-12-25T03:33:00.003-08:002017-12-25T03:33:48.144-08:00利用CGContext轉出bitmap點陣字<script src="https://gist.github.com/yycking/6f1cff64dd5330d6cc01730781365132.js"></script>阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-55269149900827227992017-12-14T18:58:00.001-08:002017-12-14T18:58:37.522-08:00一直搞不懂<span style="background-color: white; color: #586069; font-family: , "blinkmacsystemfont" , "segoe ui" , "helvetica" , "arial" , sans-serif , "apple color emoji" , "segoe ui emoji" , "segoe ui symbol"; font-size: 16px;">bindActionCreators</span>的應用原理,<br />
花了一些時間研究,<br />
<br />
<br />
<br />
<script src="https://gist.github.com/yycking/69e54cc5d76c6e73fb8a98ef0a77401b.js"></script>
<a href="http://taobaofed.org/blog/2016/08/18/react-redux-connect/">參考資料</a>阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-87957702871323660592017-10-11T02:52:00.001-07:002017-10-11T02:52:47.362-07:00iOS簡繁互轉<script src="https://gist.github.com/yycking/e7c77c4e7dced7788eacda548005e35b.js"></script>阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-49505164930402011592017-10-11T00:20:00.002-07:002017-10-11T00:20:47.986-07:00UIAlertController客製化<div>
根據🍎的文件</div>
<div style="background-color: #fbf8e8; box-sizing: inherit; color: #333333; font-family: "SF Pro Text", "SF Pro Icons", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 17px; letter-spacing: -0.3569999933242798px; margin-top: 0.7em; padding: 0px;">
The <code class="code-voice" style="box-sizing: inherit; font-family: "SF Mono", Menlo, monospace; font-size: 1em; letter-spacing: 0px;">UIAlert<wbr style="box-sizing: inherit;"></wbr>Controller</code> class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.</div>
<br />
如果不想理他的話,就用以下的方法吧!<br />
<br />
<ol>
<li>打開contentViewController的束縛<div style="background-color: #d0efd1; color: #2e2e2e; font-family: "Monaco for Powerline"; font-size: 14px; font-stretch: normal; line-height: normal;">
<span style="color: #c42275;">@interface</span> UIAlertController (ContentViewController)</div>
<div style="background-color: #d0efd1; color: #2e2e2e; font-family: "Monaco for Powerline"; font-size: 14px; font-stretch: normal; line-height: normal;">
<span style="color: #c42275;">@property</span> (<span style="color: #c42275;">nonatomic</span>,<span style="color: #c42275;">retain</span>) UIViewController * contentViewController;</div>
<div style="background-color: #d0efd1; color: #c42275; font-family: "Monaco for Powerline"; font-size: 14px; font-stretch: normal; line-height: normal;">
@end</div>
</li>
<li>呼喚神秘的contentViewController<div style="background-color: #d0efd1; color: #2e2e2e; font-family: "Monaco for Powerline"; font-size: 14px; font-stretch: normal; line-height: normal;">
alertController.<span style="color: #539aa4;">contentViewController</span> = <span style="color: #8f2ac3;">UIStoryboard</span>(name: <span style="color: #b51f6c;">"Main"</span>, bundle: <span style="color: #c42275;">nil</span>).<span style="color: #7c23aa;">instantiateViewController</span>(withIdentifier: <span style="color: #b51f6c;">"contentViewController"</span>)</div>
</li>
<li>如果,使用tableview的話,記得加上<div style="background-color: #d0efd1; color: #2e2e2e; font-family: "Monaco for Powerline"; font-size: 14px; font-stretch: normal; line-height: normal;">
<div style="font-family: "Monaco for Powerline"; font-stretch: normal; line-height: normal;">
<span style="color: #c42275;">override</span> <span style="color: #c42275;">func</span> viewWillLayoutSubviews() {</div>
<div style="color: #7c23aa; font-family: "Monaco for Powerline"; font-stretch: normal; line-height: normal;">
<span style="color: #2e2e2e;"> </span><span style="color: #c42275;">super</span><span style="color: #2e2e2e;">.</span>viewWillLayoutSubviews<span style="color: #2e2e2e;">()</span></div>
<div style="font-family: "Monaco for Powerline"; font-stretch: normal; line-height: normal; min-height: 19px;">
</div>
<div style="color: #4f9b1c; font-family: "Monaco for Powerline"; font-stretch: normal; line-height: normal;">
<span style="color: #2e2e2e;"> </span>// make UITableView fit to content's size</div>
<div style="color: #8f2ac3; font-family: "Monaco for Powerline"; font-stretch: normal; line-height: normal;">
<span style="color: #2e2e2e;"> </span><span style="color: #c42275;">self</span><span style="color: #2e2e2e;">.</span>preferredContentSize<span style="color: #2e2e2e;"> = </span><span style="color: #c42275;">self</span><span style="color: #2e2e2e;">.</span>tableView<span style="color: #2e2e2e;">.</span>contentSize</div>
<br />
<div style="font-family: "Monaco for Powerline"; font-stretch: normal; line-height: normal;">
}</div>
</div>
</li>
</ol>
<br />
<a href="https://github.com/yycking/UIAlertController">code</a>阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-66143821871896425972017-01-10T21:29:00.000-08:002017-01-10T21:29:01.810-08:00iOS launch image的黑魔法<ol>
<li> 開一個新專案,在iPhone 7 size是 414x736
<br />
<a href="https://4.bp.blogspot.com/-V_usWcGDI18/WHXBFCWu0sI/AAAAAAAAB50/SI7Cx23Kjn8s7ACLVj3s3ZY1psw7rFiTwCLcB/s1600/Simulator%2BScreen%2BShot%2B2017%25E5%25B9%25B41%25E6%259C%258811%25E6%2597%25A5%2B%25E4%25B8%258B%25E5%258D%25881.15.24.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://4.bp.blogspot.com/-V_usWcGDI18/WHXBFCWu0sI/AAAAAAAAB50/SI7Cx23Kjn8s7ACLVj3s3ZY1psw7rFiTwCLcB/s320/Simulator%2BScreen%2BShot%2B2017%25E5%25B9%25B41%25E6%259C%258811%25E6%2597%25A5%2B%25E4%25B8%258B%25E5%258D%25881.15.24.png" width="180" /></a>
<br />
</li>
<li>移除LaunchScreen.storyboard,在iPhone 7 size是 320x480
<br />
<a href="https://2.bp.blogspot.com/-uUUyHa-f4zA/WHXBExI08mI/AAAAAAAAB5s/1jTTNs0gv48V_XTVUEtjDG8QPPqcpVFfACLcB/s1600/Simulator%2BScreen%2BShot%2B2017%25E5%25B9%25B41%25E6%259C%258811%25E6%2597%25A5%2B%25E4%25B8%258B%25E5%258D%25881.14.27.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://2.bp.blogspot.com/-uUUyHa-f4zA/WHXBExI08mI/AAAAAAAAB5s/1jTTNs0gv48V_XTVUEtjDG8QPPqcpVFfACLcB/s320/Simulator%2BScreen%2BShot%2B2017%25E5%25B9%25B41%25E6%259C%258811%25E6%2597%25A5%2B%25E4%25B8%258B%25E5%258D%25881.14.27.png" width="180" /></a>
<br />
</li>
<li> 新增Default-568h@2x.png和Default-568h@3x.png到專案中(就算是 1x1的圖也沒關係,重點是檔名要正確),在iPhone 7 size是 320x58
<br />
<a href="https://4.bp.blogspot.com/-I5-PP1qAshw/WHXBE_bz-5I/AAAAAAAAB5w/XhvnUlpG8hcuJo8aIZxHyyQtlAXcSZaigCLcB/s1600/Simulator%2BScreen%2BShot%2B2017%25E5%25B9%25B41%25E6%259C%258811%25E6%2597%25A5%2B%25E4%25B8%258B%25E5%258D%25881.16.06.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://4.bp.blogspot.com/-I5-PP1qAshw/WHXBE_bz-5I/AAAAAAAAB5w/XhvnUlpG8hcuJo8aIZxHyyQtlAXcSZaigCLcB/s320/Simulator%2BScreen%2BShot%2B2017%25E5%25B9%25B41%25E6%259C%258811%25E6%2597%25A5%2B%25E4%25B8%258B%25E5%258D%25881.16.06.png" width="180" /></a>
</li>
</ol>
阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-26078571416882172202016-12-01T01:07:00.002-08:002016-12-01T01:07:43.120-08:00讓NSLocalizedString自動讀Base.lproj <script src="https://gist.github.com/yycking/72bd2bdbff4dfd8afb0b28a358b93bab.js"></script>阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-23135994720718518622016-12-01T00:39:00.000-08:002016-12-01T00:40:00.618-08:00讓obj-c也可以跟swift一樣,enum支援字串<script src="https://gist.github.com/yycking/0fe3bbdeca63368254c06ecc9f133f44.js"></script><br />阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-59259211833589028192016-11-12T00:37:00.000-08:002016-11-12T00:37:15.356-08:00iOS MapView 影像處理<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-GnzRnDPRtZE/WCbUhm_irRI/AAAAAAAAB4w/s77F0F9VGdcJHaxY1cTq-3X97ph9PRingCLcB/s1600/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2016-11-12%2B%25E4%25B8%258B%25E5%258D%25884.35.53.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://1.bp.blogspot.com/-GnzRnDPRtZE/WCbUhm_irRI/AAAAAAAAB4w/s77F0F9VGdcJHaxY1cTq-3X97ph9PRingCLcB/s320/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2016-11-12%2B%25E4%25B8%258B%25E5%258D%25884.35.53.png" width="196" /></a></div>
<div style="color: #cf8724; font-family: Menlo; font-size: 13px; line-height: normal;">
<br /></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">import</span><span style="font-variant-ligatures: no-common-ligatures;"> MapKit</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"></span><br /></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">import</span><span style="font-variant-ligatures: no-common-ligatures;"> Foundation</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"></span><br /></div>
<div style="color: #36568a; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;">extension</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">UIImage</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">func</span><span style="font-variant-ligatures: no-common-ligatures;"> grayScaleImage() -> </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">UIImage</span><span style="font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> imageRect = </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">CGRect</span><span style="font-variant-ligatures: no-common-ligatures;">(x: </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">0</span><span style="font-variant-ligatures: no-common-ligatures;">, y: </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">0</span><span style="font-variant-ligatures: no-common-ligatures;">, width: </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">size</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">width</span><span style="font-variant-ligatures: no-common-ligatures;">, height: </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">size</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">height</span><span style="font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="color: #587ea8; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">let</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> colorSpace = </span><span style="font-variant-ligatures: no-common-ligatures;">CGColorSpaceCreateDeviceGray</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">();</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> width = </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">UInt</span><span style="font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">size</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">width</span><span style="font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> height = </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">UInt</span><span style="font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">size</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">height</span><span style="font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> context = </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">CGContext</span><span style="font-variant-ligatures: no-common-ligatures;">(data: </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">nil</span><span style="font-variant-ligatures: no-common-ligatures;">, width: </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">Int</span><span style="font-variant-ligatures: no-common-ligatures;">(width), height: </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">Int</span><span style="font-variant-ligatures: no-common-ligatures;">(height), bitsPerComponent: </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">8</span><span style="font-variant-ligatures: no-common-ligatures;">, bytesPerRow: </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">0</span><span style="font-variant-ligatures: no-common-ligatures;">, space: colorSpace, bitmapInfo: .</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">allZeros</span><span style="font-variant-ligatures: no-common-ligatures;">);</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> context?.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">draw</span><span style="font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">cgImage</span><span style="font-variant-ligatures: no-common-ligatures;">!, in: imageRect)</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> imageRef = context!.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">makeImage</span><span style="font-variant-ligatures: no-common-ligatures;">();</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> newImage = </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">UIImage</span><span style="font-variant-ligatures: no-common-ligatures;">(cgImage: imageRef!)</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">return</span><span style="font-variant-ligatures: no-common-ligatures;"> newImage</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;">}</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"></span><br /></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">class</span><span style="font-variant-ligatures: no-common-ligatures;"> OSMTileOverlay: </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">MKTileOverlay</span><span style="font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"></span><br /></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> cache = </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">NSCache</span><span style="font-variant-ligatures: no-common-ligatures;"><</span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">NSURL</span><span style="font-variant-ligatures: no-common-ligatures;">, </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">NSData</span><span style="font-variant-ligatures: no-common-ligatures;">>()</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> operationQueue = </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">OperationQueue</span><span style="font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">override</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">func</span><span style="font-variant-ligatures: no-common-ligatures;"> url(forTilePath path: </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">MKTileOverlayPath</span><span style="font-variant-ligatures: no-common-ligatures;">) -> </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">URL</span><span style="font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">return</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">URL</span><span style="font-variant-ligatures: no-common-ligatures;">(string: </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">String</span><span style="font-variant-ligatures: no-common-ligatures;">(format: </span><span style="color: #e82300; font-variant-ligatures: no-common-ligatures;">"http://tile.openstreetmap.org/%d/%d/%d.png"</span><span style="font-variant-ligatures: no-common-ligatures;">, path.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">z</span><span style="font-variant-ligatures: no-common-ligatures;">, path.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">x</span><span style="font-variant-ligatures: no-common-ligatures;">, path.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">y</span><span style="font-variant-ligatures: no-common-ligatures;">))!</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">override</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">func</span><span style="font-variant-ligatures: no-common-ligatures;"> loadTile(at path: </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">MKTileOverlayPath</span><span style="font-variant-ligatures: no-common-ligatures;">, result: </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">@escaping</span><span style="font-variant-ligatures: no-common-ligatures;"> (</span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">Data</span><span style="font-variant-ligatures: no-common-ligatures;">?, </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">Error</span><span style="font-variant-ligatures: no-common-ligatures;">?) -> </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">Void</span><span style="font-variant-ligatures: no-common-ligatures;">) {</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> url = </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">url</span><span style="font-variant-ligatures: no-common-ligatures;">(forTilePath: path)</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">if</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> cachedData = </span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">cache</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">object</span><span style="font-variant-ligatures: no-common-ligatures;">(forKey: url </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">as</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">NSURL</span><span style="font-variant-ligatures: no-common-ligatures;">) </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">as</span><span style="font-variant-ligatures: no-common-ligatures;">? </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">Data</span><span style="font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> result(cachedData, </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">nil</span><span style="font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> } </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">else</span><span style="font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> request = </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">URLRequest</span><span style="font-variant-ligatures: no-common-ligatures;">(url: url)</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">URLSession</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">shared</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">dataTask</span><span style="font-variant-ligatures: no-common-ligatures;">(with: request, completionHandler: {</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> [weak </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">]</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> data, response, error </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">in</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">if</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> data = data {</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">var</span><span style="font-variant-ligatures: no-common-ligatures;"> image = </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">UIImage</span><span style="font-variant-ligatures: no-common-ligatures;">(data: data)</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> image = image?.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">grayScaleImage</span><span style="font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> newData = </span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">UIImagePNGRepresentation</span><span style="font-variant-ligatures: no-common-ligatures;">(image!)</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">?.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">cache</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">setObject</span><span style="font-variant-ligatures: no-common-ligatures;">((newData </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">as</span><span style="font-variant-ligatures: no-common-ligatures;">? </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">NSData</span><span style="font-variant-ligatures: no-common-ligatures;">)!, forKey: url </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">as</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">NSURL</span><span style="font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> result(newData, error)</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">return</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> result(data, error)</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }).</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">resume</span><span style="font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<br />
<div style="font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;">}</span></div>
<div>
<span style="font-variant-ligatures: no-common-ligatures;"><br /></span></div>
阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-78869860333535139162016-10-22T02:58:00.001-07:002016-10-22T03:01:22.591-07:00iOS MapViewe更換地圖來源<div style="color: #c35900; font-family: Menlo; font-size: 13px; line-height: normal;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-V8ElRv711sk/WAs3mn1KozI/AAAAAAAAB4Q/7mz1ejlNNIIQaf8LkALrnWYFp2Ns9-FZgCLcB/s1600/Simulator%2BScreen%2BShot%2B2016%25E5%25B9%25B410%25E6%259C%258822%25E6%2597%25A5%2B%25E4%25B8%258B%25E5%258D%25885.53.29.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://1.bp.blogspot.com/-V8ElRv711sk/WAs3mn1KozI/AAAAAAAAB4Q/7mz1ejlNNIIQaf8LkALrnWYFp2Ns9-FZgCLcB/s320/Simulator%2BScreen%2BShot%2B2016%25E5%25B9%25B410%25E6%259C%258822%25E6%2597%25A5%2B%25E4%25B8%258B%25E5%258D%25885.53.29.png" width="180" /></a><a href="https://2.bp.blogspot.com/-BRdYplNMcxI/WAs3mi4_lnI/AAAAAAAAB4M/0HGZk1jw2_ouDkzUxfgcSwZzB229QWjmgCLcB/s1600/Simulator%2BScreen%2BShot%2B2016%25E5%25B9%25B410%25E6%259C%258822%25E6%2597%25A5%2B%25E4%25B8%258B%25E5%258D%25885.54.06.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://2.bp.blogspot.com/-BRdYplNMcxI/WAs3mi4_lnI/AAAAAAAAB4M/0HGZk1jw2_ouDkzUxfgcSwZzB229QWjmgCLcB/s320/Simulator%2BScreen%2BShot%2B2016%25E5%25B9%25B410%25E6%259C%258822%25E6%2597%25A5%2B%25E4%25B8%258B%25E5%258D%25885.54.06.png" width="180" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="color: #36568a; font-variant-ligatures: no-common-ligatures;"><br /></span></div>
<blockquote class="tr_bq">
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">import</span><span style="font-variant-ligatures: no-common-ligatures;"> MapKit</span></div>
<div style="color: black; font-family: Menlo; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"></span><br /></div>
<div style="font-family: Menlo; line-height: normal;">
<span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">class</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> OSMapView: </span><span style="font-variant-ligatures: no-common-ligatures;">MKMapView</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, </span><span style="font-variant-ligatures: no-common-ligatures;">MKMapViewDelegate</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="color: black; font-family: Menlo; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"></span><br /></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">override</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">init</span><span style="font-variant-ligatures: no-common-ligatures;">(frame: </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">CGRect</span><span style="font-variant-ligatures: no-common-ligatures;">) {</span></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">super</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">init</span><span style="font-variant-ligatures: no-common-ligatures;">(frame: frame)</span></div>
<div style="color: black; font-family: Menlo; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">setup</span><span style="font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">delegate</span><span style="font-variant-ligatures: no-common-ligatures;"> = </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">;</span></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div style="color: black; font-family: Menlo; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">required</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">init</span><span style="font-variant-ligatures: no-common-ligatures;">?(coder aDecoder: </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">NSCoder</span><span style="font-variant-ligatures: no-common-ligatures;">) {</span></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">super</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">init</span><span style="font-variant-ligatures: no-common-ligatures;">(coder: aDecoder);</span></div>
<div style="color: black; font-family: Menlo; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">setup</span><span style="font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">delegate</span><span style="font-variant-ligatures: no-common-ligatures;"> = </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">;</span></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div style="color: black; font-family: Menlo; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">func</span><span style="font-variant-ligatures: no-common-ligatures;"> setup() {</span></div>
<div style="color: #e82300; font-family: Menlo; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">let</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> template = </span><span style="font-variant-ligatures: no-common-ligatures;">"http://tile.openstreetmap.org/{z}/{x}/{y}.png"</span></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> overlay = </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">MKTileOverlay</span><span style="font-variant-ligatures: no-common-ligatures;">(urlTemplate: template)</span></div>
<div style="color: #587ea8; font-family: Menlo; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> overlay.</span><span style="font-variant-ligatures: no-common-ligatures;">canReplaceMapContent</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">true</span></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">add</span><span style="font-variant-ligatures: no-common-ligatures;">(overlay, level: .</span><span style="color: #587ea8; font-variant-ligatures: no-common-ligatures;">aboveLabels</span><span style="font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div style="color: black; font-family: Menlo; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">func</span><span style="font-variant-ligatures: no-common-ligatures;"> mapView(</span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">_</span><span style="font-variant-ligatures: no-common-ligatures;"> mapView: </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">MKMapView</span><span style="font-variant-ligatures: no-common-ligatures;">, rendererFor overlay: </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">MKOverlay</span><span style="font-variant-ligatures: no-common-ligatures;">) -> </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">MKOverlayRenderer</span><span style="font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">guard</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> tileOverlay = overlay </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">as</span><span style="font-variant-ligatures: no-common-ligatures;">? </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">MKTileOverlay</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">else</span><span style="font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="font-family: Menlo; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">return</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">MKOverlayRenderer</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div style="color: black; font-family: Menlo; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;">return</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #c35900; font-variant-ligatures: no-common-ligatures;">MKTileOverlayRenderer</span><span style="font-variant-ligatures: no-common-ligatures;">(tileOverlay: tileOverlay)</span></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<br />
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;">}</span></div>
</blockquote>
<div style="color: black; font-family: Menlo; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"></span></div>
<div style="color: black; font-family: Menlo; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"></span></div>
<br />
<div style="color: black; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><br /></span></div>
<div style="color: black; font-family: Menlo; line-height: normal;">
記得要修改info.plist<br />
<blockquote class="tr_bq">
<div style="color: #36568a; font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><dict></dict></span></div>
<div style="font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;"><key></key></span><span style="font-variant-ligatures: no-common-ligatures;">NSExceptionDomains</span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;"></span></div>
</blockquote>
</div>
</div>
<div style="font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;"><dict></dict></span></div>
<div style="font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;"><key></key></span><span style="font-variant-ligatures: no-common-ligatures;">tile.openstreetmap.org</span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;"></span></div>
<div style="font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;"><dict></dict></span></div>
<div style="font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;"><key></key></span><span style="font-variant-ligatures: no-common-ligatures;">NSExceptionAllowsInsecureHTTPLoads</span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;"></span></div>
<div style="font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;"><true></true></span></div>
<div style="font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;"><key></key></span><span style="font-variant-ligatures: no-common-ligatures;">NSIncludesSubdomains</span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;"></span></div>
<div style="font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;"><true></true></span></div>
<div style="font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;"></span></div>
<div style="font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #36568a; font-variant-ligatures: no-common-ligatures;"></span></div>
<div style="color: #36568a; font-family: Menlo; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;"></span></div>
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;"></span><br />
<div style="color: black; font-family: Menlo; line-height: normal;">
</div>
阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0tag:blogger.com,1999:blog-591361592481919222.post-14129221126016406992016-08-18T02:52:00.001-07:002016-08-18T02:52:40.625-07:00swift + crc32<blockquote class="tr_bq" style="background-color:black">
<span style="color: #ec33c1; font-family: "menlo"; font-size: 13px;">class</span><span style="color: #d8e1ea; font-family: "menlo"; font-size: 13px;"> CRC32 {</span><br />
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">var</span><span style="font-variant-ligatures: no-common-ligatures;"> data = </span><span style="color: #8757ff; font-variant-ligatures: no-common-ligatures;">NSData</span><span style="font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">private</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">static</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> crcTableLength = </span><span style="color: #f0b93a; font-variant-ligatures: no-common-ligatures;">Int</span><span style="font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #f0b93a; font-variant-ligatures: no-common-ligatures;">UInt8</span><span style="font-variant-ligatures: no-common-ligatures;">.max) + </span><span style="color: #6450ce; font-variant-ligatures: no-common-ligatures;">1</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">private</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">static</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">var</span><span style="font-variant-ligatures: no-common-ligatures;"> _crcTable = [</span><span style="color: #f0b93a; font-variant-ligatures: no-common-ligatures;">UInt32</span><span style="font-variant-ligatures: no-common-ligatures;">](count: crcTableLength, repeatedValue: </span><span style="color: #6450ce; font-variant-ligatures: no-common-ligatures;">0</span><span style="font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">private</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">static</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">var</span><span style="font-variant-ligatures: no-common-ligatures;"> crcTable: [</span><span style="color: #f0b93a; font-variant-ligatures: no-common-ligatures;">UInt32</span><span style="font-variant-ligatures: no-common-ligatures;">] {</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">get</span><span style="font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">if</span><span style="font-variant-ligatures: no-common-ligatures;"> _crcTable[</span><span style="color: #6450ce; font-variant-ligatures: no-common-ligatures;">1</span><span style="font-variant-ligatures: no-common-ligatures;">] != </span><span style="color: #6450ce; font-variant-ligatures: no-common-ligatures;">0</span><span style="font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">return</span><span style="font-variant-ligatures: no-common-ligatures;"> _crcTable</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">for</span><span style="font-variant-ligatures: no-common-ligatures;"> n </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">in</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #6450ce; font-variant-ligatures: no-common-ligatures;">0</span><span style="font-variant-ligatures: no-common-ligatures;">..<crctablelength span=""></crctablelength></span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">var</span><span style="font-variant-ligatures: no-common-ligatures;"> c = </span><span style="color: #f0b93a; font-variant-ligatures: no-common-ligatures;">UInt32</span><span style="font-variant-ligatures: no-common-ligatures;">(n)</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">for</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">_</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">in</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #6450ce; font-variant-ligatures: no-common-ligatures;">0</span><span style="font-variant-ligatures: no-common-ligatures;">..<</span><span style="color: #6450ce; font-variant-ligatures: no-common-ligatures;">8</span><span style="font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">if</span><span style="font-variant-ligatures: no-common-ligatures;"> ((c & </span><span style="color: #6450ce; font-variant-ligatures: no-common-ligatures;">1</span><span style="font-variant-ligatures: no-common-ligatures;">) != </span><span style="color: #6450ce; font-variant-ligatures: no-common-ligatures;">0</span><span style="font-variant-ligatures: no-common-ligatures;">) {</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> c = </span><span style="color: #6450ce; font-variant-ligatures: no-common-ligatures;">0xedb88320</span><span style="font-variant-ligatures: no-common-ligatures;"> ^ (c >> </span><span style="color: #6450ce; font-variant-ligatures: no-common-ligatures;">1</span><span style="font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> } </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">else</span><span style="font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> c = c >> </span><span style="color: #6450ce; font-variant-ligatures: no-common-ligatures;">1</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> _crcTable[n] = c</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">return</span><span style="font-variant-ligatures: no-common-ligatures;"> _crcTable</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">func</span><span style="font-variant-ligatures: no-common-ligatures;"> getCRC() -> </span><span style="color: #e5fb42; font-variant-ligatures: no-common-ligatures;">Byte4</span><span style="font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">var</span><span style="font-variant-ligatures: no-common-ligatures;"> crc: </span><span style="color: #e5fb42; font-variant-ligatures: no-common-ligatures;">Byte4</span><span style="font-variant-ligatures: no-common-ligatures;"> = </span><span style="color: #6450ce; font-variant-ligatures: no-common-ligatures;">0</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> crc = ~crc</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">var</span><span style="font-variant-ligatures: no-common-ligatures;"> buf = </span><span style="color: #f0b93a; font-variant-ligatures: no-common-ligatures;">UnsafePointer</span><span style="font-variant-ligatures: no-common-ligatures;"><</span><span style="color: #f0b93a; font-variant-ligatures: no-common-ligatures;">UInt8</span><span style="font-variant-ligatures: no-common-ligatures;">>(</span><span style="color: #fc9a85; font-variant-ligatures: no-common-ligatures;">data</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #dcb080; font-variant-ligatures: no-common-ligatures;">bytes</span><span style="font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> len = </span><span style="color: #fc9a85; font-variant-ligatures: no-common-ligatures;">data</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #dcb080; font-variant-ligatures: no-common-ligatures;">length</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">for</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">_</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">in</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #6450ce; font-variant-ligatures: no-common-ligatures;">0</span><span style="font-variant-ligatures: no-common-ligatures;">..<len span=""></len></span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> toBuf = buf.</span><span style="color: #dcb080; font-variant-ligatures: no-common-ligatures;">memory</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> buf += </span><span style="color: #6450ce; font-variant-ligatures: no-common-ligatures;">1</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> i = </span><span style="color: #f0b93a; font-variant-ligatures: no-common-ligatures;">Int</span><span style="font-variant-ligatures: no-common-ligatures;">((crc ^ </span><span style="color: #f0b93a; font-variant-ligatures: no-common-ligatures;">UInt32</span><span style="font-variant-ligatures: no-common-ligatures;">(toBuf)) & </span><span style="color: #6450ce; font-variant-ligatures: no-common-ligatures;">0xFF</span><span style="font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> crc = </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">self</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">dynamicType</span><span style="font-variant-ligatures: no-common-ligatures;">.crcTable[i] ^ crc >> </span><span style="color: #6450ce; font-variant-ligatures: no-common-ligatures;">8</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal; min-height: 15px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #ec33c1; font-variant-ligatures: no-common-ligatures;">return</span><span style="font-variant-ligatures: no-common-ligatures;"> ~crc</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div style="color: #d8e1ea; font-family: Menlo; font-size: 13px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;">}</span></div>
</blockquote>阿勇http://www.blogger.com/profile/06020128468091610219noreply@blogger.com0