Las Vegas Dice App

이지은·2021년 3월 27일
0

Roll 버튼을 누르면 두 개의 주사위에 1에서 6까지 랜덤하게 숫자가 각각 보여지는 간단한 기능이다.

Main.storyboard: display
ViewController.swift: code
View -> Inspector -> Attribute


click dice + control 끌어서 원하는 줄에 link!

이런식으로 Interface Builder outlet code 생성
Connection: Outlet, Name은 각각 diceImageView1 & diceImageView2 로 설정해준다.

    @IBOutlet weak var diceImageView1: UIImageView!
    @IBOutlet weak var diceImageView2: UIImageView!

click right하면 아래처럼 Referencing Outlets에서 link 확인이 가능하고 끊는것도 가능하다.

다음으로 roll 버튼에도 똑같이 해준다.

버튼은 Connection: Action, Name: rollButtonPressed, Touch up inside = tab 으로 설정해준다.
No longer IBOutlet but IBAction (triggered when the action occurs on this particular UI element.).

버튼을 눌렀을 때 각각의 diceImageView1 & 2가 미리 저장해둔 Assets.xcassets folder 안에 6가지 이미지 중 한가지로 random 하게 보여지게 만들어야 한다.

 * Use (Who.What = Value) format 
 // Who needs to change what property. Value is what it should be changed to!

① allDice라는 변수 선언해주고 dice들의 image Literal 6가지를 array[] 안에 담아준다.

Swift에서 변수를 선언해줄 때 var과 let을 사용할 수 있다. var을 사용하면 data 변경이 가능하지만 let은 불가하다.

When you declare a constant, the data you add to it cannot change.
Constants are declared using a let instead of a var.

allDice는 변경되지 않는 data를 담고 있기 때문에 var을 사용하면 Variable 'allDice' was never mutated; consider changing to 'let' constant 라는 위와 같은 경고가 뜬다. let으로 바꿔 주면 해결완료.

② 이제 Who.What = value format을 참고해서 기존의 dice가 버튼을 눌렀을 때 랜덤으로 바뀌게 만든다.

 @IBAction func rollButtonPressed(_ sender: UIButton) {
       
       let allDice = [#imageLiteral(resourceName: "DiceOne"), #imageLiteral(resourceName: "DiceTwo"), 
       #imageLiteral(resourceName: "DiceThree"), #imageLiteral(resourceName: "DiceFour"),
       #imageLiteral(resourceName: "DiceFive"), #imageLiteral(resourceName: "DiceSix")]
       
       //Who          What    Value
       diceImageView1.image = allDice[Int.random(in: 0...5)]
       diceImageView2.image = allDice[Int.random(in: 0...5)]   
   }   
}

기초부터 차근차근!

//Swift! -Basics

<Variables/Constants>
var myVariable = 4
let myConstant = 10

var typedVariable: Type
let typedConstant: Type

<Functions>
func aFunction(input: Type) -> Type {
//Do some
return theOutput
}

<Data Types>
Int 23
Float 2.3
Double 3.1415926
Bool true/false
String "abcd"
Array [1,2,3,4]
Dictionary [key, value]

<If Statement>
<Switch Statement>
<Loops>

3 ways of embeding view
1.

  1. click '+' -> create image view -> drag logo file inside image view file

stack views

profile
Front-end 🐕🦶

1개의 댓글

comment-user-thumbnail
2022년 11월 18일

I personally am very glad that I found how you can play craps online. I personally I'm very glad that I found a way to play craps online. This is important for me because I can definitely say that the best decision would be to check out crypto dice here, I really liked it all and now I try to play only here. It is also possible to win crypto, that's a special thing for me. I hope I could be of help to you in this matter, play only wisely.

답글 달기