Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- class Toast: UIView {
- }
- class ToastTestVC: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- view.backgroundColor = .systemYellow
- }
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
- showToast("")
- }
- var y: CGFloat = 20.0
- func computeToastFrame() -> CGRect {
- // increment y every time this is called
- // until we get near the bottom, then start
- // back at the top
- y += 60.0
- if y > view.frame.height - 100.0 {
- y = 80.0
- }
- return .init(x: 100.0, y: y, width: 200.0, height: 50.0)
- }
- func showToast(_ message: String) {
- let frame = computeToastFrame()
- let toast = Toast(frame: frame)
- toast.backgroundColor = .red
- view.addSubview(toast)
- UIView.animate(withDuration: 1, delay: 1, options: .allowUserInteraction) {
- toast.alpha = 0.02
- } completion: { _ in
- toast.removeFromSuperview()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement