简体   繁体   English

在 Swift 2 中随机化背景图像

[英]Randomizing Background Images in Swift 2

I'm building a simple app that displays random quotations.我正在构建一个显示随机报价的简单应用程序。 I'd like to add and randomize a series of background images, but I'm stuck.我想添加并随机化一系列背景图像,但我被卡住了。 Here's what I have so far.这是我到目前为止所拥有的。 Any suggestions are greatly appreciated.任何建议都非常感谢。

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
    } 
}

You can download a working project from here你可以 从这里下载一个工作项目

You can do all you want with this:你可以用这个做你想做的一切:

Note, Rather than create an array of images, which uses more memory than you really want to, create an array of names which are used to generate the images.请注意,与其创建一个图像数组,它使用的内存比您真正想要的要多,不如创建一个用于生成图像的名称数组。

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()
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM