0

I'm working on an app that will start off with four images and eventually more images will be adding onto the screen but for now I'm trying to figure out how I can have the app randomly select one UIImageView every time and set the alpha to 0.7 and the rest to 1.0. I tried doing different ways to make it work but it just doesn't seem good. How can I go about doing this with the code I have already? How would I be able to tell that they found the lower alpha image?

func randomCGFloat() -> CGFloat {
    return CGFloat(arc4random()) / CGFloat(UInt32.max)
}

extension UIColor {
    static func randomColor() -> UIColor {
        let r = randomCGFloat()
        let g = randomCGFloat()
        let b = randomCGFloat()

        return UIColor(red: r, green: g, blue: b, alpha: 1.0)
    }
}


class ViewController: UIViewController {
    @IBOutlet var image1: UIImageView?
    @IBOutlet var image2: UIImageView?
    @IBOutlet var image3: UIImageView?
    @IBOutlet var image4: UIImageView?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let getColor = UIColor.randomColor()

        self.image1?.backgroundColor = getColor
        self.image2?.backgroundColor = getColor
        self.image3?.backgroundColor = getColor
        self.image4?.backgroundColor = getColor
    }
  • Can you please be more specific about what "just doesn't seem good?" Are the alphas not being set correctly? Are the random `CGFloat`s not being calculated correctly? – ndmeiri Jul 07 '15 at 06:29

1 Answers1

0

If you need all your images to have an alpha of 1.0 and only to have an alpha of 0.7 I would suggest the following:

First get all your images in an array in your viewDidLoad(), then randomly select one element and apply an alpha of 0.7 (default alpha for the others will be 1.0).

You can then add a tap recognizer to each of your imageViews and check in the called method to see if alpha is 0.7

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let getColor = UIColor.randomColor()

    self.image1?.backgroundColor = getColor
    self.image2?.backgroundColor = getColor
    self.image3?.backgroundColor = getColor
    self.image4?.backgroundColor = getColor

    var imageViewsArray: [UIImageView] = [image1, image2, image3, image4]

    var randomIndex = Int(arc4random()) % Int(imageViewsArray.count)
    imageViewsArray[randomIndex].alpha = 0.7

    for imageView in imageViewsArray {
        imageView.userInteractionEnabled = true

        let tapRecognizer = UITapGestureRecognizer(target: self, action:"imageTapped:")

        imageView.addGestureRecognizer(tapRecognizer)
    }
}

func imageTapped(gestureRecognizer: UITapGestureRecognizer) {
    let tappedImageView = gestureRecognizer.view!

    if tappedImageView.alpha == 0.7 {
        // Success
    }
}
The Tom
  • 2,790
  • 6
  • 29
  • 33
  • 1
    Thanks! The if tappedImageView.alpha == 0.7 doesn't work though. I'm sure I can figure it out. –  Jul 08 '15 at 04:43