ahmedattia213
5/21/2019 - 8:35 PM

POPUP

import UIKit

class BasicDialogView: UIView {
    let popupBackground = PopupBackground()
    private var dismissing: Bool = false
    lazy var circle : CAShapeLayer = {
        let circlePath = UIBezierPath(arcCenter: CGPoint(x:self.frame.width * 0.85  ,y: self.frame.height * 0.1  ), radius: CGFloat(21), startAngle: CGFloat(0), endAngle:CGFloat(Double.pi * 2), clockwise: true)
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = circlePath.cgPath
        shapeLayer.fillColor = UIColor.white.cgColor
        let xPos = (circlePath.bounds.minX + circlePath.bounds.midX)/2
        let yPos = (-circlePath.bounds.midY + circlePath.bounds.minY)/2
        xImage.frame = CGRect(x: xPos , y: yPos, width: 21, height: 21)
        xImage.contentMode = .scaleToFill
        return shapeLayer
    }()
    
    let mainView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()
    
    lazy var xImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "xIcon"), contentMode: .scaleToFill, cornerRadius: 0, userInteraction: true)
        return image
    }()
    
    func dismissDialog() {
        dismissing = true
        popupBackground.removeFromSuperview()
        let popupHeight: CGFloat = self.frame.height
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0.5, options: .curveEaseOut, animations: {
            self.frame = CGRect(x: 0, y: UIScreen.main.bounds.height , width: self.frame.width, height: popupHeight)
        }, completion: { _ in
            self.removeFromSuperview()
        })
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
        self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTapOutsidePopup)))
    }
    
    func setupViews() {
        layer.addSublayer(circle)
        addSubview(mainView)
        backgroundColor = .clear
        mainView.backgroundColor = .white
        mainView.anchorBottomLeftRight()
        mainView.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.9).isActive = true
        mainView.addSubview(xImage)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func handleTapOutsidePopup(_ sender: UITapGestureRecognizer?) {
        let point: CGPoint? = sender?.location(in: sender?.view)
        let viewTouched: UIView? = sender?.view?.hitTest(point ?? CGPoint.zero, with: nil)
        if let viewTouched = viewTouched {
            if !(viewTouched.isDescendant(of: self.mainView)) {
                if !dismissing {
                    self.dismissDialog()
                }
            }
        }
    }
    
    override func touchesBegan(_ touches: Swift.Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        let touch = touches.first
        let point = touch!.location(in: self)
        if let circlePath = circle.path {
            if circlePath.contains(point) {
                if !dismissing {
                    self.dismissDialog()
                }
            }
        }
    }
    
    func animateAndShow(height: CGFloat) {
        let popupHeight: CGFloat = height
        self.popupBackground.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTapOutsidePopup)))
        UIApplication.shared.keyWindow!.addSubview(self.popupBackground)
        self.frame = CGRect(x: 0, y: UIApplication.shared.keyWindow!.frame.height, width: UIApplication.shared.keyWindow!.frame.width, height: popupHeight)
        
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1.5, initialSpringVelocity: 0.5, options: .curveEaseOut, animations: {
            UIApplication.shared.keyWindow!.addSubview(self)
            self.frame = CGRect(x: 0, y: UIApplication.shared.keyWindow!.frame.height - popupHeight, width: UIApplication.shared.keyWindow!.frame.width, height: popupHeight)
        }, completion: nil)
    }
}