宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
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
//
//  NIBLoadable.swift
//  SwiftAppDeleget
//
//  Created by Sweet on 2020/7/6.
//  Copyright © 2020 cyyc. All rights reserved.
//
import Foundation
import UIKit
/// 加载xib
protocol NIBLoadable {}
extension NIBLoadable where Self :UIView{
    static var nibName:String{
        return String.init(describing: self)
    }
    static func loadViewFromNib()->Self{
        return Bundle.main.loadNibNamed(nibName, owner: "\(self)", options: nil)?.last as! Self
    }
}
/// 抖动view
protocol Shakeable {}
extension Shakeable where Self : UIView {
    func shake() {
        let animation = CABasicAnimation(keyPath: "position")
        animation.duration = 0.05
        animation.repeatCount = 5
        animation.autoreverses = true
        animation.fromValue = CGPoint.init(x: self.center.x - 4.0, y: self.center.y)
        animation.toValue = CGPoint.init(x: self.center.x + 4.0, y: self.center.y)
        layer.add(animation, forKey: "position")
    }
}
 
/** reuse view protocol*/
protocol ReusableView {}
extension ReusableView where Self : UIView{
    // cellIdentifier
    static var reuseIdentifier:String{
        return String.init(describing: self)
    }
}
/**UITableView regist cell protocol*/
extension UITableView{
    func register<T:UITableViewCell>(_:T.Type) where T:ReusableView,T:NIBLoadable {
        let nib = UINib(nibName: T.nibName, bundle: nil)
        register(nib, forCellReuseIdentifier: T.reuseIdentifier)
    }
    func register<T: UITableViewCell>(_ : T.Type) where T: ReusableView {
        register(T.self, forCellReuseIdentifier: T.reuseIdentifier)
    }
}
/**UITableView create cell protocol*/
extension UITableView {
    func dequeueReusableCell<T: UITableViewCell>(forIndexPath indexPath: IndexPath) -> T where T: ReusableView {
        guard let cell = dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath as IndexPath) as? T else {
            fatalError("Could not dequeue cell with identifier: \(T.reuseIdentifier)")
        }
        return cell
    }
}
extension UICollectionView{
    func register<T:UICollectionViewCell>(_:T.Type) where T:ReusableView,T:NIBLoadable {
        let nib = UINib(nibName: T.nibName, bundle: nil)
        register(nib, forCellWithReuseIdentifier: T.reuseIdentifier)
    }
    func register<T: UICollectionViewCell>(_ : T.Type) where T: ReusableView {
        register(T.self, forCellWithReuseIdentifier: T.reuseIdentifier)
    }
}
extension UICollectionView{
    func dequeueReusableCell<T:UICollectionViewCell>(forIndexPath indexPath:IndexPath) -> T where T: ReusableView {
        guard let cell  = dequeueReusableCell(withReuseIdentifier: T.reuseIdentifier, for: indexPath) as? T else {
             fatalError("Could not dequeue cell with identifier: \(T.reuseIdentifier)")
        }
        return cell
    }
}