Skip to content

Instantly share code, notes, and snippets.

@TheCodedSelf
Created August 31, 2017 09:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheCodedSelf/b5b12d7c74722ec22facb6da606ea582 to your computer and use it in GitHub Desktop.
Save TheCodedSelf/b5b12d7c74722ec22facb6da606ea582 to your computer and use it in GitHub Desktop.
@IBAction private func startToastAnimation(_ sender: Any) {
let toastView = createToastView()
view.addSubview(toastView)
animate(toastView: toastView)
}
private func createToastView() -> UIView {
// 1.
let toastViewHeight = CGFloat(80)
let toastView = UIView(frame: CGRect(x: view.frame.origin.x,
y: -toastViewHeight,
width: view.frame.width,
height: toastViewHeight))
toastView.backgroundColor = .green
return toastView
}
private func animate(toastView: UIView) {
UIView.animate(withDuration: 1.0, animations: {
// 2.
toastView.transform = toastView.transform
.translatedBy(x: 0, y: toastView.frame.height)
}, completion: { _ in
// 3.
UIView.animate(withDuration: 1.0, delay: 1.0, animations: {
toastView.transform = .identity
}, completion: { _ in
// 4.
toastView.removeFromSuperview()
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment