选择一个随机的UI图像视图-Swift 2 [英] Pick a random UI Image View - Swift 2

查看:94
本文介绍了选择一个随机的UI图像视图-Swift 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有3张图片

 @IBOutlet weak var imageOne: UIImageView!
  @IBOutlet weak var imageThree: UIImageView!
  @IBOutlet weak var imageTwo: UIImageView!

我从一组图像中生成一个随机图像,生成的随机图像是正确的"图像

I generate a random image from an array of images, the random image generated is the "correct" image

这是我目前的工作方式

  let randomIndex = Int(arc4random_uniform(UInt32(famousPeople.count)))
  let correct = famousPeople[randomIndex]
  self.imageOne.image = UIImage(named: correct)

显然,这个问题是imageOne始终是正确的图像,我希望正确的图像是随机的一两个或三个.

Obviously the issue with this is the imageOne is always the correct image, I want the correct image to be random either one two or three.

我认为我可以做以下事情

I thought I could do the following

let randomImageIndex = Int(arc4random_uniform(UInt32(famousPeople.count)))
            var imageArray: [String] = ["imageOne", "imageTwo", "imageThree"]
            let randomImage = imageArray[randomImageIndex]

然后

self.randomImage.image = UIImage(named: correct)

但是我得到了错误消息视图控制器无类型随机图像.

However I get the error message view controller no type random image.

是否有一个很好的选择随机UI imageview的方法?然后我要为其分配我随机选择的正确"图像

Is there a good way to choose a random UI imageview? and then I want to assign to it my randomly chosen "correct" image

额外的上下文

然后用户可以选择正确的"图像并获得一条消息

The user can then choose the "correct" image and gets a message

推荐答案

import Foundation

var number1 = 0
var number2 = 0
var number3 = 0

// You would like randomly choose one of number1, number2 and number2
// and assign to it random number ...

func foo(inout n: [Int]) {
    let i = Int(arc4random_uniform(UInt32(n.count)))
    for j in 0..<n.count {
        if i == j {
            n[j] = random()
        } else {
            n[j] = 0
        }
    }
}

var arr = [number1, number2, number3]
foo(&arr) // [0, 1804289383, 0]
foo(&arr) // [846930886, 0, 0]
foo(&arr) //  [0, 0, 1681692777]

这篇关于选择一个随机的UI图像视图-Swift 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