Swift
Switch
khon98
2021. 2. 16. 20:43
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var BtnCenterX: NSLayoutConstraint!
@IBOutlet weak var switchBtn: UIButton!
@IBOutlet weak var switchBg: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// 원으로 만들고 싶으면 해당 크기의 반 만큼 입력하면 됨
switchBtn.layer.cornerRadius = 25
switchBg.layer.cornerRadius = 25
}
@IBAction func selectedBtn(_ sender: Any) {
// 버튼 이동
if BtnCenterX.constant == 75 {
BtnCenterX.constant = -75
// 버튼이 이동할때 배경 색 변경
switchBg.backgroundColor = UIColor.lightGray
} else {
BtnCenterX.constant = 75
switchBg.backgroundColor = UIColor.gray
}
UIView.animate(withDuration: 0.5) {
self.view.layoutIfNeeded()
}
}
}