import
UIKit
/**
*/
@H_747_301@class
CustomLayout
:
UICollectionViewLayout
{
{
return
CGSizeMake
(collectionView!.bounds.size.width,
CGFloat
(collectionView!.numberOfItemsInSection(0) * 200 / 3 + 200))
}
)
]? {
var
attributesArray = [
]()
let
cellCount =
self
.collectionView!.numberOfItemsInSection(0)
for
i
in
0..<cellCount {
indexPath =
(forItem:i,inSection:0)
attributes =
.layoutAttributesForItemAtIndexPath(indexPath)
attributesArray.append(attributes!)
}
return
attributesArray
}
)
? {
attribute =
(forCellWithIndexPath:indexPath)
//单元格外部空隙,简单起见,这些常量都在 方法内部定义了,没有共享为类成员
//let itemSpacing = 2
lineSpacing = 5
//单元格边长
largeCellSide:
= 200
smallCellSide:
= 100
//内部间隙,左右5
insets =
UIEdgeInsetsMake
(2,5,2,5)
line:
Int
= indexPath.item / 3
//当前行的Y坐标
lineOriginY = largeCellSide *
(line) +
(lineSpacing * line)
+ insets.top
//右侧单元格X坐标,这里按左右对齐,所以中间空隙大
rightLargeX = collectionView!.bounds.size.width - largeCellSide - insets.right
rightSmallX = collectionView!.bounds.size.width - smallCellSide - insets.right
if
(indexPath.item % 6 == 0) {
attribute.frame =
CGRectMake
(insets.left,lineOriginY,largeCellSide,
largeCellSide)
}
else
(indexPath.item % 6 == 1) {
(rightSmallX,smallCellSide,
smallCellSide)
(indexPath.item % 6 == 2) {
lineOriginY + smallCellSide + insets.top,smallCellSide)
(indexPath.item % 6 == 3) {
smallCellSide )
(indexPath.item % 6 == 4) {
(indexPath.item % 6 == 5) {
(rightLargeX,
largeCellSide)
}
attribute
}
/*
//如果有页眉、页脚或者背景,可以用下面的 方法实现更多 效果
func layoutAttributesForSupplementaryViewOfKind(elementKind: String!,
atIndexPath indexPath: NSIndexPath!) -> UICollectionViewLayoutAttributes!
func layoutAttributesForDecorationViewOfKind(elementKind: String!,
atIndexPath indexPath: NSIndexPath!) -> UICollectionViewLayoutAttributes!
*/
--- 主页面 ViewController.swift ---
76
ViewController
UIViewController
,
UICollectionViewDelegate
UICollectionViewDataSource
{
collectionView:
UICollectionView
!
courses = [
[
"name"
:
"Swift"
"pic"
"swift.png"
],
"OC"
"oc.jpg"
"Java"
"java.png"
"PHP"
"PHP.jpeg"
"JS"
"js.jpeg"
"HTML"
"html.jpeg"
403_689@"Ruby"
"ruby.png"
]
]
viewDidLoad() {
super
.viewDidLoad()
layout =
()
//let layout = UICollectionViewFlowLayout()
.collectionView =
(
frame:
(0,view.bounds.size.width,view.bounds.height-20),
collectionViewLayout:layout)
.collectionView.delegate =
self
.collectionView.dataSource =
self
.collectionView.registerClass(
UICollectionViewCell
.
forCellWithReuseIdentifier:
"ViewCell"
)
//默认背景是黑色和label一致
.collectionView.backgroundColor =
UIColor
.whiteColor()
.view.addSubview(collectionView)
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
// CollectionView行数
collectionView(collectionView:
numberOfItemsInSection section:
) ->
{
courses.count;
}
cellForItemAtIndexPath indexPath:
) ->
{
// storyboard里设计的单元格
identify:
String
=
"ViewCell"
cell =
.collectionView.dequeueReusableCellWithReuseIdentifier(
identify,forIndexPath: indexPath)
as
UICollectionViewCell
img =
UIImageView
(image:
UIImage
(named: courses[indexPath.item][
]!))
img.frame = cell.bounds
lbl =
UILabel
(frame:
lbl.textAlignment =
NSTextAlignment
.
Center
lbl.text = courses[indexPath.item][
]
cell.addSubview(img)
cell.addSubview(lbl)
cell
}
//单元格大小
func collectionView(collectionView: UICollectionView!,
layout collectionViewLayout: UICollectionViewLayout!,0)!important">sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize {
let size:Float = indexPath.item % 3 == 0 ? 200 : 100
return CGSize(width:size,height:size)
}
*/
| |