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

CSY
彼得潘的 Swift iOS / Flutter App 開發教室
8 min readAug 13, 2021

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

用 AVPlayer 播音樂

// 用AVPlayer 播音樂import AVFoundationlet url2 = URL(string:"https://audio-ssl.itunes.apple.com/itunes-assets/AudioPreview115/v4/59/f7/0e/59f70e1f-cb95-de7e-0322-9b8e8e46e489/mzaf_15954149055705219534.plus.aac.p.m4a")let player2 = AVPlayer(url: url2!)player2.play()

查詢音樂網址的方法:

用 AVPlayerViewController 播影片

// 用AVPlayerviewController播影片import AVKitimport PlaygroundSupportlet url = URL(string: "https://movietrailers.apple.com/movies/universal/old2021/old-trailer-2b_h480p.mov")let player = AVPlayer(url: url!)let controller = AVPlayerViewController()controller.player = playerPlaygroundPage.current.liveView = controllerplayer.play()

電影預告連結

利用 SFSafariViewController 顯示網頁

// 利用SFSafariViewController顯示網頁import SafariServicesimport PlaygroundSupportlet url = URL(string: "https://www.apple.com/tw-edu/shop/back-to-school")let controller = SFSafariViewController(url: url!)PlaygroundPage.current.liveView = controller
apple 2021 back to school

顯示地圖

// 利用MKMapView 顯示地圖,設定顯示的內容// 台南安平經緯度 22.991687, 120.185192,安平古堡 23.001587, 120.160866,二二八紀念公園 22.991560, 120.182669,台南市政府永華 22.992531, 120.185264// 透過PlaygroundPage 的 liveView 。import MapKitimport PlaygroundSupportlet mapView = MKMapView(frame: CGRect(x: 0, y: 0, width: 1500, height: 1500))//顯示中文UserDefaults.standard.set("zh", forKey: "AppleLanguages")// 用MKPointAnnotation加入地圖標記let anping = MKPointAnnotation()anping.title = "安平"anping.coordinate = CLLocationCoordinate2D(latitude:22.991687, longitude:120.185192)anping.subtitle = "美食之都"mapView.addAnnotation(anping)let the228park = MKPointAnnotation()the228park.title = "228紀念公園"the228park.coordinate = CLLocationCoordinate2D(latitude:22.991560, longitude: 120.182669)the228park.subtitle = "紀念228"mapView.addAnnotation(the228park)// 可以利用array 來設定。// let annotations = [anping, zeelandia,the228park,tainanGOV]// mapView.addAnnotations(annotations)// center:型別CLLocationCoordinate2D ,代表地圖中心點的經緯度座標。// latitudinalMeters,longtiudinalMeters :經緯度範圍,單位是公尺。mapView.region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 22.991687  , longitude: 120.185192), latitudinalMeters: 1000, longitudinalMeters: 1000)// 設定地圖的樣式mapView.mapType = MKMapType.hybridPlaygroundPage.current.liveView = mapView

列印時間

//列印時間import Foundationvar time = Date()// 計算距離現在3小時50分20秒的時間time.addTimeInterval((60*3+50)*60+20)print(time)

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

//將時間變成特定格式的字串let now = Date()// DateFormatter 可設定時間顯示的格式。let dateFormatter = DateFormatter()dateFormatter.dateFormat = "yyyy/MM/dd"let dateString = dateFormatter.string(from: now)
// 用 Calendar.current 取得現在的月曆let today = Date()let dateComponents = Calendar.current.dateComponents(in: TimeZone.current, from: today)let month = dateComponents.monthlet day = dateComponents.daylet year = dateComponents.yearlet weekday = dateComponents.weekday

用 AVSpeechSynthesizer 講話

//AVSpeechUtterance 是要講的話,AVSpeechSynthesizer 是發出聲音的合成器,AVSpeechSynthesisVoice 控制講話聲音的語言。import AVFoundationlet speechUtterance =  AVSpeechUtterance(string: "愛一個人就是要讓他知道。")speechUtterance.voice = AVSpeechSynthesisVoice(language: "zh-TW")speechUtterance.rate = 0.1speechUtterance.pitchMultiplier = 2let synthesizer = AVSpeechSynthesizer()synthesizer.speak(speechUtterance)

參考資料

--

--