#21 解放 iOS SDK 的強大魔法,生成各式物件,並設定屬性及呼叫函數
利用各式 Kit 協助開發
Published in
8 min readAug 7, 2022
核心練習:
- 地圖: MapKit,MKMapView,MKCoordinateRegion
- 播音樂: AVFoundation,URL,AVPlayer
- 播影片: AVKit,AVPlayerViewController
- 網頁: SafariServices,SFSafariViewController
- 時間: Foundation,Date,DateFormatter,Calendar,DateComponents,Void
- App 講話: AVFoundation,AVSpeechSynthesizer,AVSpeechUtterance,AVSpeechSynthesisVoice
- playground: PlaygroundSupport,PlaygroundPage,liveView,type property
- 用 AVPlayer 播放音樂:AV Foundation
import AVFoundation//建立一個url來存取網址let url=URL(string: "https://audio-ssl.itunes.apple.com/itunes-assets/AudioPreview115/v4/1c/27/7a/1c277a0d-394d-766d-f6ea-a0a81f609911/mzaf_16652781993487448951.plus.aac.p.m4a")//建立一個player 來存取播放器,裡頭嵌入網址let player=AVPlayer(url: url!)//呼叫 player 執行 playplayer.play()
記得要查詢播放網址可以從彼得潘的這篇看:
2. 用 AVPlayerViewController 播影片
import AVKitimport PlaygroundSupport//建立一個 url 來存取網址let url=URL(string: "https://video-ssl.itunes.apple.com/itunes-assets/Video115/v4/9f/5d/4e/9f5d4ee1-eb59-04e0-907d-b30c1d4c8390/mzvf_8294274645378689398.640x298.h264lc.U.p.m4v")let player=AVPlayer(url: url!)//建立一個AVPlayerControllerlet controller=AVPlayerViewController()//讓「播放器」做「播放的動作」,播放「網址內容」controller.player=playerPlaygroundPage.current.liveView=controllerplayer.play()
再度感謝大神彼得潘:這篇有解釋如何使用 Live View 來操作顯示 Controller或是 View:
3. 利用 SFSafariViewController 顯示網頁
import SafariServicesimport PlaygroundSupport//利用 url 存取網址let url=URL(string: "https://medium.com/@ArthurMing")//利用 controller 存取 Safari 頁面 Controllerlet controller = SFSafariViewController(url:url!)//利用 liveView 檢視PlaygroundPage.current.liveView=controller
打這段的時候不知道是不是 Xcode 出現 Bug,提示不斷消失,但最後是可以在 liveView 中看見成果的!
4. 顯示地圖
import MapKitimport PlaygroundSupport//建立一個 mapView 畫布let mapView=MKMapView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
//設定畫布上顯示的位置mapView.region=MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 24.998695, longitude: 121.581246), latitudinalMeters: 1000, longitudinalMeters: 1000)//建立座標點圖釘let mrtStation=MKPointAnnotation()//建立座標的位置及名稱mrtStation.coordinate=CLLocationCoordinate2D(latitude: 24.998487, longitude: 121.579764)mrtStation.title="捷運動物園站"mrtStation.subtitle="文湖線尾站"let planePlace=MKPointAnnotation()planePlace.coordinate=CLLocationCoordinate2D(latitude: 24.997610, longitude: 121.581140)planePlace.title="蕨園"//要記得將座標點加入 mapView//let annotations=[mrtStation,planePlace]mapView.addAnnotations([mrtStation,planePlace])PlaygroundPage.current.liveView=mapView
5. 列印時間
import Foundation// Date() 可得到現在時間var time=Date()print(time)time.addTimeInterval((3*60+50)*60+20)print(time)
addTimeInterval 是用來計算距離現在幾秒鐘以後時間的函數~
更改時間格式並變為字串:
import Foundation//建立now變數儲存時間let now=Date()//建立一個變數儲存時間格式let dateFormatter=DateFormatter()dateFormatter.dateFormat="MM/dd/yyyy"//建立一個變數儲存字串化的時間(裡面輸入現在時間)let dateString=dateFormatter.string(from: now)print(dateString)
時間格式整理表參考:
存取現在的時間資料:
import Foundationlet today=Date()//建立變數取得現在的時間的資料let dateComponents=Calendar.current.dateComponents(in: TimeZone.current, from: today)//上面呼叫calendar.current的函數dateComponentslet month = dateComponents.monthlet day = dateComponents.daylet second = dateComponents.secondprint(month,day,second)
4. 用 AVSpeechSynthesizer 講話
import AVFoundation//說話的工具let avTalker=AVSpeechSynthesizer()//要說的話let avSpeech=AVSpeechUtterance(string: "Hello World!")//說話的工具,說,話avTalker.speak(avSpeech)
可事後使用 .voice .rate .pitchMultiplier 來調整「要說的話」~
參考彼得潘這篇大補帖:(詳細整理)
學習完 iOS SDK 以後是不是覺得很充實呢?