import UIKit
class ViewController: UIViewController, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
//1. 系统默认提供布局方式: 多行多列布局
let layout = UICollectionViewFlowLayout()
// //2. 设置Cell的大小
// layout.itemSize = CGSize(width: self.view.bounds.size.width, height: 200)
// //3. 滚动方向
layout.scrollDirection = .Horizontal
//// layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
// //cell的最小间距
// layout.minimumInteritemSpacing = 0
// //行间距
// layout.minimumLineSpacing = 0
let rect = CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height:self.view.bounds.size.height)
collectionView = UICollectionView(frame: rect, collectionViewLayout: layout)
collectionView.backgroundColor = UIColor.whiteColor()
collectionView.showsHorizontalScrollIndicator = false
// collectionView.contentOffset
// collectionView.scrollToItemAtIndexPath(<#T##indexPath: NSIndexPath##NSIndexPath#>, atScrollPosition: <#T##UICollectionViewScrollPosition#>, animated: <#T##Bool#>)
//分页显示
collectionView.pagingEnabled = true
//注册cell
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
//注册section header/footer
collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier:"header")
collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier:"footer")
collectionView.dataSource = self
collectionView.delegate = self
self.view.addSubview(collectionView)
// NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(didTimer(_:)), userInfo: nil, repeats: true)
}
func didTimer(timer: NSTimer) {
//两种表示方法
//NSIndexPath
//位置: frame.origin
//获取内容的偏移量
let offset = collectionView.contentOffset
//根据位置获取Cell
let indexPath = collectionView.indexPathForItemAtPoint(CGPoint(x: offset.x, y:0))
if indexPath!.item == 4 {
//滚动某个Cell到可见区域
collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem: 0, inSection:0), atScrollPosition: .Left, animated: false)
collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem: 1, inSection:0), atScrollPosition: .Left, animated: true)
}
else {
collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem: indexPath!.item + 1, inSection: 0), atScrollPosition: .Left, animated: true)
}
}
// func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
// return 4
// }
//Section中Cell的个数
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
if indexPath.item % 2 == 0 {
cell.contentView.backgroundColor = UIColor.redColor()
}
else {
cell.contentView.backgroundColor = UIColor.blueColor()
}
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("\(indexPath.section):\(indexPath.item)")
}
//section header
//section footer
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) ->UICollectionReusableView {
//header
if kind == UICollectionElementKindSectionHeader {
let header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "header", forIndexPath: indexPath)
header.backgroundColor = UIColor.blueColor()
return header
}
else {
let footer = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier: "footer", forIndexPath: indexPath)
footer.backgroundColor = UIColor.cyanColor()
return footer
}
}
//cell大小
func collectionView(collectionView: UICollectionView, layout collectionViewLayout:UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: 10 * (indexPath.item + 1), height: 10 * (indexPath.item +1))
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout:UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: 100, height: 200)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout:UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return CGSize(width: 100, height: 200)
}
}