質問編集履歴

1

ソースコードの追記をした

2019/01/21 14:38

投稿

pftyuk
pftyuk

スコア52

test CHANGED
File without changes
test CHANGED
@@ -34,7 +34,41 @@
34
34
 
35
35
  class AlbumViewController: UIViewController {
36
36
 
37
- var images:[UIImage] = []
37
+ var assets:[PHAsset] = []
38
+
39
+ var selectedAsset:PHAsset? = nil
40
+
41
+
42
+
43
+ @IBOutlet weak var albumCollectionView: GeminiCollectionView!{
44
+
45
+ didSet{
46
+
47
+ let cellIdentifier = "AlbumCollectionViewCell"
48
+
49
+ let nib = UINib(nibName: cellIdentifier, bundle: nil)
50
+
51
+ albumCollectionView.register(nib, forCellWithReuseIdentifier: cellIdentifier)
52
+
53
+ albumCollectionView.delegate = self
54
+
55
+ albumCollectionView.dataSource = self
56
+
57
+
58
+
59
+ albumCollectionView.gemini
60
+
61
+ .circleRotationAnimation()
62
+
63
+ .radius(450)
64
+
65
+ .rotateDirection(.clockwise)
66
+
67
+ .itemRotationEnabled(true)
68
+
69
+ }
70
+
71
+ }
38
72
 
39
73
 
40
74
 
@@ -42,37 +76,259 @@
42
76
 
43
77
  super.viewDidLoad()
44
78
 
79
+ assets = getAssetsInAlbum()
80
+
81
+
82
+
83
+ if let layout = albumCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
84
+
85
+ layout.scrollDirection = .vertical
86
+
87
+ albumCollectionView.collectionViewLayout = layout
88
+
89
+ }
90
+
91
+ }
92
+
93
+
94
+
95
+ private func getAssetsInAlbum()->[PHAsset]{
96
+
97
+ var assets:[PHAsset] = []
98
+
45
99
 
46
100
 
47
- //アルバム内の写真を取得
48
-
49
- images = getImagesInAlbum()
101
+ let options = PHFetchOptions()
102
+
50
-
103
+ let fetchResult = PHAsset.fetchAssets(with: .image, options: options)
104
+
105
+ fetchResult.enumerateObjects { (asset, index, stop) -> Void in
106
+
107
+ assets.append(asset)
108
+
51
- }
109
+ }
52
110
 
53
111
 
54
112
 
113
+ return assets
114
+
115
+ }
116
+
117
+
118
+
119
+ // MARK: - Navigation
120
+
121
+
122
+
123
+ // In a storyboard-based application, you will often want to do a little preparation before navigation
124
+
125
+ override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
126
+
127
+ if segue.identifier == "toPhotoPreView"{
128
+
129
+ let photoViewController = segue.destination as! PhotoPreViewController
130
+
55
- private func getImagesInAlbum()->[UIImage]{
131
+ photoViewController.selectedAsset = selectedAsset
132
+
56
-
133
+ }
134
+
135
+ }
136
+
137
+ }
138
+
139
+
140
+
141
+ // MARK: - UIScrollViewDelegate
142
+
143
+ extension AlbumViewController {
144
+
145
+ func scrollViewDidScroll(_ scrollView: UIScrollView) {
146
+
147
+ albumCollectionView.animateVisibleCells()
148
+
149
+ }
150
+
151
+ }
152
+
153
+
154
+
155
+ // MARK: - UICollectionViewDelegate
156
+
157
+ extension AlbumViewController: UICollectionViewDelegate {
158
+
159
+ func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
160
+
161
+ selectedAsset = assets[indexPath.row]
162
+
163
+ performSegue(withIdentifier: "toPhotoPreView", sender: nil)
164
+
165
+ }
166
+
167
+
168
+
169
+ func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
170
+
171
+ if let cell = cell as? GeminiCell {
172
+
173
+ self.albumCollectionView.animateCell(cell)
174
+
175
+ }
176
+
177
+ }
178
+
179
+ }
180
+
181
+ // MARK: - UICollectionViewDataSource
182
+
183
+ extension AlbumViewController:UICollectionViewDataSource{
184
+
185
+ func numberOfSections(in collectionView: UICollectionView) -> Int {
186
+
187
+ return 1
188
+
189
+ }
190
+
191
+
192
+
193
+ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
194
+
195
+ return assets.count
196
+
197
+ }
198
+
199
+
200
+
201
+ func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
202
+
203
+ let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AlbumCollectionViewCell", for: indexPath) as! AlbumCollectionViewCell
204
+
205
+ cell.configure(with: assets[indexPath.row])
206
+
207
+ self.albumCollectionView.animateCell(cell)
208
+
209
+ return cell
210
+
211
+ }
212
+
213
+ }
214
+
215
+
216
+
217
+ // MARK: - UICollectionViewDelegateFlowLayout
218
+
219
+ extension AlbumViewController: UICollectionViewDelegateFlowLayout {
220
+
57
- var images:[UIImage] = []
221
+ private enum Const {
222
+
223
+ static let collcetionViewSize = CGSize(width: UIScreen.main.bounds.size.width * 0.7, height: UIScreen.main.bounds.size.height * 0.7)
224
+
225
+ }
226
+
227
+
228
+
229
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
230
+
231
+ return Const.collcetionViewSize
232
+
233
+ }
234
+
235
+
236
+
237
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
238
+
239
+ guard let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else {
240
+
241
+ return UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50)
242
+
243
+ }
58
244
 
