#8 紙娃娃app開發_複習亂數
Published in
8 min readAug 21, 2021
這份作業卡關有點久,主要是對秀出圖片方式還有圖片檔名的設定摸索了很久。
以下為圖片下載與教學參考:
下載圖檔後將圖檔加到 Assets
將圖檔加到 Xcode 的 Assets.xcassets,勾選 Preserve Vector Data,Scales 選擇 Single Scale。
要記得將render as 調成Original image
否則圖片呈現就會變成以下情形。
outlet跟action拉好後打好換裝的程式碼,基本上都按照自己想的方式切換。
但加上亂數功能時,array裡面放著錯誤的圖片檔名,按下按鈕後一直呈現無圖狀況,卡了好幾天才確定是要輸入自己改過後的檔名。
以下為完整程式碼:
import UIKitclass ViewController: UIViewController {
@IBOutlet weak var hairStyleImageView: UIImageView!
@IBOutlet weak var faceImageView: UIImageView!
@IBOutlet weak var clothesImageView: UIImageView!
@IBOutlet weak var glassesImageView: UIImageView!
@IBOutlet var glassesListView: UIView!
@IBOutlet var clothesListView: UIView!
@IBOutlet var faceListView: UIView!
@IBOutlet var hairStyleListView: UIView!
let hairStylelist = ["hairStyle1","hairStyle2","hairStyle3","hairStyle4","hairStyle5","hairStyle6","hairStyle7","hairStyle8","hairStyle9","hairStyle10","hairStyle11","hairStyle12","hairStyle13","hairStyle14","hairStyle15","hairStyle16","hairStyle17","hairStyle18","hairStyle19","hairStyle20","hairStyle21","hairStyle22","hairStyle23","hairStyle24","hairStyle25","hairStyle26","hairStyle27","hairStyle28","hairStyle29","hairStyle30","hairStyle31","hairStyle32","hairStyle33","hairStyle34","hairStyle35","hairStyle36","hairStyle37","hairStyle38","hairStyle39","hairStyle40","hairStyle41","hairStyle42","hairStyle43","hairStyle44","hairStyle45","No Hair"]
let clothesList = ["clothes1","clothes2","clothes3","clothes4","clothes5","clothes6","clothes7","clothes8","clothes9","clothes10","clothes11","clothes12","clothes13","clothes14","clothes15","clothes16","clothes17","clothes18","clothes19","clothes20","clothes21","clothes22","clothes23","clothes24","clothes25","clothes26","clothes27","clothes28","clothes29","clothes30"]
override func viewDidLoad() {
super.viewDidLoad()
glassesListView.isHidden = true
clothesListView.isHidden = true
faceListView.isHidden = true
hairStyleListView.isHidden = false
}@IBAction func clothesChangeBtn(_ sender: UIButton) {
clothesImageView.image = sender.currentImage
}
@IBAction func faceChangeBtn(_ sender: UIButton) {
faceImageView.image = sender.currentImage
}@IBAction func hairStyleChangeBtn(_ sender: UIButton) {
hairStyleImageView.image = sender.currentImage
}@IBAction func glassesChangeBtn(_ sender: UIButton) {
glassesImageView.image = sender.currentImage
}
@IBAction func clothesBtn(_ sender: UIButton) {
glassesListView.isHidden = true
clothesListView.isHidden = false
faceListView.isHidden = true
hairStyleListView.isHidden = true
}@IBAction func faceBtn(_ sender: UIButton) {
glassesListView.isHidden = true
clothesListView.isHidden = true
faceListView.isHidden = false
hairStyleListView.isHidden = true
}
@IBAction func hairStyleBtn(_ sender: UIButton) {
glassesListView.isHidden = true
clothesListView.isHidden = true
faceListView.isHidden = true
hairStyleListView.isHidden = false
}
@IBAction func glassesBtn(_ sender: UIButton) {
glassesListView.isHidden = false
clothesListView.isHidden = true
faceListView.isHidden = true
hairStyleListView.isHidden = true
}
@IBAction func randomBtn(_ sender: UIButton) {
hairStyleImageView.image = UIImage(named: hairStylelist.randomElement()!)
clothesImageView.image = UIImage(named: clothesList.randomElement()!)
}
}