<font color="#8A2BE2">[Udemy IOS Angela]</font> Ch7 Using and Understanding Apple Documentation
===
# Ch7 Outline
* Project Skeleton
* How to play sound by searching documentation and StackOverflow
* Swift Function
* Linking multiple elements to one IBAction
* Creating Function with input
* Play different sounds for different buttons
## 71. The 5 Step Approach to Solve Any Programming Problem
建議解決問題順序
1. Google
(程式碼做什麼事+程式語言+搜尋平台)
(Binary search Swift Stackoverflow)
2. StackOverflow
3. 用在自己code裡面使用 (長壓command鍵,滑鼠移過可以看基本程式碼解釋)
4. 看官方文件
5. 客製化
AVFoundation 音樂文件可以控制攝像頭、處理音頻
AVAudioPlayer 如果播放單曲
Buddle 存儲在磁盤上的代碼和資源,使用Bundle來定位聲音文件
AVAudioSession
```swift=
import UIKit
import AVFoundation //Apple的Libary
class ViewController: UIViewController {
var player: AVAudioPlayer! //建立
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func keyPressed(_ sender: UIButton) {
playSound()
}
func playSound() {
let url = Bundle.main.url(forResource: "C", withExtension: "wav") //Url設置為檔名C格式wav
player = try! AVAudioPlayer(contentsOf: url!) //把url放進 player變數
player.play() //player變數使用play方法
}
}
```
## 72. [Swift Deep Dive] Functions and Scope
```swift=
//建立函式
func getMilk() {
}
//呼叫函式
getMike()
```
:::danger
快速縮排
command+A -> 上方Editor -> Re-Indent
:::
## 73. [Coding Exercise] Functions Part 1
```swift=
print("Starting map")
start()
//Write your code here:
right()
right()
right()
right()
down()
down()
down()
down()
down()
```

## 74. Linking Multiple Buttons to the Same IBAction

印出點擊顏色

印出點擊title文字
方法1

方法2

## 75. [Swift Deep Dive] Functions with Inputs and Type Inference
長按option按鍵滑向變數,可以看變數細節

```swift=
var 變數名:型別 = 變數value
var Angela: Int = 0983214226
func myFunction(參數:型別){
}
```
:::danger
變數如何命名
```swift=
方式1
var myAge = 12 //型別是數字
myAge = 13
myAge = "Three" //型別是字串,不同型別不能賦值,會報錯
方式2
var myAge: Int = 12
```
搭配函式與參數
```swift=
func 函式名稱 (名 屬性) {
var 變數名稱 = 參數名稱 * 1.5
}
getMilk(名: 參數)
//建立函式
func getMilk (bottles Int) {
var cost = bottles * 1.5
}
//呼叫函式
getMilk(bottles: 2)
```
:::
```swift=
//加法函式
// 函式 參數1 類型 參數2 類型
func add(input1: Int, input2: Int) {
print(input1+input2)
}
//呼叫函示 參數1 值 參數2 值
add(input1: 2, input2: 3)
```

<hr>
```swift=
//叫名子函式func greeting() {
print("hello")
}
func greeting2(whoToGreet: String) {
print("Hello \(whoToGreet)")
}
greeting2(whoToGreet: "Bob")
greeting2(whoToGreet: "Angela")
```
### 編碼練習#5
我的code
```swift
//Don't change this code:
func calculator() {
let a = 3 //example first input
let b = 4 //example second input
add(n1: a, n2: b)
subtract(n1: a, n2: b)
multiply(n1: a, n2: b)
divide(n1: a, n2: b)
}
//Write your code below this line to make the above function calls work.
func add(n1: Int, n2: Int){
print(n1+n2)
}
func subtract(n1: Int, n2: Int){
print(n1-n2)
}
func multiply(n1: Int, n2: Int){
print(n1*n2)
}
func divide(n1: Int, n2: Int){
print(n1/n2)
}
//Don't move or change this code:
calculator()
```

Angela 修正
```swift=
func divide(n1: Int, n2: Int) {
print(Double (n1) / Double (n2))
}
```

