Desarrollo de iOS Swift-3-UI y aplicación Button Button-Shake the Dice

1. Crea un nuevo proyecto Dice

 2. Icono

 Elimine AppIcon y arrastre el archivo AppIcon.appiconset descomprimido al paquete Assets.

 3. Convierta los puntos de material 1 a 6 en versiones 2x y 3x a través de la página web y arrástrelos al Activo.

 4. Configure la interfaz de usuario correspondiente.

 5. Arrastre el componente Botón y establezca el estilo.

 6. Presione Ctrl y arrastre para arrastrar el Botón a ViewController, configure Conexión, Nombre, etc., y haga clic en Conectar.

 Crea el código para dos dados de la misma forma.

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var diceImageView1: UIImageView!
    
    @IBOutlet weak var diceImageView2: UIImageView!
    
    @IBAction func roll(_ sender: Any) {
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

 7. Continúe escribiendo código para crear números aleatorios.

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var diceImageView1: UIImageView!
    
    @IBOutlet weak var diceImageView2: UIImageView!
    
    @IBAction func roll(_ sender: Any) {
        var index1: Int = Int.random(in: 1...6)
        var index2: Int = Int.random(in: 1...6)
        diceImageView1.image = UIImage(named: "dice\(index1)")
        diceImageView2.image = UIImage(named: "dice\(index2)")    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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

}

8. Iniciar y ejecutar

9. Lanza dados aleatoriamente cuando se carga el programa.

import UIKit

class ViewController: UIViewController {
    
    let diceArr = ["dice1", "dice2", "dice3", "dice4", "dice5", "dice6"]

    @IBOutlet weak var diceImageView1: UIImageView!
    
    @IBOutlet weak var diceImageView2: UIImageView!
    
    @IBAction func roll(_ sender: Any) {
        //调用摇骰子函数
        updateDiceImages()
    }
    
    //当程序加载的时候的活动
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        //调用摇骰子函数
        updateDiceImages()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func updateDiceImages(){
        var index1: Int = Int.random(in: 0...5)
        var index2: Int = Int.random(in: 0...5)
        
        diceImageView1.image = UIImage(named: diceArr[index1])
        diceImageView2.image = UIImage(named: diceArr[index2])    }
    
}

10. Llame a la función de agitar los dados cuando el usuario agite los dados.

import UIKit

class ViewController: UIViewController {
    
    let diceArr = ["dice1", "dice2", "dice3", "dice4", "dice5", "dice6"]

    @IBOutlet weak var diceImageView1: UIImageView!
    
    @IBOutlet weak var diceImageView2: UIImageView!
    
    @IBAction func roll(_ sender: Any) {
        //调用摇骰子函数
        updateDiceImages()
    }
    
    //当程序加载的时候的活动
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        //调用摇骰子函数
        updateDiceImages()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func updateDiceImages(){
        var index1: Int = Int.random(in: 0...5)
        var index2: Int = Int.random(in: 0...5)
        
        diceImageView1.image = UIImage(named: diceArr[index1])
        diceImageView2.image = UIImage(named: diceArr[index2])    }
    
    //当用户摇晃手机时触发摇骰子
    override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
        updateDiceImages()
    }
    
}

Supongo que te gusta

Origin blog.csdn.net/LYly_B/article/details/132553143
Recomendado
Clasificación