2015-10-05 76 views
-1

我正在構建一個顯示隨機引號的簡單應用程序。我想添加並隨機化一系列背景圖片,但我被卡住了。這是迄今爲止我所擁有的。任何建議,非常感謝。在Swift 2中隨機化背景圖像

class FirstViewController: UIViewController { 
    @IBOutlet weak var lblQuote: UILabel! 
    @IBOutlet weak var imgBackground: UIImageView! 

    var quoteArray = ["Hustle is the dark horse of creativity,\n the close cousin of Grit and Tenacity.\n Without the hustle, drive, and complete devotion to making things happen, you are average.\n\n- Rebecca Rebouché ", 
     "What you want to do is not study in some prestigious field,\nbut study something that a prestigious field will grow out of.\n That’s the really big win.\n\n - Paul Graham", 
     "There’s nothing like being tossed into necessity to help you figure out who you are and what matters most in life –\n necessity may be the mother of invention, but it’s even more so the fairy godmother of self-invention.\n\n - Maria Popova", 
     "Pursuing that feeling of not really knowing what to do,and choosing what doesn’t quite seem like the logical next step, but feels right at a gut level, is how I’ve pieced together where I am today. It’s about that combination of anxiety about going into territory where I’m totally unfamiliar, and not knowing a big chunk of it.\n\n - Liz Danzico", 
     "On the fringes...is where disruptive innovation begins.\n\n - Neri Oxman", 
     "I wanted to be a certain kind of woman.\n I became that kind of woman.\n\n - Diane von Furstenburg"] 

    var imageArray = [ 
     UIImage(named: "boldlivingbackground.png"), 
     UIImage(named: "background1.png"), 
     UIImage(named: "background2.png"), 
     UIImage(named: "background3.png"), 
     UIImage(named: "background4.png"), 
     UIImage(named: "background5.png"), 
     UIImage(named: "background6.png"), 
    ] 

    var numberQuote = 0 
    var numberImage = 0 
    var numberButton = 0 
    var numberCheck = 0 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    @IBAction func btnExploreOtherQuotes(sender: UIButton) { 
     selectQuote() 
    } 

    func selectQuote() {  
     numberQuote = Int(arc4random_uniform(5))  
     while numberCheck == numberQuote { 
      numberQuote = Int(arc4random_uniform(UInt32(quoteArray.count))) 
     } 
     printQuote() 
     numberCheck = numberQuote 
    } 

    func printQuote() { 
     lblQuote.text = "\(quoteArray[numberQuote])" 
    } 

    func selectImage() {  
     numberImage = Int(arc4random_uniform(60))  
     while numberCheck == numberImage { 
      numberImage = Int(arc4random_uniform(UInt32(imageArray.count))) 
     } 
     numberCheck = numberImage 
    } 
} 
+1

你是什麼意思,你「卡住」?你特別注意什麼?與預期行爲相比實際發生了什麼? –

+0

對不起。我認爲我的帖子明確表示,我不知道如何隨機化背景圖片,儘管已成功將報價隨機化。具體來說,我不明白如何顯示一個隨機圖像從一個單獨的數組隨機引用。 – natashagodwin

+0

你什麼時候調用'selectImage()'?不應該在'btnExploreOtherQuotes(...)'中調用嗎?你在哪裏設置你隨機選擇的新圖像? –

回答

1

你可以下載一個工作項目from here

你可以做你想做這個:

注意,而不是創建圖像的陣列,它使用更多的內存比你真的想,創建一個用於生成圖像的名稱數組。

import UIKit 

class ViewController: UIViewController { 

    @IBOutlet var imageView: UIImageView! 
    @IBOutlet var quoteLabel: UILabel! 

    let quotes = [ 
     "Hustle is the dark horse of creativity,\n the close cousin of Grit and Tenacity.\n Without the hustle, drive, and complete devotion to making things happen, you are average.\n\n- Rebecca Rebouché ", 
     "What you want to do is not study in some prestigious field,\nbut study something that a prestigious field will grow out of.\n That’s the really big win.\n\n - Paul Graham", 
     "There’s nothing like being tossed into necessity to help you figure out who you are and what matters most in life –\n necessity may be the mother of invention, but it’s even more so the fairy godmother of self-invention.\n\n - Maria Popova", 
     "Pursuing that feeling of not really knowing what to do,and choosing what doesn’t quite seem like the logical next step, but feels right at a gut level, is how I’ve pieced together where I am today. It’s about that combination of anxiety about going into territory where I’m totally unfamiliar, and not knowing a big chunk of it.\n\n - Liz Danzico", 
     "On the fringes...is where disruptive innovation begins.\n\n - Neri Oxman", 
     "I wanted to be a certain kind of woman.\n I became that kind of woman.\n\n - Diane von Furstenburg" 
    ] 

    // These are just vector solid colours stored in the Asset Catalogue. 
    let images = [ 
     "Image1", 
     "Image2", 
     "Image3", 
     "Image4", 
     "Image5", 
     "Image6" 
    ] 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     setupView() 
    } 

    @IBAction func onNext(sender: UIButton) { 
     setupView() 
    } 

    private func randomImage() -> UIImage { 
     let idx = Int(arc4random_uniform(UInt32(images.count))) 
     guard let image = UIImage(named: images[idx]) else { fatalError() } 

     return image 
    } 

    private func randomQuote() -> String { 
     let idx = Int(arc4random_uniform(UInt32(quotes.count))) 
     return quotes[idx] 
    } 

    private func setupView() { 
     imageView.image = randomImage() 
     quoteLabel.text = randomQuote() 
    } 

} 
+0

它的工作!非常感謝你的幫助! – natashagodwin