宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-04-11 4356615a9252a987a62469331b1fcf91c102e24c
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//
//  YYStaticTableViewController.swift
//  XinYuDriver
//
//  Created by alvin_y on 2020/3/23.
//  Copyright © 2020 yangwang. All rights reserved.
//
 
import UIKit
import RxCocoa
import RxSwift
import MBProgressHUD
//import SwifterSwift
class YYStaticTableViewController: UITableViewController,UIGestureRecognizerDelegate {
 
    // 调用此属性会自动拦截pop
    open var yy_popBlock:(() -> Void)?
    
    /// 导航栏黑线
    var nav_bar_hair_line_imageView : UIImageView?
    
    let disposeBag = DisposeBag()
    
    /// MBProgressHUD
    var hud : MBProgressHUD?
    
    // 导航栏返回按钮图片
    open var yy_nav_back_img:UIImage = UIImage.init(named: "back") ?? UIImage.init() {
        didSet {
            if yy_isHiddenBackItem {
                let btn = navigationItem.leftBarButtonItem?.customView as! UIButton
                btn.setImage(yy_nav_back_img, for: .normal)
                
            } else {
                yy_isHiddenBackItem = true
            }
        }
    }
    
    // 是否需要返回按钮
    open var yy_isHiddenBackItem:Bool = false {
        didSet {
            if yy_isHiddenBackItem {
                navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "", style: .done, target: nil, action: nil)
            } else {
                navigationItem.leftBarButtonItem = UIBarButtonItem.yy_creat(image: yy_nav_back_img, target: self, alignment:.left, action: #selector(backItemEvent)).item
            }
        }
    }
    
    // 是否需要返回手势
    open var yy_isEnabledBackTap:Bool = true {
        didSet {
            if yy_isEnabledBackTap {
                self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
            } else {
                self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
            }
        }
    }
    
    /// 是否需要导航栏黑线
    open var yy_isHiddenNavBarLine:Bool = false {
        didSet {
            if yy_isHiddenNavBarLine {
                nav_bar_hair_line_imageView?.isHidden = true
            } else {
                nav_bar_hair_line_imageView?.isHidden = false
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
 
        // Do any additional setup after loading the view.
        // 隐藏之后navigationBar不存在导致cash
        if let nav = navigationController?.navigationBar{
           nav_bar_hair_line_imageView = findHairlineImageViewUnder(view: nav)
        }
        view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1),NSAttributedString.Key.font: UIFont.init(name: Semibold, size: 18) ?? UIFont.systemFont(ofSize: 18)]
        self.navigationController?.navigationBar.barTintColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        self.navigationController?.navigationBar.shadowImage = UIImage.init(color: #colorLiteral(red: 0.9529411765, green: 0.9529411765, blue: 0.9529411765, alpha: 1))
        yy_isHiddenBackItem = false
        yy_isHiddenNavBarLine = false
        setupViews()
        defineLayouts()
        bindRx()
        request()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
    }
    
    //MARK: - 初始化或者配置所有的view,此方法将在UIViewController的viewDidLoad()方法中调用
    func setupViews()  {
        
    }
    
    //MARK: - 定义布局,在这里添加约束,此方法将在UIViewController的viewDidLoad()方法中调用
    func defineLayouts()  {
        
    }
    
    //MARK: - 绑定ViewModel中的数据,此方法将在UIViewController的viewDidLoad()方法中调用
    func bindRx() {
        
    }
    
    //MARK: - 请求数据,此方法将在UIViewController的viewDidLoad()方法中调用
    func request() {
    
    }
    
    //MARK: - NavigationController
    // pop点击事件
    @objc fileprivate func backItemEvent() {
        
        // 拦截pop事件
        if (yy_popBlock != nil) {
            yy_popBlock?()
            return
        }
        
        yy_pop()
    }
    
    /// UIStatusBarStyle
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return .default
    }
    
    /// 找到导航栏的线条并选择显示或隐藏
    /// - Parameter view: 导航栏View
    func findHairlineImageViewUnder(view: UIView) -> UIImageView?{
        if view.isKind(of: UIImageView.classForCoder()) && view.bounds.size.height <= 1.0{
            return view as? UIImageView
        }
        for subView in view.subviews {
            let imageView = self.findHairlineImageViewUnder(view: subView)
            if (imageView != nil){
                return imageView
            }
        }
        return nil
    }
    
    
    /// pop
    /// - Parameter animated: animated
    open func yy_pop(_ animated:Bool = true) {
        navigationController?.popViewController(animated: animated)
    }
    
    /// yy_popToRoot
    /// - Parameter animated: animated
    open func yy_popToRoot(_ animated: Bool = true) {
        navigationController?.popToRootViewController(animated: animated)
    }
    
    /// yy_push
    /// - Parameters:
    ///   - vc: UIViewController
    ///   - animated: animated
    ///   - hiddenBottom: hiddenBottom
    open func yy_push(vc:UIViewController, _ animated:Bool = true, _ hiddenBottom:Bool = true) {
        vc.hidesBottomBarWhenPushed = hiddenBottom
        self.navigationController?.pushViewController(vc, animated: animated)
    }
    
    /// push
    ///
    /// - Parameters:
    ///   - vc: UIViewController
    ///   - animated: animated
    ///   - hiddenBottom: hiddenBottom
    open func wy_pushAnimate(vc:UIViewController, _ animated:Bool = false, _ hiddenBottom:Bool = true){
        let transition = CATransition()
        transition.duration = 0.3
        transition.type = .moveIn
        transition.subtype = .fromTop
        navigationController?.view.layer.add(transition, forKey: kCATransition)
        vc.hidesBottomBarWhenPushed = hiddenBottom
        navigationController?.pushViewController(vc, animated: animated)
    }
    
    
    /// yy_present
    /// - Parameter vc: UIViewController
    open func yy_present(vc:UIViewController){
        vc.modalPresentationStyle = .custom
        vc.transitioningDelegate = self
        vc.view.frame = CGRect(x: 0, y: 0, width: screenW, height: screenH)
        self.present(vc, animated: true, completion: nil)
    }
    
    //MARK: - MBProgressHUD
    /// 不带文字
    func show()  {
        hud = build()
    }
    
    func hide()  {
        hud?.hide(animated: true)
    }
    
    func build() -> MBProgressHUD {
        let hud = MBProgressHUD.showAdded(to: UIApplication.shared.keyWindow!, animated: true)
        hud.mode = .indeterminate
        return hud
    }
    
    
    // MARK: - Table view data source
 
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return super.numberOfSections(in: tableView)
    }
 
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return super.tableView(tableView, numberOfRowsInSection: section)
    }
}
// MARK: - UIViewControllerTransitioningDelegate
extension YYStaticTableViewController: UIViewControllerTransitioningDelegate{
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return DismissAnimation()
    }
    
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return PresentAnimation()
    }
}