-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserListViewController.swift
More file actions
106 lines (78 loc) · 3.04 KB
/
UserListViewController.swift
File metadata and controls
106 lines (78 loc) · 3.04 KB
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
//
// UserListViewController.swift
// AutoLayoutAnimation
//
// Created by Ozgur Vatansever on 10/26/15.
// Copyright © 2015 Techshed. All rights reserved.
//
import UIKit
private let reuseIdentifier = "UserCollectionViewCell"
class UserListViewController: UICollectionViewController {
convenience init() {
self.init(nibName: "UserListViewController", bundle: nil)
}
let dataSource: UserDataSource = {
let users = UserDataSource()
users.loadUsersFromPlist(named: "Users")
return users
}()
let colors: [UIColor] = [.red, .green, .yellow, .blue,
.orange, .red, .green, .yellow]
override func viewDidLoad() {
super.viewDidLoad()
title = "Users"
view.backgroundColor = .white
edgesForExtendedLayout = UIRectEdge()
collectionView!.register(UINib(nibName: "UserCollectionViewCell", bundle: nil),
forCellWithReuseIdentifier: reuseIdentifier)
let layout = collectionView!.collectionViewLayout as! UserCollectionViewLayout
layout.numberOfItemsInRow = 2
layout.delegate = self
}
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataSource.count
}
override func collectionView(
_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: reuseIdentifier,
for: indexPath) as! UserCollectionViewCell
cell.user = dataSource[indexPath.item]
cell.backgroundColor = colors[indexPath.item]
cell.delegate = self
return cell
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let layout = collectionView.collectionViewLayout as! UserCollectionViewLayout
layout.animatingIndexPath = indexPath
collectionView.performBatchUpdates({ () -> Void in
layout.invalidateLayout()
collectionView.setCollectionViewLayout(layout, animated: true)
}, completion: nil)
}
}
extension UserListViewController: UserCollectionViewLayoutDelegate {
func collectionView(
_ collectionView: UICollectionView,
layout userCollectionViewLayout: UserCollectionViewLayout,
heightForUserAtIndexPath indexPath: IndexPath) -> CGFloat {
return 150.0
}
}
extension UserListViewController: UserCollectionViewCellDelegate {
func cell(_ cell: UserCollectionViewCell, detailButtonTapped button: UIButton) {
if let indexPath = collectionView?.indexPath(for: cell) {
let layout = collectionView?.collectionViewLayout as! UserCollectionViewLayout
layout.animatingIndexPath = indexPath
self.collectionView!.performBatchUpdates({ () -> Void in
layout.invalidateLayout()
self.collectionView!.setCollectionViewLayout(layout, animated: true)
}, completion: nil)
}
}
}