deactivate
- constraints 설정을 다 비활성화 시킴
activate
- deactivate와 반대
비활성화와 활성화를 시키지 않으면 값이 중첩되기 때문에 오류 발생
import UIKit
// multiplier 값 변경하기 위한 조건
extension NSLayoutConstraint {
func changeMultiplier(value: CGFloat) -> NSLayoutConstraint {
// 처음에 있는 constraint 값 비활성화
NSLayoutConstraint.deactivate([self])
// constraints 새로 만들기
let newConstraints = NSLayoutConstraint.init(item: self.firstItem, attribute: self.firstAttribute, relatedBy: self.relation, toItem: self.secondItem, attribute: self.secondAttribute, multiplier: value, constant: self.constant)
newConstraints.priority = self.priority
newConstraints.shouldBeArchived = self.shouldBeArchived
newConstraints.identifier = self.identifier
// 새로 만든 constraint로 활성화
NSLayoutConstraint.activate([newConstraints])
// 활성화된 constraint return
return newConstraints
}
}
class ViewController: UIViewController {
@IBOutlet weak var graph1Height: NSLayoutConstraint!
@IBOutlet weak var graph2Height: NSLayoutConstraint!
@IBOutlet weak var graph3Height: NSLayoutConstraint!
@IBOutlet weak var graph4Height: NSLayoutConstraint!
@IBOutlet weak var graph5Height: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func style1(_ sender: Any) {
// animation 추가
UIView.animate(withDuration: 0.5) {
// 클로저 안에는 self가 있어야 함
self.graph1Height = self.graph1Height.changeMultiplier(value: 0.5)
self.graph2Height = self.graph2Height.changeMultiplier(value: 0.8)
self.graph3Height = self.graph3Height.changeMultiplier(value: 0.1)
self.graph4Height = self.graph4Height.changeMultiplier(value: 0.9)
self.graph5Height = self.graph5Height.changeMultiplier(value: 0.2)
// 화면 갱신
self.view.layoutIfNeeded()
}
}
@IBAction func style2(_ sender: Any) {
graph1Height = graph1Height.changeMultiplier(value: 0.1)
graph2Height = graph2Height.changeMultiplier(value: 0.6)
graph3Height = graph3Height.changeMultiplier(value: 0.8)
graph4Height = graph4Height.changeMultiplier(value: 0.3)
graph5Height = graph5Height.changeMultiplier(value: 0.5)
UIView.animate(withDuration: 0.5) {
self.view.layoutIfNeeded()
}
}
}
'Swift' 카테고리의 다른 글
Switch (0) | 2021.02.16 |
---|---|
커스텀 레이아웃을 스토리 보드와 연동 (0) | 2021.02.15 |
Floating Button (0) | 2021.02.13 |
table view (0) | 2021.02.13 |
보안 키보드 만들기 (0) | 2021.02.13 |