Skip to content

Instantly share code, notes, and snippets.

@krummler
Last active February 5, 2020 20:54
Show Gist options
  • Save krummler/d0e8db8cb037ae7202f7d801d3114111 to your computer and use it in GitHub Desktop.
Save krummler/d0e8db8cb037ae7202f7d801d3114111 to your computer and use it in GitHub Desktop.
//: A UIKit based Playground for presenting user interface
import PlaygroundSupport
import UIKit
class MyViewController: UIViewController {
let button1 = UIButton()
let button2 = UIButton()
let button3 = UIButton()
let button4 = UIButton()
let view1 = UIView()
let view2 = UIView()
let view3 = UIView()
let view4 = UIView()
override func loadView() {
let view = UIView()
view.backgroundColor = .gray
button1.addTarget(self, action: #selector(show1), for: .touchUpInside)
button1.translatesAutoresizingMaskIntoConstraints = false
button1.setTitle("show view 1", for: .normal)
button2.addTarget(self, action: #selector(show2), for: .touchUpInside)
button2.translatesAutoresizingMaskIntoConstraints = false
button2.setTitle("show view2", for: .normal)
button3.addTarget(self, action: #selector(show3), for: .touchUpInside)
button3.translatesAutoresizingMaskIntoConstraints = false
button3.setTitle("show view3", for: .normal)
button4.addTarget(self, action: #selector(show4), for: .touchUpInside)
button4.translatesAutoresizingMaskIntoConstraints = false
button4.setTitle("show view4", for: .normal)
view.addSubview(button1)
view.addSubview(button2)
view.addSubview(button3)
view.addSubview(button4)
let stack = UIStackView()
stack.axis = .vertical
view.addSubview(stack)
stack.translatesAutoresizingMaskIntoConstraints = false
view1.backgroundColor = .red
view1.translatesAutoresizingMaskIntoConstraints = false
view2.backgroundColor = .green
view2.translatesAutoresizingMaskIntoConstraints = false
view2.alpha = 0
view2.isHidden = true
view3.backgroundColor = .blue
view3.translatesAutoresizingMaskIntoConstraints = false
view3.alpha = 0
view3.isHidden = true
view4.backgroundColor = .yellow
view4.translatesAutoresizingMaskIntoConstraints = false
view4.alpha = 0
view4.isHidden = true
let constraints = [
button1.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
button1.centerXAnchor.constraint(equalTo: view.centerXAnchor),
button2.topAnchor.constraint(equalTo: button1.bottomAnchor),
button2.centerXAnchor.constraint(equalTo: view.centerXAnchor),
button3.topAnchor.constraint(equalTo: button2.bottomAnchor),
button3.centerXAnchor.constraint(equalTo: view.centerXAnchor),
button4.topAnchor.constraint(equalTo: button3.bottomAnchor),
button4.centerXAnchor.constraint(equalTo: view.centerXAnchor),
view1.heightAnchor.constraint(equalToConstant: 200),
view1.widthAnchor.constraint(equalToConstant: 200),
view2.heightAnchor.constraint(equalToConstant: 100),
view3.heightAnchor.constraint(equalToConstant: 250),
view4.heightAnchor.constraint(equalToConstant: 150),
stack.widthAnchor.constraint(equalToConstant: 250),
stack.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
stack.centerXAnchor.constraint(equalTo: view.centerXAnchor),
]
NSLayoutConstraint.activate(constraints)
stack.addArrangedSubview(view1)
stack.addArrangedSubview(view2)
stack.addArrangedSubview(view3)
stack.addArrangedSubview(view4)
self.view = view
}
@objc func show1() {
animate {
self.resetSubviews(to: self.view1)
}
}
@objc func show2() {
animate {
self.resetSubviews(to: self.view2)
}
}
@objc func show3() {
animate {
self.resetSubviews(to: self.view3)
}
}
@objc func show4() {
animate {
self.resetSubviews(to: self.view4)
}
}
private func resetSubviews(to view: UIView) {
view1.alpha = view == view1 ? 1 : 0
view2.alpha = view == view2 ? 1 : 0
view3.alpha = view == view3 ? 1 : 0
view4.alpha = view == view4 ? 1 : 0
view1.isHidden = view != view1
view2.isHidden = view != view2
view3.isHidden = view != view3
view4.isHidden = view != view4
}
func animate(_ actions: @escaping () -> Void) {
UIView.animate(withDuration: 0.2, animations: {
actions()
})
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment