logo头像
Snippet 博客主题

自定义控件-LFImagePickerLayoutView

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
//
// LFImagePickerLayoutView.swift
// PXSSwift
//
// Created by Pro on 2019/12/24.
// Copyright © 2019 刘刘峰. All rights reserved.
//

import UIKit

public typealias CallBack = ()->()
public typealias DeleteCallBack = (_ deleteIndex:Int)->()
public typealias IndexCallBack = (_ selectRow:Int)->()

public struct ItemSize {
var width:CGFloat = 70
var height:CGFloat = 70
var minimumInteritemSpacing:CGFloat = 10
var minimumLineSpacing:CGFloat = 10
}

class LFImagePickerLayoutView: UIView {

let cellIdentifier = "LFImagePickerCell"
public var itemSize:ItemSize!
public var space:CGFloat = 10
public var datasourceHeight:CGFloat = 0
//添加回调
public var addCallBack:CallBack?
//点击回调
public var tapCellCallBack:IndexCallBack?
//删除回调
public var deletePhotoCallBack:DeleteCallBack?
//image个数
public var dataSource:[ZYPhotoModel]?
//当需要加号的时候是否隐藏加号
var neeHiddenPlus = false
//是否需要加号
public var hiddenPlus = false {
didSet{
if hiddenPlus == true{
neeHiddenPlus = false
}
}
}

//一行个数
public var numberOfLine = 3
//最大几个数
public var maxNumber = 9
public var hiddenDelete = false


private lazy var imageCollectionView: UICollectionView = {
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .vertical
// collectionView
let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout)
collectionView.backgroundColor = UIColor.clear
collectionView.isPagingEnabled = true
// 添加协议方法
collectionView.delegate = self
collectionView.dataSource = self
// 设置 cell

collectionView.register(cellWithClass: LFImagePickerCell.self)
collectionView.register(cellWithClass: LFAddImageCell.self)

return collectionView
}()

override init(frame: CGRect) {
super.init(frame: frame)
self.setupUI()
}
override public func awakeFromNib() {
super.awakeFromNib()
self.setupUI()
}

required public init?(coder aDecoder: NSCoder) {
// fatalError("init(coder:) has not been implemented")
super.init(coder: aDecoder)
}

}

//MARK: - UI
extension LFImagePickerLayoutView {
override func layoutSubviews() {
super.layoutSubviews()
for constanst in self.constraints {
var lineNumber:Float = 0.0
if neeHiddenPlus == true{
lineNumber = ceilf(Float(CGFloat(dataSource?.count ?? 0)/CGFloat(numberOfLine)))
}else{
lineNumber = ceilf(Float(CGFloat((dataSource?.count ?? 0)+1)/CGFloat(numberOfLine)))
}
print(lineNumber)
constanst.constant = CGFloat(lineNumber) * (itemSize.width + 10.0)
imageCollectionView.frame.size.width = self.frame.width
imageCollectionView.frame.size.height = constanst.constant
}
self.snp_updateConstraints { (make) in
make.height.equalTo(imageCollectionView.frame.size.height )
}



}

func setupUI(){
self.imageCollectionView.frame = self.frame
self.addSubview(self.imageCollectionView)
itemSize = ItemSize()

}

func updateCollectionView(){

}

public func reloadView(){
checkHiddenPlus()

let spaceNumber = CGFloat(numberOfLine) - 1
let width = (self.frame.size.width - (space * spaceNumber))/CGFloat(numberOfLine)
itemSize = ItemSize.init(width:width , height: width, minimumInteritemSpacing: space, minimumLineSpacing: space)
self.layoutSubviews()
imageCollectionView.reloadData()
}

func checkHiddenPlus(){

if dataSource == nil{
neeHiddenPlus = false
return
}
//显示加号且不是最大的时候
if maxNumber > (dataSource?.count)! && hiddenPlus == false{
neeHiddenPlus = false
}else{
neeHiddenPlus = true
let array = dataSource?[0..<maxNumber]
dataSource = Array(array!)

}
}

}

//MARK:- UICollectionViewDelegate
extension LFImagePickerLayoutView:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if neeHiddenPlus == true{

return dataSource?.count ?? 0
}else{

return (dataSource?.count ?? 0) + 1
}

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//加号cell
if indexPath.row >= dataSource?.count ?? 0 {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier:"LFAddImageCell", for: indexPath) as? LFAddImageCell else {
return UICollectionViewCell()
}

return cell
//图片cell
}else{
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier:cellIdentifier, for: indexPath) as? LFImagePickerCell else {
return UICollectionViewCell()
}
//==========
let photoTuple: ZYPhotoModel = self.dataSource![indexPath.row]
if let originImage = photoTuple.originImage {
cell.imageView.image = originImage
} else if let imageURL = photoTuple.imageURL {
if let thumbnailImage = photoTuple.thumbnailImage {
cell.imageView.image = thumbnailImage
}

cell.imageView.kf.setImage(with: URL.init(string: imageURL))
} else {
cell.imageView.image = photoTuple.thumbnailImage
}
//==========


cell.deleteBtn.isHidden = hiddenDelete

//删除
cell.deleteCallBack = { () in
self.dataSource?.remove(at: indexPath.row)
self.imageCollectionView.reloadData()
self.checkHiddenPlus()
self.deletePhotoCallBack!(indexPath.row)
}
return cell
}

}

public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.row >= dataSource?.count ?? 0 { //加号按钮
if let callback = addCallBack{
callback()
}
}else{
if let callback = tapCellCallBack {//点击图片
callback(indexPath.row)
}
}
}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return itemSize.minimumLineSpacing
}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return itemSize.minimumInteritemSpacing
}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width:itemSize.width, height: itemSize.height)
}

}
微信打赏

赞赏是不耍流氓的鼓励