Home > Mobile >  UIImageView Array iOS13
UIImageView Array iOS13

Time:06-15

I am trying with learning Swift and got stuck on an issue here.

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var diceImageView1: UIImageView!

@IBOutlet weak var diceImageView2: UIImageView!

var leftDiceNumber=1

override func viewDidLoad() {
    super.viewDidLoad()
    
    // Do any additional setup after loading the view.

    diceImageView1.image = UIImage(imageLiteralResourceName: "DiceSix")

    // change transparency with diceImageView1.alpha=0.7

        }

@IBAction func rollButtonPressed(_ sender: UIButton) {
    print("button pressed")
    diceImageView1 = [ UIImageView(imageLiteralResourceName: "DiceOne"),UIImageView(imageLiteralResourceName: "DiceTwo"),UIImageView(imageLiteralResourceName: "DiceThree"),UIImageView(imageLiteralResourceName: "DiceFour"),UIImageView(imageLiteralResourceName: "DiceFive"),UIImageView(imageLiteralResourceName: "DiceSix")],[leftDiceNumber]
    
    leftDiceNumber=leftDiceNumber 1
  
}

}

But all I get is the error messages on the IBAction:

1.Argument passed to call that takes no arguments

2.Cannot assign value of type '[UIImageView]' to type 'UIImageView'

3.Consecutive statements on a line must be separated by ';'

4.Expected expression

What's the difference between UIImageView and UIImage ? When they should be used?

Many Thanks in advance !

CodePudding user response:

You want to change the .image property to change the image.

To "cycle through" the dice, you could do this:

class DiceViewController: UIViewController {
    
    @IBOutlet weak var diceImageView1: UIImageView!
    @IBOutlet weak var diceImageView2: UIImageView!

    let diceNames: [String] = [
        "DiceOne", "DiceTwo", "DiceThree", "DiceFour", "DiceFive", "DiceSix"
    ]
    
    var leftDiceNumber = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        diceImageView1.image = UIImage(named: diceNames[leftDiceNumber % 6])
    }
    
    @IBAction func rollButtonPressed(_ sender: UIButton) {
        print("button pressed")

        // increment the index
        leftDiceNumber  = 1
        
        // udpate the image view
        diceImageView1.image = UIImage(named: diceNames[leftDiceNumber % 6])

    }
    
}

I'm guessing your goal is to "randomly roll" the dice, so take a look at this slightly different class:

class DiceViewController: UIViewController {
    
    @IBOutlet weak var diceImageView1: UIImageView!
    @IBOutlet weak var diceImageView2: UIImageView!
    
    let diceNames: [String] = [
        "DiceOne", "DiceTwo", "DiceThree", "DiceFour", "DiceFive", "DiceSix"
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // start with both dice at One
        diceImageView1.image = UIImage(named: diceNames[0])
        diceImageView2.image = UIImage(named: diceNames[0])

    }
    
    @IBAction func rollButtonPressed(_ sender: UIButton) {
        print("button pressed")
        
        // arrays are Zero-based, so get a random Int
        //  from 0 to 5
        //let l = Int.random(in: 0...5)
        //let r = Int.random(in: 0...5)
        //diceImageView1.image = UIImage(named: diceNames[l])
        //diceImageView2.image = UIImage(named: diceNames[r])

        // more "modern Swifty" method
        if let nm = diceNames.randomElement() {
            diceImageView1.image = UIImage(named: nm)
        }
        if let nm = diceNames.randomElement() {
            diceImageView2.image = UIImage(named: nm)
        }

    }
    
}
  • Related