在Swift 2中随机化背景图像

时间:2015-10-05 17:49:21

标签: ios swift user-interface

我正在构建一个显示随机引用的简单应用。我想添加并随机化一系列背景图像,但我被卡住了。这是我到目前为止所拥有的。任何建议都非常感谢。

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 :(得分: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()
    }

}