//
|
// 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
|
}
|
}
|