JLChen
2021-04-30 a5247b61d585627a1a7b1e1f35f34de9f0af9fba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
//  Copyright © 2018年 iblue. All rights reserved.
//
 
import UIKit
 
class DHBlurViewContainer: UIControl {
    
    private var contentView: UIView?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func awakeFromNib() {
        setupSubviews()
    }
    
    public func show(onView superview: UIView, animated: Bool) {
        guard contentView != nil else {
            return
        }
        
        superview.addSubview(self)
        addSubview(contentView!)
        
        self.alpha = 1
        
        if animated {
            contentView?.dh_y = self.bounds.height
            UIView.animate(withDuration: 0.35, delay: 0, options: .curveEaseInOut, animations: {
                self.contentView?.dh_y = self.bounds.height - self.contentView!.dh_height
            }, completion: nil)
        }
    }
    
    public func dismiss(animated: Bool) {
        if animated {
            UIView.animate(withDuration: 0.35, delay: 0, options: .curveEaseOut, animations: {
                self.contentView?.dh_y = self.bounds.height
                self.alpha = 0
            }, completion: { _ in
                self.removeFromSuperview()
            })
        } else {
            removeFromSuperview()
        }
    }
    
    public func setupContent(view: UIView, height: CGFloat) {
        contentView = view
        addSubview(view)
        
        view.frame = CGRect(x: 0, y: self.bounds.height - height, width: self.bounds.width, height: height)
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapContent))
        view.addGestureRecognizer(tap)
    }
    
    private func setupSubviews() {
        let blurEffect = UIBlurEffect(style: .light)
        let visualEffectView = UIVisualEffectView(effect: blurEffect)
        visualEffectView.frame = self.bounds
        addSubview(visualEffectView)
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapBackground))
        addGestureRecognizer(tap)
    }
    
    @objc private func tapBackground() {
        dismiss(animated: true)
    }
    
    @objc private func tapContent() {
        
    }
}