Ten Years Challenge!

從大一到現在,時間也快10年了,真是快,一開始看到阿哲跟Julia學姊的程式,想說把兩者結合起來XD,寫程式當然要配音樂囉XD

當然廢話不多說先來看個示範影片(背景音樂是Pan-LitterSoldier,Pan的片尾曲,覺得還蠻好聽的XD)

在接下來是程式碼

宣告區
@IBOutlet weak var pictureView: UIImageView!
@IBOutlet weak var datePicker: UIDatePicker!
@IBOutlet weak var autoPlaySwitch: UISwitch!

@IBOutlet weak var backwardBtn: UIButton!
@IBOutlet weak var playBtn: UIButton!
@IBOutlet weak var forwardBtn: UIButton!
@IBOutlet weak var songNameTextView: UITextView!

let dateFormatter = DateFormatter()
var dateString:String = ""
var timer:Timer?
let dateImageView = ["20101225","20111217","20111224","20121007","20130410","20140115","20140427","20150301","20160818","20170909","20181010","20190113"]
var dateImageViewNum = 0
let songAry = ["LittleSoldier","DrinkTea","Aesthetic","9277"]
var songNum = 0
var songPlayer = AVPlayer()
var playerItem:AVPlayerItem?
var songLooper: AVPlayerLooper?
var isPlaying = false //判斷music是否播放中

下面算是主Func,這裡碰到的問題有Bundle.main.path(forResource: string?, ofType: string?),一開始都只抓到nil,後來加了inDirectory才OK,後來問Peter,才知道原來是我當初,直接把music資料夾拖曳到左邊專案檔列(可能是這緣故,所以才需要指定資料夾讓他去找檔案的路徑),雖然有建立連結,但是後來Peter說還是要在那邊按右鍵New Group With Folder,才是比較正確的做法,沒想到一個不同的動作搞了我兩個多小時,真的要處處小心!
一不小心碰到BUG就死掉了。

override func viewDidLoad() {
super.viewDidLoad()
datePicker.locale = Locale(identifier: "zh_TW")
dateFormatter.dateFormat = "yyyy/MM/dd"
pictureView.image = UIImage(named: dateImageView[dateImageViewNum])
autoPlaySwitch.isOn = false //一開始呈現Switch Off
initPlaySong()

// Do any additional setup after loading the view.
}
func initPlaySong(){ //初始化AVplayer
let nowSong = songAry[songNum]
songNameTextView.text = songAry[songNum]
let sound = Bundle.main.path(forResource: nowSong, ofType: "mp3", inDirectory: "music")
playerItem = AVPlayerItem(url: URL(fileURLWithPath: sound!))
songPlayer = AVPlayer(playerItem: playerItem!)
}

func autoPlayImage(){ //自動播放照片Array
if dateImageViewNum >= dateImageView.count{
dateImageViewNum = 0
chooseImage(date: dateImageViewNum)
pictureView.image = UIImage(named: dateImageView[dateImageViewNum])
}else{
chooseImage(date: dateImageViewNum)
pictureView.image = UIImage(named: dateImageView[dateImageViewNum])
}
dateImageViewNum += 1
}
func setTimer(){ //設定每兩秒就執行該閉包
timer = Timer.scheduledTimer(withTimeInterval: 2, repeats: true){ (timer) in
self.autoPlayImage()

}
}


func chooseImage(date:Int){ //用來把圖片日期丟給Datepicker
switch date{
case 0:
dateString = "2010/12/25"
case 1:
dateString = "2011/12/17"
case 2:
dateString = "2011/12/24"
case 3:
dateString = "2012/10/07"
case 4:
dateString = "2013/04/10"
case 5:
dateString = "2014/01/15"
case 6:
dateString = "2014/04/27"
case 7:
dateString = "2015/03/01"
case 8:
dateString = "2016/08/18"
case 9:
dateString = "2017/09/09"
case 10:
dateString = "2018/10/10"
default:
dateString = "2019/01/13"
}
//modified datePicker date
let Pdate = dateFormatter.date(from: dateString)
datePicker.date = Pdate!
}
override func viewDidDisappear(_ animated: Bool) { //當離開畫面時,停止timer
timer?.invalidate()
}

下面為@IBAction func,這裡沒碰到什麼問題。

@IBAction func datePickerChange(_ sender: UIDatePicker) {    }//雖然沒做什麼事,但是還是些出來比較安心,怕沒寫又出現什麼奇怪BUG  XD.@IBAction func dateSwitchChange(_ sender: UISwitch) {  //當Switch點擊時的動作,這裡用來設定音樂是否一起自動播放
if sender.isOn{
setTimer()
songPlayer.play()
playBtn.setImage(UIImage(systemName: "pause.fill"), for: UIControl.State.normal)
isPlaying = true
}else{
songPlayer.pause()
playBtn.setImage(UIImage(systemName: "play.fill"), for: UIControl.State.normal)
isPlaying = false
timer?.invalidate()
}
}

@IBAction func playSongBtn(_ sender: UIButton) { //這裡的想法是想說做一個動態按鈕,按下去播放時,會自動切換不同圖標。
if isPlaying == false{
playBtn.setImage(UIImage(systemName: "pause.fill"), for: UIControl.State.normal)
isPlaying = true
songPlayer.play()

}else{
playBtn.setImage(UIImage(systemName: "play.fill"), for: UIControl.State.normal)
isPlaying = false
songPlayer.pause()
}

}

@IBAction func previousSongBtn(_ sender: Any) { //這裡就是上一首歌動作,跟上一個播放鈕一樣的原理。
if songNum == 0{
songNum = songAry.count-1
}else{
songNum -= 1
}
initPlaySong()
playBtn.setImage(UIImage(systemName: "pause.fill"), for: UIControl.State.normal)
isPlaying = true
songPlayer.play()
}
@IBAction func nextSongBtn(_ sender: Any) { //反之下一首歌
if songNum == songAry.count-1{
songNum = 0
}else{
songNum += 1
}
initPlaySong()
playBtn.setImage(UIImage(systemName: "pause.fill"), for: UIControl.State.normal)
isPlaying = true
songPlayer.play()
}

一開始是先看阿哲跟Julia學姊的程式,想說把兩個結合在一起,順便在練習個小作業用UIBezierPath,來自定特別圖案。

GitHub:

Reference:

阿哲_ #69 10YearChallenge(Bruno Mars)

Julia學姊_人生轉捩點之 My 10YearChallenge!

Peter_用 UIBezierPath 設計特別形狀的圖片 — 比方斜邊 & 愛心

Peter_自訂 controller 類別和利用 viewDidLoad 客製 App 畫面

--

--