简体   繁体   English

使用Swift锁定或保存图像

[英]Locking or holding an image with Swift

I am working on a small dice game made with Swift. 我正在用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'. 例如:我抛出“ 3”和“ 1”。 I tap on the '1' and press the button 'throw' now only the '3' will roll and the '1' stays '1'. 我点击“ 1”并按下“ throw”按钮,现在只有“ 3”滚动,而“ 1”保持为“ 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). 使您的骰子成为UIButtons而不是UIImageView(您可以为Buttons分配图像,就像Images一样,但是它们可以被轻敲)。 Create 2 booleans and name them like 'lockDice1' and 'lockDice2' Make @IBAction's for both buttons and name them something like "switchDice1Lock" and "switchDice2Lock". 创建2个布尔值,并将其命名为“ lockDice1”和“ lockDice2”。为两个按钮都设置@IBAction,并将其命名为“ switchDice1Lock”和“ switchDice2Lock”。 Inside of this functions you can switch diceLocks like 在此功能内部,您可以像这样切换diceLocks

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. 在imageView中添加边框确认模具被锁定。

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

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

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