• swift UIView弹窗协议


    //
    //  DCUIAlertSystemAnimationProtocol.swift
    //  doctor
    //
    //  Created by baitongtong on 2022/6/6.
    //  Copyright © 2022 apple. All rights reserved.
    //
    
    import UIKit
    import SnapKit
    
    private var _coverKey = "DC_alertCoverKey"
    private var _constraintsClosureKey = "DC_constraintsClosureKey"
    
    protocol DCAlertSystemAnimationProtocol {
        
        /// 弹出
        func present()
        /// 消失
        func dismiss()
        
        /// 指定alertView约束 present 之前可设置
        func constrains(_ closure: @escaping (ConstraintMaker) -> Void)
    }
    
    extension DCAlertSystemAnimationProtocol where Self: UIView {
    
        
         private var _alertCover: UIView {
            set {
                objc_setAssociatedObject(self, &_coverKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            }
            get {
                return ((objc_getAssociatedObject(self, &_coverKey) as? UIView) ?? UIView())
            }
        }
        
        
        private var _constrainsClosure: ((ConstraintMaker) -> Void)? {
            set {
                objc_setAssociatedObject(self, &_constraintsClosureKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            }
            get {
                guard let closure = objc_getAssociatedObject(self, &_constraintsClosureKey) as? ((ConstraintMaker) -> Void) else {
                    return nil
                }
                return closure
            }
        }
        
        func constrains(_ closure: @escaping (ConstraintMaker) -> Void) {
            _constrainsClosure = closure
        }
        
        func present() {
            let cover = UIView()
            cover.frame = UIScreen.main.bounds
            cover.backgroundColor = .init(white: 0, alpha: 0.25)
            cover.addSubview(self)
            
            if let constraintsClosure = _constrainsClosure {
                self.snp.makeConstraints(constraintsClosure)
            } else {
                self.snp.makeConstraints { make in
                    make.center.equalToSuperview()
                    make.left.equalTo(25)
                }
            }
    
            cover.alpha = 0
            self.alpha = 0.5
            
            UIApplication.shared.keyWindow?.addSubview(cover)
            cover.layoutIfNeeded()
            
            self.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
            
            UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseInOut) {
                cover.alpha = 1
                self.alpha = 1
                self.transform = CGAffineTransform.identity
            }
            _alertCover = cover
        }
        
        func dismiss() {
            UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseInOut) {
                self._alertCover.alpha = 0
            } completion: { isCompleted in
                if isCompleted {
                    self.removeFromSuperview()
                    self._alertCover.removeFromSuperview()
                }
            }
        }
    }
  • 相关阅读:
    java获得文件的最后修改时间
    【Tomcat】解决Tomcat catalina.out 不断成长导致档案过大的问题
    mysql报错Packet for query is too large (12238 > 1024). You can change this value
    【Tomcat】linux下实时查看tomcat运行日志
    linux下面MySQL变量修改及生效
    【Vim命令大全】史上最全的Vim命令
    (总结)Linux的chattr与lsattr命令详解
    MySql将查询结果插入到另外一张表
    dos中定义变量与获取常见的引用变量以及四则运算、备份文件(set用法)
    批处理BAT替换与截取字符串的用法t1=%a:~3%是什么意思
  • 原文地址:https://www.cnblogs.com/baitongtong/p/16371730.html
Copyright © 2020-2023  润新知