#42 利用 iOS SDK 各式型別生成東西,設定它的屬性和呼叫方法-《海洋奇緣,Moana》

作業:

1. 用 AVPlayer 播音樂

因為女兒最近音樂課練唱她選了 "How Far I’ll Go" 所以我現在整天被這首歌洗腦, 好啦 好啦~ 翅膀硬了,看妳能走多遠

用以下下載.txt

https://itunes.apple.com/lookup?id=1440639531
import AVFoundation
let url = URL(string: "https://audio-ssl.itunes.apple.com/itunes-assets/AudioPreview115/v4/3b/63/f8/3b63f80c-e0d3-c202-9f14-104400632217/mzaf_13407272909629172062.plus.aac.p.m4a")
let player = AVPlayer(url: url!)
player.play()

2. 用 AVPlayerViewController 播影片

import AVKit
import PlaygroundSupport
let url = URL(string: "480p")
let player = AVPlayer(url: url!)
let controller = AVPlayerViewController()
controller.player = player
PlaygroundPage.current.liveView = controller
player.play()

3. 利用 SFSafariViewController 顯示網頁

import SafariServices
import PlaygroundSupport
let url = URL(string:"https://disneyanimation.com/films/moana/")
let controller = SFSafariViewController(url: url!)
PlaygroundPage.current.liveView = controller

4. 顯示地圖

因為《海洋奇緣》是以玻里尼西亞的原住民毛利人為原型。(呃…當然也有專家覺得踩了很多雷…)

import MapKit
import PlaygroundSupport
let mapView = MKMapView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
mapView.region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 19.57327, longitude: 157.81533), latitudinalMeters: 1000, longitudinalMeters: 1000)
PlaygroundPage.current.liveView = mapView

latitude 緯度:19.57327。S, 經度 longtude。W: 157.81533

因為是小島,地圖上看起來空空的…

5 . 列印時間

將時間變成特定格式字串

import Foundation
let now = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE, yyyy年MM月dd日 HH:mm"
let dateString = dateFormatter.string(from: now)

取得今天幾月幾號的數字

  1. year: 取得今天年份的數字。
  2. weekday: 取得今天星期幾的數字。
  3. weekdayOriginal: The ordinal number of weekdays.
  4. weekOfMonth: 取得這週是本月的第幾週
  5. weekOfYear:取得這週是今年的第幾週
import Foundation
let today = Date()
let dateComponents = Calendar.current.dateComponents(in: TimeZone.current, from: today)
let year = dateComponents.year
let weekday = dateComponents.weekday
let weekdayOriginal = dateComponents.weekdayOrdinal
let weekOfMonth = dateComponents.weekOfMonth
let weekOfYear = dateComponents.weekOfYear

6. 用 AVSpeechSynthesizer 講話

使用AVSpeechSynthesize 讓毛伊說出 " You’re Welcome"

我找了一下玻里尼西亞人的可能語言:

Polynesian languages, group of about 30 languages belonging to the Eastern, or Oceanic, branch of the Austronesian (Malayo-Polynesian) language family and most closely related to the languages of Micronesia and Melanesia.

也太多了,現在最多人說的是薩摩耶語,原住民說的是毛利語

import AVFoundation
let speechUtterance = AVSpeechUtterance(string:"tena koe")
speechUtterance.voice = AVSpeechSynthesisVoice(language: "mao")
speechUtterance.rate = 0.5
speechUtterance.pitchMultiplier = 0
let sythesizer = AVSpeechSynthesizer()
sythesizer.speak(speechUtterance)

有一種說法是玻里尼西亞祖先來自台灣,同是南島語系:

6. 玻里尼西亞國旗

(中間的圓圖先用偷懶版)

先用GeoGebra看一下長寬與座標:

SCHEMECOLOR 分析RGB ((red: 204/255, green: 0, blue: 51/255, alpha: 1)

import UIKit

var PolynesiaFlag = CGRect(x: 0, y: 0, width: 1368, height: 913)
let PolynesiaFlagView = UIView(frame: PolynesiaFlag)
PolynesiaFlagView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
let UpRedRec = CGRect(x: 0, y: 0, width: 1368, height: 220)
let UpRedRecView = UIView(frame: UpRedRec)
UpRedRecView.backgroundColor = UIColor(red: 204/255, green: 0, blue: 51/255, alpha: 1)
let downRedRec = CGRect(x: 0, y: 693, width: 1368, height: 220)
let downRedRecView = UIView(frame: downRedRec)
downRedRecView.backgroundColor = UIColor(red: 204/255, green: 0, blue: 51/255, alpha: 1)
let frame = CGRect(x: 0, y: 0, width: 0, height: 0)
let LogoView = UIImageView(image: UIImage(named: "PolynesiaLogo.png"))
LogoView.frame = CGRect(x: 440, y: 250, width: 400, height: 410)
PolynesiaFlagView.addSubview(UpRedRecView)
UpRedRecView.addSubview(downRedRecView)
PolynesiaFlagView.addSubview(downRedRecView)
PolynesiaFlagView.addSubview(LogoView)

7. 定義 function, 列印歌詞

How Far I’ll Go

I’ve been staring at the edge of the water
Long as I can remember
Never really knowing why
I wish I could be the perfect daughter
But I come back to the water
No matter how hard I try

Every turn I take
Every trail I track
Every path I make
Every road leads back to the place I know
Where I cannot go
Where I long to be

See the light where the sky meets the sea
It calls me
And no one knows how far it goes
If the wind in my sail on the sea stays behind me
One day I’ll know

If I go there’s just no telling how far I’ll go

I know everybody on this island
Seems so happy on this island
Everything is by design
I know everybody on this island
Has a role on this island
So maybe I can roll with mine

I can lead with pride
I can make us strong
I’ll be satisfied if I play along
But the voice inside sings a different song
What is wrong with me

See the light as it shines on the sea
It’s blinding
But no one knows how deep it goes
And it seems like it’s calling out to me
So come find me
And let me know

What’s beyond that line
Will I cross that line

See the light where the sky meets the sea
It calls me
And no one knows how far it goes
If the wind in my sail on the sea stays behind me

One day I’ll know
How far I’ll go

其實它的歌詞雖然長得很像,但重覆的句子竟然不多,只有粗斜體的部份

太多了,就不全打完了

import UIKitfunc SeeTheLight() {
print("See the light where the sky meets the sea")
print("It calls me")
print("And no one knows how far it goes")
print("If the wind in my sail on the sea stays behind me")
print("One day I'll know")
print("how far I'll go")
}
print("I've been staring at the edge of the water\nLong as I can remember\nNever really knowing why\nI wish I could be the perfect daughter\nBut I come back to the wate\nNo matter how hard I try\n")
print("Every turn I take\nEvery trail I track\nEvery path I make\nEvery road leads back to the place I know\nWhere I cannot go\nWhere I long to be\n")
SeeTheLight()

--

--