Advertisement
srk72

Success PopUp

Jun 27th, 2022 (edited)
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.73 KB | None | 0 0
  1.     func showSuccessView(message: String = "Request Sent Successfully!")
  2.     {
  3.         var size = CGSize()
  4.        
  5.         if UIDevice.current.userInterfaceIdiom == .pad
  6.         {
  7.             size = CGSize(width: self.view.frame.width / 3, height: self.view.frame.height / 6)
  8.         }
  9.         else
  10.         {
  11.             size = CGSize(width: self.view.frame.width * 0.8, height: 205)
  12.         }
  13.        
  14.         let successView = UIView(frame: CGRect.zero)
  15.         successView.backgroundColor = .white
  16.         successView.translatesAutoresizingMaskIntoConstraints = false
  17.         successView.layer.cornerRadius = 21
  18.         successView.layer.zPosition = 7
  19.         successView.transform = CGAffineTransform(scaleX: 0, y: 0)
  20.        
  21.         let background = UIView(frame: .zero)
  22.         background.backgroundColor = UIColor(white: 0.1, alpha: 0.37)
  23.         background.layer.zPosition = 4
  24.         background.isUserInteractionEnabled = false
  25.         background.translatesAutoresizingMaskIntoConstraints = false
  26.        
  27.         let imageView = UIImageView(frame: CGRect.zero)
  28.         imageView.image = UIImage(systemName: "checkmark.circle", withConfiguration: UIImage.SymbolConfiguration(weight: .thin))
  29.         imageView.tintColor = .systemGreen
  30.         imageView.translatesAutoresizingMaskIntoConstraints = false
  31.        
  32.         let label = UILabel(frame: CGRect.zero)
  33.         label.text = message
  34.         label.translatesAutoresizingMaskIntoConstraints = false
  35.         label.textAlignment = .center
  36.        
  37.         let action = UIAction { _ in
  38.             // Call hide animation here
  39.             UIView.animateKeyframes(withDuration: 0.7, delay: 0, options: .layoutSubviews)
  40.             {
  41.                 UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1/3)
  42.                 {
  43.                     successView.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
  44.                 }
  45.                
  46.                 UIView.addKeyframe(withRelativeStartTime: 1/1.3, relativeDuration: 1/1.3)
  47.                 {
  48.                     successView.transform = CGAffineTransform(scaleX: 0, y: 0)
  49.                 }
  50.                
  51.             } completion:
  52.             { (animation) in
  53.                 successView.removeFromSuperview()
  54.                 background.removeFromSuperview()
  55.                 successView.transform = .identity
  56.             }
  57.         }
  58.        
  59.         let cancelButton = UIButton(frame: CGRect.zero)
  60.         cancelButton.setImage(UIImage(systemName: "xmark")?.applyingSymbolConfiguration(.init(pointSize: 22)), for: .normal)
  61.         cancelButton.tintColor = #colorLiteral(red: 0.8374180198, green: 0.8374378085, blue: 0.8374271393, alpha: 1)
  62.         cancelButton.addAction(action, for: .touchUpInside)
  63.         cancelButton.translatesAutoresizingMaskIntoConstraints = false
  64.        
  65.         self.view.addSubview(successView)
  66.         self.view.addSubview(background)
  67.        
  68.         successView.addSubview(imageView)
  69.         successView.addSubview(label)
  70.         successView.addSubview(cancelButton)
  71.        
  72.         successView.heightAnchor.constraint(equalToConstant: size.height).isActive = true
  73.         successView.widthAnchor.constraint(equalToConstant: size.width).isActive = true
  74.         successView.centerXAnchor.constraint(equalToSystemSpacingAfter: self.view.centerXAnchor, multiplier: 0).isActive = true
  75.         successView.centerYAnchor.constraint(equalToSystemSpacingBelow: self.view.centerYAnchor, multiplier: 0).isActive = true
  76.        
  77.         background.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
  78.         background.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
  79.         background.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
  80.         background.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
  81.        
  82.         cancelButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
  83.         cancelButton.widthAnchor.constraint(equalToConstant: 40).isActive = true
  84.         cancelButton.topAnchor.constraint(equalTo: successView.topAnchor, constant: 8).isActive = true
  85.         cancelButton.trailingAnchor.constraint(equalTo: successView.trailingAnchor, constant: -8).isActive = true
  86.        
  87.         label.centerXAnchor.constraint(equalTo: successView.centerXAnchor).isActive = true
  88.         label.topAnchor.constraint(equalTo: imageView.bottomAnchor, constant: 25).isActive = true
  89.         label.heightAnchor.constraint(equalToConstant: 28).isActive = true
  90.         label.leadingAnchor.constraint(lessThanOrEqualToSystemSpacingAfter: successView.leadingAnchor, multiplier: 5).isActive = true
  91.         label.trailingAnchor.constraint(lessThanOrEqualToSystemSpacingAfter: successView.trailingAnchor, multiplier: 5).isActive = true
  92.        
  93.         imageView.heightAnchor.constraint(equalToConstant: 80).isActive = true
  94.         imageView.widthAnchor.constraint(equalToConstant: 80).isActive = true
  95.         imageView.centerXAnchor.constraint(equalToSystemSpacingAfter: successView.centerXAnchor, multiplier: 0).isActive = true
  96.         imageView.topAnchor.constraint(equalTo: successView.topAnchor, constant: 25).isActive = true
  97.        
  98.         UIView.animateKeyframes(withDuration: 0.9, delay: 0, options: .layoutSubviews)
  99.         {
  100.             UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1/1.3)
  101.             {
  102.                 successView.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
  103.             }
  104.            
  105.             UIView.addKeyframe(withRelativeStartTime: 1/3, relativeDuration: 1/3)
  106.             {
  107.                 successView.transform = .identity
  108.             }
  109.            
  110.         }
  111.         completion: { (animation) in
  112.             print("Animation Done!")
  113.         }
  114.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement