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
| import UIKit class MainLayout : UICollectionViewLayout { override func collectionViewContentSize() -> CGSize { return CGSizeMake( collectionView!.bounds.size.width, 320 ) } override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { var attributesArray = [UICollectionViewLayoutAttributes]() let cellCount = self.collectionView!.numberOfItemsInSection(0) for i in 0..<cellCount { let indexPath = NSIndexPath(forItem:i, inSection:0) let attributes = self.layoutAttributesForItemAtIndexPath(indexPath) attributesArray.append(attributes!) } return attributesArray } override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? { let attribute = UICollectionViewLayoutAttributes(forCellWithIndexPath:indexPath) let topSpace:CGFloat = 10; let space:CGFloat = 6; let leftSpace:CGFloat = 10; let itemWidth:CGFloat = (collectionView!.bounds.size.width - leftSpace*2 - space)/2; var itemHeight:CGFloat = 214; var itemHeight2:CGFloat = 104; if(ZJ_SysUtils.getDeviceType() == ZJ_SysUtils.DeviceType.iPhone6){ itemHeight2 = itemHeight2 * 1.3; itemHeight = itemHeight2 * 2 + space; } let rightX:CGFloat = leftSpace + itemWidth + space; let right1Y = topSpace; let right2Y = topSpace + itemHeight2 + space; let right3Y = topSpace + itemHeight + space; let item = indexPath.item; if (item == 0) { attribute.frame = CGRectMake(leftSpace, right1Y, itemWidth, itemHeight) } else if (item == 1) { attribute.frame = CGRectMake(rightX, right1Y, itemWidth, itemHeight2) } else if (item == 2) { attribute.frame = CGRectMake(rightX, right2Y, itemWidth, itemHeight2) } else if (item == 3) { attribute.frame = CGRectMake(leftSpace, right3Y, itemWidth, itemHeight2) } else if (item == 4) { attribute.frame = CGRectMake(rightX, right3Y, itemWidth, itemHeight2) } return attribute } }
|