Advertisement
srk72

Detail and image popup

Jun 27th, 2022 (edited)
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 9.67 KB | None | 0 0
  1. private func popUp(index: Int) {
  2.        
  3.         var size = CGSize()
  4.         let imageSize: CGFloat = 75
  5.        
  6.         if UIDevice.current.userInterfaceIdiom == .pad
  7.         {
  8.             size = CGSize(width: 330, height: 240)
  9.         }
  10.         else
  11.         {
  12.             size = CGSize(width: self.view.frame.width * 0.87, height: 225)
  13.         }
  14.        
  15.         let mainView = UIView(frame: .zero)
  16.         mainView.backgroundColor = .white
  17.         mainView.translatesAutoresizingMaskIntoConstraints = false
  18.         mainView.layer.cornerRadius = 8
  19.         mainView.isUserInteractionEnabled = true
  20.         mainView.clipsToBounds = true
  21.         mainView.layer.zPosition = 5
  22.         mainView.transform = CGAffineTransform(scaleX: 0, y: 0)
  23.        
  24.         guard !background.isDescendant(of: self.view) else {
  25.             return
  26.         }
  27.        
  28.         background.backgroundColor = UIColor(white: 0.1, alpha: 0.37)
  29.         background.layer.zPosition = 4
  30.         background.isUserInteractionEnabled = false
  31.         background.translatesAutoresizingMaskIntoConstraints = false
  32.        
  33.         let colorView = UIView(frame: .zero)
  34.         colorView.backgroundColor = .blueColor
  35.         colorView.translatesAutoresizingMaskIntoConstraints = false
  36.        
  37.         let image = UIImageView(frame: .zero)
  38.         image.contentMode = .scaleAspectFill
  39.         image.translatesAutoresizingMaskIntoConstraints = false
  40.        
  41.         if requests[index].image != nil && requests[index].image != "" {
  42.             image.loadImageUsingCache(withUrl: requests[index].image)
  43.         } else {
  44.             image.image = UIImage(named: "avatar")
  45.         }
  46.        
  47.         let nameLbl = UILabel(frame: .zero)
  48.         nameLbl.font = UIFont.systemFont(ofSize: 22, weight: .regular)
  49.         nameLbl.translatesAutoresizingMaskIntoConstraints = false
  50.         nameLbl.text = requests[index].username
  51.        
  52.         let subjLbl = UILabel(frame: .zero)
  53.         subjLbl.font = UIFont.systemFont(ofSize: 19)
  54.         subjLbl.textColor = .lightGray
  55.         subjLbl.translatesAutoresizingMaskIntoConstraints = false
  56.         subjLbl.text = requests[index].subject_name
  57.        
  58.         let stdLbl = UILabel(frame: .zero)
  59.         stdLbl.textColor = .lightGray
  60.         stdLbl.font = UIFont.systemFont(ofSize: 19)
  61.         stdLbl.translatesAutoresizingMaskIntoConstraints = false
  62.         stdLbl.text = "Std - \(requests[index].standard)"
  63.        
  64.         let addressLbl = UILabel(frame: .zero)
  65.         addressLbl.textColor = .lightGray
  66.         addressLbl.numberOfLines = 2
  67.         addressLbl.font = UIFont.systemFont(ofSize: 19)
  68.         addressLbl.translatesAutoresizingMaskIntoConstraints = false
  69.         addressLbl.text = requests[index].location ?? ""
  70.        
  71.         let acceptBut = UIButton(frame: .zero)
  72.         acceptBut.backgroundColor = #colorLiteral(red: 0.4500938654, green: 0.9813225865, blue: 0.4743030667, alpha: 1)
  73.         acceptBut.setTitleColor(#colorLiteral(red: 0.2106181933, green: 0.4906361527, blue: 0.5, alpha: 1), for: .normal)
  74.         acceptBut.setTitle("Accept", for: .normal)
  75.         acceptBut.titleLabel?.font = .systemFont(ofSize: 17, weight: .semibold)
  76.         acceptBut.layer.cornerRadius = 6
  77.         acceptBut.translatesAutoresizingMaskIntoConstraints = false
  78.         acceptBut.addAction(UIAction(handler: { (act) in
  79.             hideView()
  80.             let but = UIButton()
  81.             but.tag = index
  82.             self.acceptAction(sender: but)
  83.         }) , for: .touchUpInside)
  84.        
  85.         let crossBut = UIButton(frame: .zero)
  86.         let butImg = UIImage(systemName: "xmark", withConfiguration: UIImage.SymbolConfiguration(pointSize: 20, weight: .light))
  87.         crossBut.translatesAutoresizingMaskIntoConstraints = false
  88.         crossBut.setImage(butImg, for: .normal)
  89.         crossBut.tintColor = .lightGray
  90.         crossBut.addAction( UIAction(handler: { (act) in
  91.             hideView()
  92.         }) , for: .touchUpInside)
  93.        
  94.         self.view.addSubview(mainView)
  95.         self.view.addSubview(background)
  96.         mainView.addSubview(image)
  97.         mainView.addSubview(nameLbl)
  98.         mainView.addSubview(subjLbl)
  99.         mainView.addSubview(stdLbl)
  100.         mainView.addSubview(addressLbl)
  101.         mainView.addSubview(colorView)
  102.         mainView.addSubview(acceptBut)
  103.         mainView.addSubview(crossBut)
  104.        
  105.         mainView.heightAnchor.constraint(equalToConstant: size.height).isActive = true
  106.         mainView.widthAnchor.constraint(equalToConstant: size.width).isActive = true
  107.         mainView.centerXAnchor.constraint(equalToSystemSpacingAfter: self.view.centerXAnchor, multiplier: 0).isActive = true
  108.         mainView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: -100).isActive = true
  109.        
  110.         background.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
  111.         background.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
  112.         background.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
  113.         background.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
  114.        
  115.         image.heightAnchor.constraint(equalToConstant: imageSize).isActive = true
  116.         image.widthAnchor.constraint(equalToConstant: imageSize).isActive = true
  117.         image.centerYAnchor.constraint(equalToSystemSpacingBelow: mainView.centerYAnchor, multiplier: 0).isActive = true
  118.         image.leadingAnchor.constraint(equalTo: mainView.leadingAnchor, constant: 20).isActive = true
  119.        
  120.         image.layer.masksToBounds = true
  121.         image.layer.cornerRadius = imageSize / 2
  122.        
  123.         stdLbl.heightAnchor.constraint(equalToConstant: 25).isActive = true
  124.         stdLbl.centerYAnchor.constraint(equalTo: image.centerYAnchor, constant: 0).isActive = true
  125.         stdLbl.leadingAnchor.constraint(equalTo: image.trailingAnchor, constant: 15).isActive = true
  126.         stdLbl.trailingAnchor.constraint(greaterThanOrEqualTo: mainView.trailingAnchor, constant: 5).isActive = true
  127.        
  128.         nameLbl.leadingAnchor.constraint(equalTo: image.trailingAnchor, constant: 15).isActive = true
  129.         nameLbl.heightAnchor.constraint(equalToConstant: 25).isActive = true
  130.         nameLbl.trailingAnchor.constraint(greaterThanOrEqualTo: mainView.trailingAnchor, constant: 5).isActive = true
  131.         nameLbl.bottomAnchor.constraint(equalTo: subjLbl.topAnchor, constant: -8).isActive = true
  132.        
  133.         subjLbl.leadingAnchor.constraint(equalTo: image.trailingAnchor, constant: 15).isActive = true
  134.         subjLbl.heightAnchor.constraint(equalToConstant: 25).isActive = true
  135.         subjLbl.trailingAnchor.constraint(greaterThanOrEqualTo: mainView.trailingAnchor, constant: 15).isActive = true
  136.         subjLbl.bottomAnchor.constraint(equalTo: stdLbl.topAnchor, constant: -8).isActive = true
  137.        
  138.         addressLbl.heightAnchor.constraint(equalToConstant: 50).isActive = true
  139.         addressLbl.leadingAnchor.constraint(equalTo: image.trailingAnchor, constant: 15).isActive = true
  140.         addressLbl.trailingAnchor.constraint(lessThanOrEqualTo: mainView.trailingAnchor, constant: 10).isActive = true
  141.         addressLbl.topAnchor.constraint(equalTo: stdLbl.bottomAnchor, constant: 8).isActive = true
  142.        
  143.         colorView.heightAnchor.constraint(equalToConstant: 7.5).isActive = true
  144.         colorView.topAnchor.constraint(equalTo: mainView.topAnchor).isActive = true
  145.         colorView.leadingAnchor.constraint(equalTo: mainView.leadingAnchor).isActive = true
  146.         colorView.trailingAnchor.constraint(equalTo: mainView.trailingAnchor).isActive = true
  147.        
  148.         acceptBut.heightAnchor.constraint(equalToConstant: 35).isActive = true
  149.         acceptBut.widthAnchor.constraint(equalToConstant: 80).isActive = true
  150.         acceptBut.bottomAnchor.constraint(equalTo: mainView.bottomAnchor, constant: -8).isActive = true
  151.         acceptBut.trailingAnchor.constraint(equalTo: mainView.trailingAnchor, constant: -14).isActive = true
  152.        
  153.         crossBut.heightAnchor.constraint(equalToConstant: 35).isActive = true
  154.         crossBut.widthAnchor.constraint(equalToConstant: 35).isActive = true
  155.         crossBut.topAnchor.constraint(equalTo: colorView.bottomAnchor, constant: 0).isActive = true
  156.         crossBut.trailingAnchor.constraint(equalTo: mainView.trailingAnchor, constant: 0).isActive = true
  157.        
  158.         UIView.animateKeyframes(withDuration: 0.9, delay: 0, options: .layoutSubviews)
  159.         {
  160.             UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1/1.3)
  161.             {
  162.                 mainView.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
  163.             }
  164.            
  165.             UIView.addKeyframe(withRelativeStartTime: 1/3, relativeDuration: 1/3)
  166.             {
  167.                 mainView.transform = .identity
  168.             }
  169.         }
  170.         completion: { (animation) in
  171.             print("Animation Done!")
  172.         }
  173.        
  174.         func hideView() {
  175.             // Call hide animation here
  176.             UIView.animateKeyframes(withDuration: 0.7, delay: 0, options: .layoutSubviews)
  177.             {
  178.  
  179.                 UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1/3)
  180.                 {
  181.                     mainView.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
  182.                 }
  183.  
  184.                 UIView.addKeyframe(withRelativeStartTime: 1/1.3, relativeDuration: 1/1.3)
  185.                 {
  186.                     mainView.transform = CGAffineTransform(scaleX: 0, y: 0)
  187.                 }
  188.  
  189.             } completion:
  190.             { (animation) in
  191.                 mainView.removeFromSuperview()
  192.                 self.background.removeFromSuperview()
  193.                 mainView.transform = .identity
  194.             }
  195.         }
  196.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement