A control that offers a binary choice, such as on/off. _ Apple Developer Documentation
on/off와 같은 2개의 반대되는 성질(Boolean)을 선택할 수 있도록 제공되는 버튼
switch 버튼은 아래 사진과 같이 object library(cmd + shift + L)에서 꺼내 쓰면 된다.
위 사진과 같은 형태의 스위치를 토글(toggle)이라고도 한다.
on / off 상태 설정을 하려면,
isOn _ Apple Developer Documentation
Instance Property
A Boolean value that determines whether the switch is in the on or off position.
This property allows you to retrieve and set (without animation) a value determining whether the UISwitch object is on or off.
-> 스위치의 현재 상태를 읽음
Declaration
var isOn: Bool { get set }
setOn(_:animated:) _ Apple Developer Documentation
Instance Method
Sets the state of the switch to the on or off position, optionally animating the transition.
Setting the switch to either position doesn’t result in an action message being sent.
-> true/false 사용하여 스위치 on/off 설정
Declaration
func setOn(
_ on: Bool,
animated: Bool
)
Parameters
on
true if the switch should be turned to the on position; false if it should be turned to the off position. If the switch is already in the designated position, nothing happens.
만약 스위치가 on 포지션이 되길 원하면 true, off 포지션일 때 false.
animated
true to animate the “flipping” of the switch; otherwise false.
true가 되면 스위치가 활성화 됨
switch 구현
18번째 줄 : setOn을 사용해 on 버튼이 true 상태이며 on 상태부터 스위치가 animated 되도록 설정 -> On 상태면 Hi, Off 상태면 Bye가 화면에 나옴
만약 switchbutton.setOn(false, animated: true)로 설정하면 on 버튼이 false 상태 -> on을 누르면 Bye가 화면에 나옴
22번째 줄: switch 버튼이 On 상태면 Hi, Off 상태면 Bye가 화면에 나옴
만약 switchbutton.isOn = true이면 off 버튼이 눌리지 않음
반대로 switchbutton.isOn = false이면 on 버튼으로 넘어가지지 않음
'FE 공부 > Swift' 카테고리의 다른 글
Swift _ Activity Indicator View (0) | 2022.11.15 |
---|---|
Swift _ Stepper (0) | 2022.11.15 |
Swift _ Generics (0) | 2022.10.11 |
Swift _ Protocols (0) | 2022.10.11 |
Swift _ Extensions (0) | 2022.10.11 |
댓글