Don_Mag

Simple timed notification view

Jun 15th, 2020
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.02 KB | None | 0 0
  1. class MyAlertView: UIView {
  2.    
  3.     let titleLabel: UILabel = {
  4.         let v = UILabel()
  5.         v.translatesAutoresizingMaskIntoConstraints = false
  6.         v.textAlignment = .center
  7.         v.text = "My Alert View"
  8.         v.font = UIFont.systemFont(ofSize: 20.0, weight: .bold)
  9.         v.textColor = .red
  10.         return v
  11.     }()
  12.     let messageLabel: UILabel = {
  13.         let v = UILabel()
  14.         v.translatesAutoresizingMaskIntoConstraints = false
  15.         v.textAlignment = .center
  16.         v.numberOfLines = 0
  17.         v.font = UIFont.systemFont(ofSize: 15.0, weight: .regular)
  18.         v.textColor = .blue
  19.         return v
  20.     }()
  21.  
  22.     override init(frame: CGRect) {
  23.         super.init(frame: frame)
  24.         commonInit()
  25.     }
  26.     required init?(coder: NSCoder) {
  27.         super.init(coder: coder)
  28.         commonInit()
  29.     }
  30.     func commonInit() -> Void {
  31.         backgroundColor = UIColor.black.withAlphaComponent(0.25)
  32.         layer.cornerRadius = 16
  33.         layer.borderColor = UIColor.black.cgColor
  34.         layer.borderWidth = 2
  35.         addSubview(titleLabel)
  36.         addSubview(messageLabel)
  37.         NSLayoutConstraint.activate([
  38.             // constrain title label to width of self, 16-pts from the top
  39.             titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: 16.0),
  40.             titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor),
  41.             titleLabel.trailingAnchor.constraint(equalTo: trailingAnchor),
  42.            
  43.             // constrain Top of message label to Bottom of title label + 10-pts "spacing"
  44.             messageLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 10.0),
  45.             // 16-pts leading and trailing
  46.             messageLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16.0),
  47.             messageLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16.0),
  48.             // bottom to self bottom + 16-pts "spacing"
  49.             messageLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -16.0),
  50.         ])
  51.     }
  52. }
  53.  
  54. class ShowMyAlertViewController: UIViewController {
  55.  
  56.     override func viewDidLoad() {
  57.         super.viewDidLoad()
  58.         view.backgroundColor = .white
  59.     }
  60.     override func viewDidAppear(_ animated: Bool) {
  61.         super.viewDidAppear(animated)
  62.         // 2 seconds after view appears, show the alert
  63.         DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
  64.             self.showMyAlert("This is a test of showing a custom \"Alert / Notification\" view, which will dismiss itself after 5 seconds.")
  65.         }
  66.     }
  67.     func showMyAlert(_ msg: String) -> Void {
  68.         // instantiate MyAlertView
  69.         let v = MyAlertView()
  70.         // set the label's text
  71.         v.messageLabel.text = msg
  72.         v.translatesAutoresizingMaskIntoConstraints = false
  73.         // add it to self's view
  74.         view.addSubview(v)
  75.         // hidden constraint will place the BOTTOM of the view 10-pts above the top of self's view
  76.         let hiddenConstraint = v.bottomAnchor.constraint(equalTo: view.topAnchor, constant: -10.0)
  77.         // visible constraint will place the TOP of the view 10-pts below the top of self's view (respecting safe area)
  78.         let visibleConstraint = v.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10.0)
  79.         NSLayoutConstraint.activate([
  80.             // center the alert view horizontally
  81.             v.centerXAnchor.constraint(equalTo: view.centerXAnchor),
  82.             // make its width 80% of the view
  83.             v.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.8),
  84.             // height will be determined by the content of the view
  85.            
  86.             // start with view above self's view frame ("hidden")
  87.             hiddenConstraint,
  88.         ])
  89.         // allow the alert view to start at its hidden position
  90.         DispatchQueue.main.async {
  91.             // swap active constraints
  92.             hiddenConstraint.isActive = false
  93.             visibleConstraint.isActive = true
  94.             // animate it into view
  95.             UIView.animate(withDuration: 0.3, animations: {
  96.                 self.view.layoutIfNeeded()
  97.             }, completion: { done in
  98.                 // wait 5 seconds
  99.                 DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
  100.                     // swap active constraints
  101.                     visibleConstraint.isActive = false
  102.                     hiddenConstraint.isActive = true
  103.                     // animate it out of view
  104.                     UIView.animate(withDuration: 0.3, animations: {
  105.                         self.view.layoutIfNeeded()
  106.                     }, completion: { done in
  107.                         // remove it
  108.                         v.removeFromSuperview()
  109.                     })
  110.                 }
  111.             })
  112.         }
  113.     }
  114.    
  115. }
Add Comment
Please, Sign In to add comment