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

張又壬
10 min readJul 19, 2021

--

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

關鍵技術:

  • 地圖: 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

以下範例都在 playground 裡執行

用 AVPlayer 播音樂

import AVFoundationlet url = URL(string: "https://audio-ssl.itunes.apple.com/itunes-assets/AudioPreview124/v4/1a/0b/e5/1a0be5ab-12e7-8bb3-2fa6-5724a3dda437/mzaf_13480818112944396654.plus.aac.p.m4a")let player = AVPlayer(url: url!)player.play()

取得 url 的方法

在 Link Mark 輸入喜歡的歌曲和地區

點擊歌曲後把 id 複製起來,在瀏覽器輸入,把剛剛複製的 id 貼到id=後面

https://itunes.apple.com/tw/lookup?id=1560927427

輸入完成後會自動下載一個文字檔,搜尋 previewUrl 即可獲得歌曲試聽連結

用 AVPlayerViewController 播影片

import AVKitimport PlaygroundSupportlet url = URL(string: "https://movietrailers.apple.com/movies/marvel/black-widow/black-widow-trailer-legacy_h1080p.mov")let player = AVPlayer(url: url!)let controller = AVPlayerViewController()controller.player = playerPlaygroundPage.current.liveView = controllerplayer.play()

為了在 playground 看到 controller 的畫面,必須利用 live view 顯示

可參考

利用 SFSafariViewController 顯示網頁

import SafariServicesimport PlaygroundSupportlet url = URL(string: "https://www.pui-pui.com.tw/")let controller = SFSafariViewController(url: url!)PlaygroundPage.current.liveView = controller

顯示地圖

透過 MapKit 顯示地圖

import MapKitimport PlaygroundSupportlet mapView = MKMapView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))mapView.region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 25.036976, longitude: 121.499927), latitudinalMeters: 1000, longitudinalMeters: 1000)let lungshanAnnotation = MKPointAnnotation()lungshanAnnotation.coordinate = CLLocationCoordinate2D(latitude: 25.036976, longitude: 121.499927)lungshanAnnotation.title = "龍山寺"lungshanAnnotation.subtitle = "好多遊客"mapView.addAnnotation(lungshanAnnotation)let sushiAnnotation = MKPointAnnotation()sushiAnnotation.coordinate = CLLocationCoordinate2D(latitude: 25.039903, longitude: 121.502686)sushiAnnotation.title = "三味食堂"sushiAnnotation.subtitle = "巨無霸鮭魚壽司"mapView.addAnnotation(sushiAnnotation)PlaygroundPage.current.liveView = mapView

透過 MapKit 介紹我家附近景點和美食

列印時間

import Foundationvar time = Date.nowprint("現在時間",time)time.addTimeInterval(86400)print("一天過後的時間", time)

練習:

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

extension Date {  mutating func addTimeInterval(hours: Double, minutes: Double, seconds: Double) {self.addTimeInterval(hours * 60.0 * 60.0 + minutes * 60.0 + seconds)  }}var today = Date.nowtoday.addTimeInterval(hours: 3, minutes: 50, seconds: 20)print(today)

時間變成特定格式字串

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

時間格式可參考

let dateComponents = Calendar.current.dateComponents(in: TimeZone.current, from: Date.now)let month = dateComponents.monthlet day = dateComponents.daylet year = dateComponents.yearlet weekday = dateComponents.weekday

用 AVSpeechSynthesizer 講話

透過引入 AVFoundation,讓指定的字串能夠透過指定的方式發出聲音。

import AVFoundationlet speechUtterance =  AVSpeechUtterance(string: "As you like, My Pleasure,光を目指して生まれてきた,絆 細い糸に結ばれて")speechUtterance.voice = AVSpeechSynthesisVoice(language: "ja-JP")print("Min:", AVSpeechUtteranceMinimumSpeechRate, "Max:", AVSpeechUtteranceMaximumSpeechRate, "default:", AVSpeechUtteranceDefaultSpeechRate)speechUtterance.rate = AVSpeechUtteranceDefaultSpeechRatespeechUtterance.pitchMultiplier = 1.5let synthesizer = AVSpeechSynthesizer()synthesizer.speak(speechUtterance)

根據裡面 rate 解說 裡面有 Minimum(0.0), Default(0.5), Maximum(1.0) 可以用,但是我用 default 聽起來感覺略快

pitchMultiplier 0.5~2.0 ,0.5 聲音最低 2.0 最高

聽程式照著字串唸歌詞感覺好好玩啊 XDD

參考文章:

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

張又壬
張又壬

Written by 張又壬

目前正在努力學習 swift

No responses yet

Write a response