jsdw_ios/QuickLocation/UIKit/Pop/DLCustomPopVC.swift

158 lines
4.7 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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)?
/// 16relative
open var bottomCornerRadius: CGFloat = 16
/// 16relative
open var centerCornerRadius: CGFloat = 16
/// 32relative
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
}
}
}