59
245
 
60
246
 
247
+ switch layout.scrollDirection {
248
+
249
+ case .horizontal:
250
+
251
+
252
+
253
+ let verticalMargin: CGFloat = (collectionView.bounds.height - Const.collcetionViewSize.height) / 2
254
+
255
+ return UIEdgeInsets(top: 50 + verticalMargin,
256
+
257
+ left: 50,
258
+
259
+ bottom: 50 + verticalMargin,
260
+
261
+ right: 50)
262
+
263
+ case .vertical:
264
+
265
+
266
+
267
+ let horizontalMargin: CGFloat = (collectionView.bounds.width - Const.collcetionViewSize.width) / 2
268
+
269
+ return UIEdgeInsets(top: 50,
270
+
271
+ left: 50 + horizontalMargin,
272
+
273
+ bottom: 50,
274
+
275
+ right: 50 + horizontalMargin)
276
+
277
+ }
278
+
279
+ }
280
+
281
+
282
+
283
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
284
+
285
+ return 10
286
+
287
+ }
288
+
289
+
290
+
291
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
292
+
293
+ return 0
294
+
295
+ }
296
+
297
+ }
298
+
299
+ ```
300
+
301
+ ```
302
+
303
+ class AlbumCollectionViewCell: GeminiCell {
304
+
305
+ @IBOutlet weak var image: UIImageView!
306
+
307
+
308
+
309
+ override func awakeFromNib() {
310
+
311
+ super.awakeFromNib()
312
+
313
+ // Initialization code
314
+
315
+ }
316
+
317
+
318
+
319
+ func configure(with asset:PHAsset ) {
320
+
321
+ var photoImage:UIImage? = nil
322
+
323
+ let targetSize = CGSize(width: self.bounds.width, height: self.bounds.height)
324
+
61
325
  let manager = PHImageManager()
62
326
 
63
- let options = PHFetchOptions()
64
-
65
- let assets: PHFetchResult = PHAsset.fetchAssets(with: .image, options: options)
66
-
67
- assets.enumerateObjects { (asset, index, stop) -> Void in
68
-
69
- manager.requestImage(for: asset, targetSize: PHImageManagerMaximumSize, contentMode: .aspectFill,options: nil){ (image, info) in
327
+ manager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFit,options: nil){ (image, info) in
70
-
328
+
71
- if image != nil {
329
+ if image != nil{
72
-
330
+
73
- images.append(image!)
331
+ photoImage = image
74
-
75
- }
76
332
 
77
333
  }
78
334
 
@@ -80,10 +336,10 @@
80
336
 
81
337
 
82
338
 
83
- return images
339
+ self.image.image = photoImage
84
-
340
+
85
- }
341
+ }
86
-
342
+
87
- }
343
+ }
88
-
344
+
89
- ```
345
+ ```