## 76. Playing Different Xylophone Sounds
按每一個都是C聲音
```swift=
import UIKit
import AVFoundation //Apple的Libary
class ViewController: UIViewController {
var player: AVAudioPlayer! //建立
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func keyPressed(_ sender: UIButton) {
//print(sender.backgroundColor) //印出點擊的顏色
print(sender.currentTitle) //印出title的文字
playSound() 沒有參數
}
func playSound() {
檔案名是C
let url = Bundle.main.url(forResource: "C", withExtension: "wav") //Url設置為檔名C格式wav
player = try! AVAudioPlayer(contentsOf: url!) //把url放進 player變數
player.play() //player變數使用play方法
}
}
```
修改過
```swift=
import UIKit
import AVFoundation //Apple的Libary
class ViewController: UIViewController {
var player: AVAudioPlayer! //建立
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func keyPressed(_ sender: UIButton) {
//print(sender.backgroundColor) //印出點擊的顏色
//print(sender.currentTitle) //印出title的文字
永遠會是按鈕所以放心加上!
抓取點擊的Title
名 : 參數
playSound(soundName: sender.currentTitle!) //呼叫函示(名:參數)
}
名 : 參數
func playSound(soundName: String) {
名 : 參數
let url = Bundle.main.url(forResource: soundName, withExtension: "wav") //Url設置為檔名C格式wav
player = try! AVAudioPlayer(contentsOf: url!) //把url放進 player變數
player.play() //player變數使用play方法
}
}
```

## 77. Boss Challenge
* 按下按鈕,透明度變成一半
* 按下按鈕 印出Start ->0.2秒後印出End (應該跟timerset有關)
* 按下按鈕 ->0.2秒後透明度變成一半
```swift=
//按下按鈕函式
@IBAction func keyPressed(_ sender: UIButton) {
//print(sender.backgroundColor) //印出點擊的顏色
//print(sender.currentTitle) //印出title的文字
playSound(soundName: sender.currentTitle!) //呼叫函示(名:參數)
//透明度少一半
//Reduces the sender's (the button that got pressed) opacity to half.
sender.alpha = 0.5
//0.2秒後啟動
//Code should execute after 0.2 second delay.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
//變回原本顏色
//Bring's sender's opacity back up to fully opaque.
sender.alpha = 1.0
}
}
```
完整程式碼
```swift=
/*
* 按下按鈕,透明度變成一半
* 按下按鈕 印出Start ->0.2秒後印出End (應該跟timerset有關)
* 按下按鈕 ->0.2秒後透明度變成一半
*/
import UIKit
import AVFoundation //Apple的Libary
class ViewController: UIViewController {
var player: AVAudioPlayer! //建立
override func viewDidLoad() {
super.viewDidLoad()
}
//按下按鈕函式
@IBAction func keyPressed(_ sender: UIButton) {
//print(sender.backgroundColor) //印出點擊的顏色
//print(sender.currentTitle) //印出title的文字
playSound(soundName: sender.currentTitle!) //呼叫函示(名:參數)
//透明度少一半
//Reduces the sender's (the button that got pressed) opacity to half.
sender.alpha = 0.5
//0.2秒後啟動
//Code should execute after 0.2 second delay.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
//變回原本顏色
//Bring's sender's opacity back up to fully opaque.
sender.alpha = 1.0
}
}
//播音樂函式
func playSound(soundName: String) {
let url = Bundle.main.url(forResource: soundName, withExtension: "wav") //Url設置為檔名C格式wav
player = try! AVAudioPlayer(contentsOf: url!) //把url放進 player變數
player.play() //player變數使用play方法
}
//變透明函式
}
```
:::info
關於alpha

:::
:::danger
非同步

:::
:::success
同步

:::
[alpha參考](https://medium.com/彼得潘的-swift-ios-app-開發問題解答集/swiftui-生成-color-的方法-766974bd0c6a)
[同步/非同步參考1](https://www.appcoda.com.tw/grand-central-dispatch/)
[同步/非同步參考2](https://medium.com/程式愛好者/dispatchqueue基本介紹-adb0dd6b135e)
###### tags: `[Udemy IOS Angela Yu]`