選擇題app-電影通考驗 🎥
Published in
10 min readJun 8, 2020
選擇題app,參考了好多同學的做法,謝謝:)
程式碼
import UIKitclass ViewController: UIViewController {@IBOutlet weak var movieImage: UIImageView!@IBOutlet weak var imageSizeSlider: UILabel!@IBOutlet weak var questionLabel: UILabel!@IBOutlet var optionButton: [UIButton]!@IBOutlet var correctImage: [UIImageView]!@IBOutlet weak var scoreLabel: UILabel!var questionData = [Question(description: "本電影片名是?", answer: "活個痛快", imageName: "50-50-icon", option: ["五十之五十","五十件事","活個痛快"]),Question(description: "本電影劇中女主角叫?", answer: "艾蜜莉", imageName: "Amelie-icon", option: ["愛美麗","艾米粒","艾蜜莉"]),Question(description: "本電影男主從事之運動?", answer: "高爾夫", imageName: "Arthur-Newman-icon", option: ["高爾夫","游泳","棒球"]),Question(description: "本電影背景時間發生於", answer: "1861", imageName: "Gone-with-the-Wind-icon", option:["1911","1861","1951"]),Question(description: "本電影在imdb上之分數", answer: "6", imageName: "Hide-and-Seek-icon", option: ["5","6","7"]),Question(description: "本電影中文片名是?", answer: "人人有份", imageName: "In-the-Loop-icon", option: ["迴圈之中","人人有份","身在環中"]),Question(description: "本電影主角是何動物?", answer: "熊貓", imageName: "Kung-Fu-Panda-Holiday-icon", option: ["貓熊","狸貓","熊貓"]),Question(description: "本電影用什麼發電?", answer: "用叫發電", imageName: "Monsters-University-icon", option: ["用愛發電","用笑發電","用叫發電"]),Question(description: "本電影主角遇見了誰?", answer: "蘇格拉底", imageName: "Peaceful-Warrior-icon", option: ["蘇格拉底","柏拉圖","亞里士多德"]),Question(description: "本電影劇中離別禮物?", answer: "音樂盒", imageName: "Seven-Years-in-Tibet-icon", option: ["音樂盒","巧克力一盒","珠寶盒"]),Question(description: "本電影中文片名是?", answer: "福祿雙霸天", imageName: "The-Blues-Brothers-icon", option: ["藍調兄弟","福祿雙霸天","天才靈魂樂手"]),Question(description: "本電影電影發生地點?", answer: "紐約", imageName: "The-Nanny-Diaries-icon", option: ["舊金山","紐約","波士頓"])]var index = 0var score = 0var rightAnswer = ""override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view.//顯示題目圖片部分:設定畫面一開始時電影圖片的大小與位置,若slider畫面一開始時停在50%,則要去計算圖片寬高*0.5等,去計算呈現的大小與xy位置movieImage.frame = CGRect(x: 119, y: 120, width: 176, height: 155)//下面電影選擇題部分questionData.shuffle()scoreLabel.text = "\(score)"playGame()}
//建個playgame方法,選擇題運作方法func playGame(){questionLabel.text = questionData[index].descriptionrightAnswer = questionData[index].answermovieImage.image = UIImage(named:questionData[index].imageName)questionData[index].option.shuffle()for i in 0...2{optionButton[i].setTitle(questionData[index].option[i], for: UIControl.State.normal)}}//滑動slider改變電影圖片大小(放大縮小)@IBAction func sliderImagesize(_ sender: UISlider) {//label(叫imageSizeSlider)會顯示slider滑動的數值imageSizeSlider.text = "\(Int(sender.value*10))%"//宣告一個常數imageSize,當作Slider的sender.value,sender.value為float,要轉為CGFloatlet imageSize = CGFloat(sender.value)//宣告movieImage長寬的變數(因為設為每次以10%縮放可縮放十次,原圖大小為350*310,故起始的縮放大小應為原始圖十分之一,35*31)let imgWidth:CGFloat = 35let imgHeight:CGFloat = 31/*使用CGAffineTransform會縮放到超出頁面movieImage.transform = CGAffineTransform(scaleX: imageSize, y: imageSize)*//* movieImage.frame.size.width = imgWidth * imageSize,用frame會以左上角為原點縮放,用boounds會以中心點為縮放*/movieImage.bounds.size.width = imgWidth * imageSizemovieImage.bounds.size.height = imgHeight * imageSize//上面兩行也可以寫成一行 movieImage.bounds = CGRect(x: 0, y: 0, width: imgWidth*imageSize, height: imgWidth*imageSize)}@IBAction func chooseAnswerBtn(_ sender: UIButton) { let selectAnswer = sender.titleLabel?.text var correctNumber = -1 var isCorrect = false for button in optionButton{ //鎖定答案,按鈕不能再按了,button狀態改為不能點選 button.isEnabled = false if button.titleLabel!.text == rightAnswer{ correctNumber = button.tag
} } if selectAnswer == rightAnswer { isCorrect = true score += 10 scoreLabel.text = "\(score)" } for correctImage in correctImage{ if correctImage.tag == correctNumber{ correctImage.image = UIImage(named: "playstation-circle- icon") } if correctImage.tag == sender.tag{ if !isCorrect { correctImage.image = UIImage(named: "playstation-cross-icon") } } }}@IBAction func nextButton(_ sender: UIButton) {//清空correctImage,按鈕恢復for button in optionButton{button.isEnabled = true}for correctImage in correctImage{correctImage.image = UIImage(named: "")}if index == questionData.count - 1{let finalGameAlert = UIAlertController(title: "遊戲結束", message: "總計\(score)/120分", preferredStyle: .alert)let okActionButton = UIAlertAction(title: "知道了", style: .default, handler: nil)finalGameAlert.addAction(okActionButton)present(finalGameAlert, animated: true, completion: nil)}if index < questionData.count - 1 {index = index + 1playGame()}}@IBAction func allReplayBtn(_ sender: UIButton) {//清空correctImage,按鈕恢復for button in optionButton{button.isEnabled = true}for correctImage in correctImage{correctImage.image = UIImage(named: "")}score = 0scoreLabel.text = "\(score)"index = 0questionData.shuffle()playGame()}}
參考資料:
還有好多彼得潘#25選擇題app下的作品集分享~非常值得每個都點開來閱讀!收穫滿滿~感謝大家。