MOBILE DICE APP COMPLETE CODE
(XCODE)
SWIFT
import UIKit //1
class ViewController: UIViewController { //2
let diceArray = ["dice1", "dice2", "dice3", "dice4", "dice5", "dice6" ] //3
@IBOutlet weak var diceImageView1: UIImageView!
@IBOutlet weak var diceImageView2: UIImageView! //4
var randomDiceIndex1 : Int = 0
var randomDiceIndex2 : Int = 0 //5
override func viewDidLoad() {
super.viewDidLoad()
updateDiceImages() //6
}
@IBAction func rollButtonPressed(_ sender: UIButton) { //7
updateDiceImages()
}
override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
updateDiceImages()
}
func updateDiceImages() { //8
randomDiceIndex1 = Int.random(in: 0 ... 5) //9
randomDiceIndex2 = Int.random(in: 0 ... 5)
diceImageView1.image = UIImage(named: diceArray[randomDiceIndex1])
diceImageView2.image = UIImage(named: diceArray[randomDiceIndex2]) //10
}
}
*CODEEND
The below shown is the image of The viewcontroller of this following app, simulated in iPhone XS.
*CODEEND
The below shown is the image of The viewcontroller of this following app, simulated in iPhone XS.
CODE BREAKDOWN
//1 -> UIKIT being imported from the Xcode library for the essentials in the code.
//2 -> A New class called CLASS VIEWCONTROLER is created which is a super class of UIViewcontroller.
NOTE - Any code in the format of [ Class superclass : subclass {.....} ] refers to a
super class which has a subclass nested in it.
//3 -> An array is declared using "LET" key word, the array contains all the dice names.
//4 -> IBOutlets being linked to the viecontroller where , the first outlet is linked to the first image (dice1), the second is linked to the second image (dice2)
//5 -> Two variables is created named randomDIceIndex1 and randomDIceIndex2 which are initially set to a value '0'
//6 -> UpdateDiceImage is a function which is written to update random dice images when roll button is pressed , that function is being called here
//7 -> IBAction function in which triggers after roll button is pressed hence the updateDiceImages function is being called inside
//8 & //9 -> The function updateDiceImages which is used a couple of times is written, where randomDiceIndex1 and randomDiceIndex2 which are set to a default value of '0' are allowed here to come up with any random number between 0-5 using " Int.random(in: 0 ... 5) " key word. The array which is mentioned above which has all the dice are being randomly called here
**there are 6 elements in the array, where as we are calling until 5 here**
this is because the array is counted from '0' , which is 0 to 1 to 2 to 3... to 5.
//10 -> "diceImageView1.image = UIImage(named: diceArray[randomDiceIndex1])" This enables the dice images to change randomly with the number in the above line.
------------------------------------------------------------------------------------------
CODE wriiten by - yoshithRoy
project idea - London App Brewery
NOTE - Any code in the format of [ Class superclass : subclass {.....} ] refers to a
super class which has a subclass nested in it.
//3 -> An array is declared using "LET" key word, the array contains all the dice names.
//4 -> IBOutlets being linked to the viecontroller where , the first outlet is linked to the first image (dice1), the second is linked to the second image (dice2)
//5 -> Two variables is created named randomDIceIndex1 and randomDIceIndex2 which are initially set to a value '0'
//6 -> UpdateDiceImage is a function which is written to update random dice images when roll button is pressed , that function is being called here
//7 -> IBAction function in which triggers after roll button is pressed hence the updateDiceImages function is being called inside
//8 & //9 -> The function updateDiceImages which is used a couple of times is written, where randomDiceIndex1 and randomDiceIndex2 which are set to a default value of '0' are allowed here to come up with any random number between 0-5 using " Int.random(in: 0 ... 5) " key word. The array which is mentioned above which has all the dice are being randomly called here
**there are 6 elements in the array, where as we are calling until 5 here**
this is because the array is counted from '0' , which is 0 to 1 to 2 to 3... to 5.
//10 -> "diceImageView1.image = UIImage(named: diceArray[randomDiceIndex1])" This enables the dice images to change randomly with the number in the above line.
------------------------------------------------------------------------------------------
CODE wriiten by - yoshithRoy
project idea - London App Brewery