158 lines
4.7 KiB
Swift
158 lines
4.7 KiB
Swift
//
|
||
// ZWStandardPopVC.swift
|
||
// ZWStandardSDK
|
||
//
|
||
// Created by JH on 2022/11/21.
|
||
//
|
||
|
||
import UIKit
|
||
|
||
/// 底部|中心弹窗控制器基类,自动撑开
|
||
open class DLCustomPopVC: UIViewController {
|
||
|
||
// MARK: - Style
|
||
/// 弹窗样式
|
||
public enum Style: Int {
|
||
/// 底部弹窗
|
||
case bottom
|
||
/// 中心弹窗
|
||
case center
|
||
}
|
||
|
||
// MARK: - Accessor
|
||
/// 自定义弹窗位置,默认底部,需在viewDidLoad之前设置生效
|
||
open var popStyle: Style = .bottom {
|
||
didSet {
|
||
updatePopStyle()
|
||
}
|
||
}
|
||
|
||
/// 底部弹窗时是否允许侧滑关闭,默认false
|
||
open var screenEdgeEnabled: Bool = false {
|
||
didSet {
|
||
updatePopStyle()
|
||
}
|
||
}
|
||
/// 动画执行时间
|
||
open var transitionDuration: CGFloat = 0.25 {
|
||
didSet {
|
||
updatePopStyle()
|
||
}
|
||
}
|
||
/// 是否可点击背景关闭界面,默认true
|
||
open var dimmingClick: Bool = true {
|
||
didSet {
|
||
backgroundView.isUserInteractionEnabled = dimmingClick
|
||
}
|
||
}
|
||
/// 暗色背景颜色,默认黑色,透明度0.4
|
||
open var dimmingColor: UIColor? = UIColor.black.withAlphaComponent(0.3) {
|
||
didSet {
|
||
backgroundView.backgroundColor = dimmingColor
|
||
}
|
||
}
|
||
/// 点击背景关闭回调,默认nil
|
||
open var dimmingBlock: (() -> Void)?
|
||
|
||
/// 自定义底部圆角大小,默认16,自动relative
|
||
open var bottomCornerRadius: CGFloat = 16
|
||
/// 自定义中心圆角大小,默认16,自动relative
|
||
open var centerCornerRadius: CGFloat = 16
|
||
/// 自定义中心内容边距,默认32,自动relative
|
||
open var centerContentInset: CGFloat = 32
|
||
|
||
// MARK: - Subviews
|
||
/// 背景视图,默认可点击背景关闭
|
||
private lazy var backgroundView: UIView = {
|
||
let view = UIView()
|
||
view.backgroundColor = .clear
|
||
view.isUserInteractionEnabled = dimmingClick
|
||
view.addTapGestrue { [weak self] _ in
|
||
self?.dismiss(animated: true, completion: self?.dimmingBlock)
|
||
}
|
||
return view
|
||
}()
|
||
|
||
/// 内容视图,高度须内部撑开
|
||
public lazy var contentView: UIView = {
|
||
let view = UIView()
|
||
view.backgroundColor = UIColor.white
|
||
return view
|
||
}()
|
||
|
||
// MARK: - Lifecyle
|
||
public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
|
||
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
|
||
|
||
modalPresentationStyle = .custom
|
||
updatePopStyle()
|
||
}
|
||
|
||
public required init?(coder: NSCoder) {
|
||
super.init(coder: coder)
|
||
|
||
modalPresentationStyle = .custom
|
||
updatePopStyle()
|
||
}
|
||
|
||
open override func viewDidLoad() {
|
||
super.viewDidLoad()
|
||
view.backgroundColor = .clear
|
||
|
||
view.addSubview(backgroundView)
|
||
view.addSubview(contentView)
|
||
|
||
backgroundView.layoutChain.edges()
|
||
if popStyle == .bottom {
|
||
contentView.layoutChain
|
||
.left()
|
||
.right()
|
||
.bottom()
|
||
} else {
|
||
contentView.layoutChain
|
||
.centerY()
|
||
.left(centerContentInset)
|
||
.right(centerContentInset)
|
||
}
|
||
}
|
||
|
||
deinit {
|
||
debugPrint(self, #function)
|
||
}
|
||
|
||
open override func viewDidLayoutSubviews() {
|
||
super.viewDidLayoutSubviews()
|
||
|
||
if popStyle == .bottom {
|
||
let cornerLayer = CAShapeLayer()
|
||
let path = UIBezierPath(roundedRect: contentView.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: bottomCornerRadius, height: bottomCornerRadius))
|
||
cornerLayer.frame = contentView.bounds
|
||
cornerLayer.path = path.cgPath
|
||
contentView.layer.mask = cornerLayer
|
||
} else {
|
||
contentView.layer.masksToBounds = true
|
||
contentView.layer.cornerRadius = centerCornerRadius
|
||
}
|
||
}
|
||
|
||
// MARK: - Private
|
||
private func updatePopStyle() {
|
||
if popStyle == .bottom {
|
||
let transition = __dl_setPresentTransition { [weak self] presentation in
|
||
presentation.dimmingColor = self?.dimmingColor
|
||
}
|
||
if screenEdgeEnabled {
|
||
transition.interactEnabled = true
|
||
transition.interactScreenEdge = true
|
||
}
|
||
transition.transitionDuration = transitionDuration
|
||
} else {
|
||
let transition = __dl_setAlertTransition { [weak self] presentation in
|
||
presentation.dimmingColor = self?.dimmingColor
|
||
}
|
||
transition.transitionDuration = transitionDuration
|
||
}
|
||
}
|
||
|
||
}
|