-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathG0CenterCollectionView.swift
64 lines (44 loc) · 2.08 KB
/
G0CenterCollectionView.swift
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
import UIKit
class G0CenterCollectionView : UICollectionView, UICollectionViewDataSource, UICollectionViewDelegate {
var left_right_padding : CGFloat = 20.0
var item_width : CGFloat = 320.0
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initComponents()
}
override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
super.init(frame: frame, collectionViewLayout: layout)
initComponents()
}
func initComponents () {
delegate = self
dataSource = self
let layout : UICollectionViewFlowLayout = CenterCellCollectionViewFlowLayout()
layout.itemSize = CGSize(width: item_width - left_right_padding, height: item_width - left_right_padding)
layout.scrollDirection = .Horizontal
self.setCollectionViewLayout(layout, animated: false)
}
override func layoutSubviews() {
super.layoutSubviews()
var insets = self.contentInset
let value = (self.frame.size.width - (collectionViewLayout as! UICollectionViewFlowLayout).itemSize.width) * 0.5
insets.left = value
insets.right = value
self.contentInset = insets
self.decelerationRate = UIScrollViewDecelerationRateFast;
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("center_cell", forIndexPath: indexPath)
cell.backgroundColor = getRandomColor()
return cell
}
func getRandomColor() -> UIColor{
let randomRed:CGFloat = CGFloat(drand48())
let randomGreen:CGFloat = CGFloat(drand48())
let randomBlue:CGFloat = CGFloat(drand48())
return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: 1.0)
}
}