Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@stevencurtis
Created May 19, 2020 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevencurtis/67bd05a495ad185813b177f67e603260 to your computer and use it in GitHub Desktop.
Save stevencurtis/67bd05a495ad185813b177f67e603260 to your computer and use it in GitHub Desktop.
animatefullcode
class ViewController: UIViewController {
let subView = MyView()
var coverView = UIView()
var moveCenter = UIView()
var constraintsView = UIView()
var combinationView = UIView()
var springView = UIView()
var coverTopConstraint: NSLayoutConstraint!
var coverBottomConstraint: NSLayoutConstraint!
override func loadView() {
subView.backgroundColor = .red
self.view = subView
setupCover()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
NSLayoutConstraint.deactivate([
self.coverBottomConstraint
])
let newBottomConstraint = self.coverView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -100)
NSLayoutConstraint.activate([
newBottomConstraint
])
UIView.animate(withDuration: 5.0, animations: {
self.coverView.alpha = 0.0
self.coverBottomConstraint = newBottomConstraint
self.view.layoutIfNeeded()
}) { _ in
self.coverView.removeFromSuperview()
self.setupMoveCenter()
self.animateCenter()
self.setupConstraintsViewAndAnimate()
self.combinationAnimation()
}
}
func animateSpringView() {
springView.backgroundColor = .gray
springView.frame = CGRect(x: 0, y: 500, width: 200, height: 200)
self.view.addSubview(springView)
UIView.animate(
withDuration: 0.5,
delay: 0.5,
usingSpringWithDamping: 0.7,
initialSpringVelocity: 0.8,
options: .curveEaseInOut,
animations: {
self.springView.center = self.view.center
}, completion: nil)
}
func combinationAnimation() {
combinationView.backgroundColor = .blue
combinationView.frame = CGRect(x: 200, y: 500, width: 200, height: 200)
self.view.addSubview(combinationView)
let translate = CGAffineTransform(translationX: 100, y: 100)
let rotate = CGAffineTransform(rotationAngle: 1600)
let scale = CGAffineTransform(scaleX: 0.5, y: 0.5)
UIView.animate(withDuration: 0.5) {
self.combinationView.transform = translate.concatenating(rotate).concatenating(scale)
}
}
func setupConstraintsViewAndAnimate() {
constraintsView.backgroundColor = .purple
self.view.addSubview(constraintsView)
constraintsView.translatesAutoresizingMaskIntoConstraints = false
var topConstraint = constraintsView.topAnchor.constraint(equalTo: self.view.topAnchor)
let widthConstraint = constraintsView.widthAnchor.constraint(equalToConstant: 200)
let heightConstraint = constraintsView.heightAnchor.constraint(equalToConstant: 200)
NSLayoutConstraint.activate([
topConstraint,
widthConstraint,
heightConstraint
])
NSLayoutConstraint.deactivate([
topConstraint
])
let newConstraint = constraintsView.topAnchor.constraint(
equalTo: self.view.topAnchor,
constant: 400)
NSLayoutConstraint.activate([
newConstraint
])
UIView.animate(withDuration: 0.5) {
topConstraint = newConstraint
self.view.layoutIfNeeded()
}
}
func animateCenter() {
UIView.animate(withDuration: 0.5, animations: {
self.moveCenter.center = self.view.center
})
}
func setupMoveCenter() {
moveCenter.backgroundColor = .green
moveCenter.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
self.view.addSubview(moveCenter)
}
func setupCover() {
coverView.backgroundColor = .blue
self.view.addSubview(coverView)
coverView.translatesAutoresizingMaskIntoConstraints = false
coverTopConstraint = coverView.topAnchor.constraint(equalTo: self.view.topAnchor)
coverBottomConstraint = coverView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
NSLayoutConstraint.activate([
coverBottomConstraint,
coverTopConstraint,
coverView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
coverView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
])
}
}
class MyView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment