CNLiveModalPresentationViewController.swift 1.81 KB
//
//  CNLiveModalPresentationViewController.swift
//  metaCode
//
//  Created by zx on 2023/11/16.
//

import Foundation

typealias completionBlock = (_ finished:Bool)->Void
typealias confirmActionBlock = ()->Void

class CNLiveModalPresentationViewController:UIViewController{
    
    var confirmBlock:confirmActionBlock?
    lazy var contentView :UIView = {
        let contentView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
        return contentView
    }()
    
    lazy var bgView :UIView = {
        let bgView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.width, height: self.view.height))
        bgView.backgroundColor = .black
        bgView.alpha = 0.2
        return bgView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .clear
        self.view.addSubview(self.bgView)
        self.view.addSubview(self.contentView)
    }
    
    @objc func cancelButtonAction(){
        self.hidingAnimationWithCompletion { finished in
            
        }
    }
    @objc func confirmButtonAction(){
        if ((self.confirmBlock?()) != nil){
            self.confirmBlock!()
        }
    }
    func showingAnimationWithCompletion(completion:@escaping completionBlock){
 
        UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseOut) {
            self.contentView.top = (self.view.height-self.contentView.height)/2
        } completion: { finished in
            completion(finished)
        }
    }
    
    func hidingAnimationWithCompletion(completion:@escaping completionBlock){
        UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseOut) {
            self.contentView.top = self.view.height
        } completion: { finished in
            completion(finished)
            self.view.removeFromSuperview()
        }
    }
}