#42 利用 iOS SDK 各式型別生成東西,設定它的屬性和呼叫方法

目的: 學習利用 iOS SDK 各式型別生成東西,設定它的屬性和呼叫方法。

關鍵技術:

  • 地圖: MapKit,MKMapView,MKCoordinateRegion
  • 播音樂: AVFoundation,URL,AVPlayer
  • 播影片: AVKit,AVPlayerViewController
  • 網頁: SafariServices,SFSafariViewController
  • 時間: Foundation,Date,DateFormatter,Calendar,DateComponents,Void
  • App 講話: AVFAudio,AVSpeechSynthesizer,AVSpeechUtterance,AVSpeechSynthesisVoice
  • playground: PlaygroundSupport,PlaygroundPage,liveView,type property

以下程式範例建議在 playground 練習。

用 AVPlayer 播音樂

import AVFoundation

let url = URL(string: "https://audio-ssl.itunes.apple.com/apple-assets-us-std-000001/AudioPreview118/v4/69/0e/98/690e98db-440d-cb0c-2bff-91b00a05bdda/mzaf_1674062311671795807.plus.aac.p.m4a")
let player = AVPlayer(url: url!)
player.play()

URL 代表某個資源的位置(location of a resource),比方網頁的網址,或是 App 裡檔案的路徑。關於 AVPlayer(url: url!) 裡的 ! ,之後會說明。

查詢音樂網址的方法可參考以下連結。值得注意的,YouTube 網頁的網址不能用 AVPlayer 播放,因為它是網頁,不是影片,除非透過一些方法找出影片的網址。

若是檔案放在 Google Drive,也可以參考以下方法取得檔案連結。

用 AVPlayerViewController 播影片

import AVKit
import PlaygroundSupport

let url = URL(string: "https://video-ssl.itunes.apple.com/apple-assets-us-std-000001/Video128/v4/ac/7c/62/ac7c6274-60ea-5b7c-4c99-f08d78bfe574/mzvf_484000410198456586.640x352.h264lc.U.p.m4v")
let player = AVPlayer(url: url!)
let controller = AVPlayerViewController()
controller.player = player
PlaygroundPage.current.liveView = controller
player.play()

為了在 playground 看到 controller 的畫面,我們必須利用 live view 顯示,相關說明可參考以下連結。

參考的影片連結來源: 廣告影片網站。

利用 SFSafariViewController 顯示網頁

import SafariServices
import PlaygroundSupport

let url = URL(string: "https://apppeterpan.strikingly.com")
let controller = SFSafariViewController(url: url!)
PlaygroundPage.current.liveView = controller

顯示地圖

列印時間

import Foundation

var time = Date()
print(time)
time.addTimeInterval(91)
print(time)

var time = Date()
利用 Date() 得到現在的時間。在程式裡 Date 型別的東西代表時間,由於 Date 是在 Foundation 裡定義,所以須 import Foundation。

print(time)

時間印出時會顯示 +0 時區的時間,相關說明可參考以下連結。

time.addTimeInterval(91)
在 Date 資料上增加秒數,比方 Date 資料的時間是 2018/9/9 10:10:10,addTimeInterval(10) 後將變成 2018/9/9 10:10:20。

練習:

計算距離現在3小時50分20秒的時間。

將時間變成特定格式的字串

import Foundation

let now = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy年MM月dd日"
let dateString = dateFormatter.string(from: now)

let dateFormatter = DateFormatter()

DateFormatter 可設定時間顯示的格式。

dateFormatter.dateFormat = "yyyy年MM月dd日"

yyyy,MM,dd 等時間格式的說明,可參考以下連結。

取得今天幾月幾號的數字

import Foundation

let today = Date()
let dateComponents = Calendar.current.dateComponents(in: TimeZone.current, from: today)
let month = dateComponents.month
let day = dateComponents.day

利用 Calendar.current 取得現在的月曆,然後呼叫它的 function dateComponents。 function dateComponents 將回傳型別 DateComponents 的資料,它包含詳細的時間資料,比方年月日時分秒等。

練習

  1. 取得今天年份的數字。
  2. 取得今天星期幾的數字。

取得相對時間的 RelativeDateTimeFormatter

使用 AVSpeechSynthesizer 講話

import AVFAudio

let utterance = AVSpeechUtterance(string: "就是開不了口,讓她知道。我一定會呵護著妳,也逗妳笑。")
utterance.voice = AVSpeechSynthesisVoice(language: "zh-TW")
utterance.rate = 0.1
utterance.pitchMultiplier = 0.5
let synthesizer = AVSpeechSynthesizer()
synthesizer.speak(utterance)

AVSpeechUtterance 是要講的話,AVSpeechSynthesizer 是發出聲音的合成器。呼叫 AVSpeechSynthesizer 的 speak function 講話。

語言字串列表可參考以下連結。

ps: 現在甚至也不用自己查語言代碼, Xcode 的 predictive code completion 從前後文就能猜出正確的代碼。

不知道要說什麼也可以請 AI 編台詞。

其它補充功能。

--

--

彼得潘的 iOS App Neverland
彼得潘的 100 道 Swift iOS App 謎題

彼得潘的iOS App程式設計入門,文組生的iOS App程式設計入門講師,彼得潘的 Swift 程式設計入門,App程式設計入門作者,http://apppeterpan.strikingly.com