JLChen
2021-05-18 a869383e163a18cdedcf587383c1eca043129754
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
//  Copyright © 2018年 dahua. All rights reserved.
//    定义一个参考的view、能根据keyboard能够,自动调整frame的视图
 
import UIKit
 
@objc public class DHAutoKeyboardView: UIView {
    
    /// 是否全屏界面,影响参考视图与键盘距离的计算
    public var isFullScreen: Bool = false
    
    /// 键盘关联的视图
    public weak var relatedView: UIView?
    
    /// 内部记录上次键盘的高度
    private var lastKeyboardHeight: CGFloat = 0
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addObserver()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.addObserver()
        self.addTapGesture()
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    func addObserver() {
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }
    
    func addTapGesture() {
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tap))
        self.addGestureRecognizer(tapGesture)
    }
    
    override public func layoutSubviews() {
        super.layoutSubviews()
    }
    
    @objc func keyboardWillShow(notification: Notification) {
        
        guard relatedView != nil else {
            return
        }
        
        if let value = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
            let keyboardFrame = value.cgRectValue
            lastKeyboardHeight = keyboardFrame.height
            
            keyboardShow(height: keyboardFrame.height)
        }
    }
    
    func keyboardShow(height: CGFloat, offset: CGFloat = 0) {
        //【*】参考视图与屏幕底部、键盘的距离计算
        var distanceToBottom = UIScreen.main.bounds.height - UIApplication.shared.statusBarFrame.height - self.relatedView!.frame.maxY
        
        if !isFullScreen {
            distanceToBottom -= 44
        }
        
        var transform: CGAffineTransform
        if distanceToBottom < height {
            transform = CGAffineTransform(translationX: 0, y: distanceToBottom - height - offset)
        } else {
            transform = CGAffineTransform.identity
        }
        
        UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut, animations: {
            self.transform = transform
        }, completion: nil)
    }
    
    // MARK: - Auto Adjust
    
    /// 自动调整布局
    ///
    /// - Parameter offset: 向上偏移尺寸,默认为0
    @objc func autoAdjust(offset: CGFloat = 0) {
    
        guard lastKeyboardHeight > 0 else {
            return
        }
        
        keyboardShow(height: lastKeyboardHeight, offset: offset)
    }
    
    //MAKR: - Notification
    @objc func keyboardWillHide(notification: Notification) {
        lastKeyboardHeight = 0
        
        guard relatedView != nil else {
            return
        }
        
        UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut, animations: {
            self.transform = CGAffineTransform.identity
        }, completion: nil)
    }
    
    @objc func tap() {
        self.endEditing(true)
    }
}