简体   繁体   中英

Locking or holding an image with Swift

I am working on a small dice game made with Swift. I started with a udemy course and now I am missing a functionality that I really want to be implemented.

I got two dices and a button. If I press the button both dices will roll. Is there a way to lock a dice?

For example: I throw a '3' and a '1'. I tap on the '1' and press the button 'throw' now only the '3' will roll and the '1' stays '1'.

Anyone that can help me out with this? I hope my question and example is clear enough to understand.

Looking forward to your responses.

Here is the code:

    @IBOutlet weak var diceImage1: UIImageView!
    @IBOutlet weak var diceImage2: UIImageView!

    var randomDiceIndex1 : Int = 0
    var randomDiceIndex2 : Int = 0

    let diceArray = ["dice1", "dice2", "dice3", "dice4", "dice5", "dice6"]

    override func viewDidLoad() {
        super.viewDidLoad()

        updateDiceImages()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func buttonPressed(_ sender: Any) {

        updateDiceImages()

    }

    func updateDiceImages(){

        randomDiceIndex1 = Int(arc4random_uniform(6))
        randomDiceIndex2 = Int(arc4random_uniform(6))

        diceImage1.image = UIImage(named: diceArray[randomDiceIndex1])
        diceImage2.image = UIImage(named: diceArray[randomDiceIndex2])

    }

    override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
        updateDiceImages()
    }
}

Make your dices UIButtons instead of UIImageView (you can assign images for Buttons just like Images but additionally they will be tappable). Create 2 booleans and name them like 'lockDice1' and 'lockDice2' Make @IBAction's for both buttons and name them something like "switchDice1Lock" and "switchDice2Lock". Inside of this functions you can switch diceLocks like

lockDice1 = !lockDice1

and

lockDice2 = !lockDice2

accordingly.

then change

    randomDiceIndex1 = Int(arc4random_uniform(6))
    randomDiceIndex2 = Int(arc4random_uniform(6))

to

    randomDiceIndex1 = lockDice1 ? randomDiceIndex1 : Int(arc4random_uniform(6))
    randomDiceIndex2 = lockDice2 ? randomDiceIndex2 : Int(arc4random_uniform(6))

and it should work then.

Here's the solution to your problem:

Add tap gesture to your die images and change their highlighted state, If the state is highlighted don't change that die else change the images.

Adding a border to the imageView Confirms that die is locked.

class ViewController: UIViewController, UIGestureRecognizerDelegate {

  @IBOutlet weak var diceImage1: UIImageView!
  @IBOutlet weak var diceImage2: UIImageView!

  var randomDiceIndex1 : Int = 0
  var randomDiceIndex2 : Int = 0


  let diceArray = ["dice1", "dice2", "dice3", "dice4", "dice5", "dice6"]

  override func viewDidLoad() {
    super.viewDidLoad()

    updateDiceImages()

    let die1tap = UITapGestureRecognizer(target: self, action: #selector(dice1Tapped))
    let die2tap = UITapGestureRecognizer(target: self, action: #selector(dice2Tapped))
    diceImage1.addGestureRecognizer(die1tap)
    diceImage2.addGestureRecognizer(die2tap)

  }

  @IBAction func buttonPressed(_ sender: Any) {

    updateDiceImages()

  }

  @objc func dice1Tapped() {

    diceImage1.isHighlighted = !diceImage1.isHighlighted
    if !diceImage1.isHighlighted {
        diceImage1.layer.borderWidth = 0
    }else {
        diceImage1.layer.borderWidth = 2
        diceImage1.layer.borderColor = UIColor.red.cgColor
    }
  }

  @objc func dice2Tapped() {
    diceImage2.isHighlighted = !diceImage2.isHighlighted
    if !diceImage2.isHighlighted {
        diceImage2.layer.borderWidth = 0
    }else {
        diceImage2.layer.borderWidth = 2
        diceImage2.layer.borderColor = UIColor.red.cgColor
    }
  }


  func updateDiceImages(){

    if !diceImage1.isHighlighted {
        randomDiceIndex1 = Int(arc4random_uniform(6))
        diceImage1.image = UIImage(named: diceArray[randomDiceIndex1])
    }
    if !diceImage2.isHighlighted {
        randomDiceIndex2 = Int(arc4random_uniform(6))


        diceImage2.image = UIImage(named: diceArray[randomDiceIndex2])
    }


  }

  override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
    updateDiceImages()
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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