練習Segmented & Button-利用古人撩妹圖練習

學習目的
-利用Segmented的action搭配if else控制imageView
-利用Button的action搭配if else控制imageView

隨便找著主題練習,這次非常簡單都是上課學到的,只要知道如何利用action跟if else就可以做出來了

1.Segmented控制imageView
-將Segmented拉近ViewController利用action
-將imageView拉出IBOutlet
-搭配if else利用selectedSegmentIndex判斷所選項目

@IBOutlet weak var imageView: UIImageView!
@IBAction func imageChangeSegmented(_ sender: UISegmentedControl) {
    if sender.selectedSegmentIndex == 0 {
        imageView.image = UIImage(named: "03")
    } else if sender.selectedSegmentIndex == 1 {
        imageView.image = UIImage(named: "12")
    }
}

2.Button控制imageView
-將Button拉近ViewController利用action
-將imageView拉出IBOutlet
-利用array將圖片名稱放進去

let imageName =    ["01","02","03","04","05","06","07","08","09","10","11","12"]
var imageNumber = 0
@IBOutlet weak var imageView: UIImageView!
@IBAction func changeButton(_ sender: UIButton) {
    if imageNumber < 11{
        imageNumber+=1
        imageView.image = UIImage(named: "\. 
(imageName[imageNumber])")
    } else {
        imageNumber = 0
        imageView.image = UIImage(named: "\
(imageName[imageNumber])")
    }
}