Advertisement
Don_Mag

Untitled

Apr 17th, 2023
1,105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.91 KB | None | 0 0
  1. //
  2.  
  3. class Toast: UIView {
  4.    
  5. }
  6. class ToastTestVC: UIViewController {
  7.    
  8.     override func viewDidLoad() {
  9.         super.viewDidLoad()
  10.         view.backgroundColor = .systemYellow
  11.     }
  12.    
  13.     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  14.         showToast("")
  15.     }
  16.    
  17.     var y: CGFloat = 20.0
  18.     func computeToastFrame() -> CGRect {
  19.         // increment y every time this is called
  20.         //  until we get near the bottom, then start
  21.         //  back at the top
  22.         y += 60.0
  23.         if y > view.frame.height - 100.0 {
  24.             y = 80.0
  25.         }
  26.         return .init(x: 100.0, y: y, width: 200.0, height: 50.0)
  27.     }
  28.  
  29.     func showToast(_ message: String) {
  30.         let frame = computeToastFrame()
  31.         let toast = Toast(frame: frame)
  32.         toast.backgroundColor = .red
  33.         view.addSubview(toast)
  34.         UIView.animate(withDuration: 1, delay: 1, options: .allowUserInteraction) {
  35.             toast.alpha = 0.02
  36.         } completion: { _ in
  37.             toast.removeFromSuperview()
  38.         }
  39.     }
  40.  
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement