Guest User

Untitled

a guest
Feb 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. //
  2. // NotificationBanner.swift
  3. // TopNotificationRecipe
  4. //
  5. // Created by Dushyant Bansal on 11/02/18.
  6. // Copyright © 2018 db42.in. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10. import UIKit
  11.  
  12. class NotificationBanner {
  13. static let labelLeftMarging = CGFloat(16)
  14. static let labelTopMargin = CGFloat(24)
  15. static let animateDuration = 0.5
  16. static let bannerAppearanceDuration: TimeInterval = 2
  17.  
  18. static func show(_ text: String) {
  19. let superView = UIApplication.shared.keyWindow!.rootViewController!.view!
  20.  
  21. let height = CGFloat(64)
  22. let width = superView.bounds.size.width
  23.  
  24. let bannerView = UIView(frame: CGRect(x: 0, y: 0-height, width: width, height: height))
  25. bannerView.layer.opacity = 1
  26. bannerView.backgroundColor = UIColor.red
  27. bannerView.translatesAutoresizingMaskIntoConstraints = false
  28.  
  29. let label = UILabel(frame: CGRect.zero)
  30. label.textColor = UIColor.white
  31. label.numberOfLines = 0
  32. label.text = text
  33. label.translatesAutoresizingMaskIntoConstraints = false
  34.  
  35. bannerView.addSubview(label)
  36. superView.addSubview(bannerView)
  37.  
  38. let labelCenterYContstraint = NSLayoutConstraint(item: label, attribute: .centerY, relatedBy: .equal, toItem: bannerView, attribute: .centerY, multiplier: 1, constant: 0)
  39. let labelCenterXConstraint = NSLayoutConstraint(item: label, attribute: .centerX, relatedBy: .equal, toItem: bannerView, attribute: .centerX, multiplier: 1, constant: 0)
  40. let labelWidthConstraint = NSLayoutConstraint(item: label, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: width - labelLeftMarging*2)
  41. let labelTopConstraint = NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: bannerView, attribute: .top, multiplier: 1, constant: labelTopMargin)
  42.  
  43. let bannerWidthConstraint = NSLayoutConstraint(item: bannerView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: width)
  44. let bannerCenterXConstraint = NSLayoutConstraint(item: bannerView, attribute: .leading, relatedBy: .equal, toItem: superView, attribute: .leading, multiplier: 1, constant: 0)
  45. let bannerTopConstraint = NSLayoutConstraint(item: bannerView, attribute: .top, relatedBy: .equal, toItem: superView, attribute: .top, multiplier: 1, constant: 0-height)
  46.  
  47. NSLayoutConstraint.activate([labelCenterYContstraint, labelCenterXConstraint, labelWidthConstraint, labelTopConstraint, bannerWidthConstraint, bannerCenterXConstraint, bannerTopConstraint])
  48.  
  49. UIView.animate(withDuration: animateDuration) {
  50. bannerTopConstraint.constant = 0
  51. superView.layoutIfNeeded()
  52. }
  53.  
  54. //remove subview after time 2 sec
  55. UIView.animate(withDuration: animateDuration, delay: bannerAppearanceDuration, options: [], animations: {
  56. bannerTopConstraint.constant = 0 - bannerView.frame.height
  57. superView.layoutIfNeeded()
  58. }, completion: { finished in
  59. if finished {
  60. bannerView.removeFromSuperview()
  61. }
  62. })
  63. }
  64. }
Add Comment
Please, Sign In to add comment