Commit f8d7781b by Daniel Dahan

updated MaterialCollectionViewLayout to take scale into account

parent db20b06a
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
public protocol MaterialCollectionViewDataSource : MaterialDelegate, UICollectionViewDataSource { public protocol MaterialCollectionViewDataSource : MaterialDelegate, UICollectionViewDataSource {
/** /**
/// Retrieves the items for the collectionView. /// Retrieves the items for the collectionView.
- Returns: An Array of MaterialCollectionViewDataSourceItem objects. - Returns: An Array of Arrays of MaterialCollectionViewDataSourceItem objects.
*/ */
func items() -> Array<MaterialCollectionViewDataSourceItem> func items() -> Array<Array<MaterialCollectionViewDataSourceItem>>
} }
\ No newline at end of file
...@@ -36,22 +36,24 @@ public enum MaterialCollectionViewLayoutScrollDirection { ...@@ -36,22 +36,24 @@ public enum MaterialCollectionViewLayoutScrollDirection {
} }
public class MaterialCollectionViewLayout : UICollectionViewLayout { public class MaterialCollectionViewLayout : UICollectionViewLayout {
/// Size of the content. /// Size of the content.
private var contentSize: CGSize = CGSizeZero private var contentSize: CGSize = CGSizeZero
/// Layout attribute items. /// Layout attribute items.
private var layoutItems: Array<(UICollectionViewLayoutAttributes, NSIndexPath)>? private var layoutItems: Array<(UICollectionViewLayoutAttributes, NSIndexPath)> = Array<(UICollectionViewLayoutAttributes, NSIndexPath)>()
/// Used to calculate the dimensions of the cells. /// Used to calculate the dimensions of the cells.
private var offset: CGFloat = 0 private var offset: CGFloat = 0
/// Cell items. /// Cell items.
private var items: Array<MaterialCollectionViewDataSourceItem>? private var items: Array<Array<MaterialCollectionViewDataSourceItem>>?
/// Scroll direction. /// Scroll direction.
public var scrollDirection: MaterialCollectionViewLayoutScrollDirection = .Vertical public var scrollDirection: MaterialCollectionViewLayoutScrollDirection = .Vertical
/// Scale of the screen.
public var scale: CGFloat = 2
/** /**
Retrieves the index paths for the items within the passed in CGRect. Retrieves the index paths for the items within the passed in CGRect.
- Parameter rect: A CGRect that acts as the bounds to find the items within. - Parameter rect: A CGRect that acts as the bounds to find the items within.
...@@ -59,7 +61,7 @@ public class MaterialCollectionViewLayout : UICollectionViewLayout { ...@@ -59,7 +61,7 @@ public class MaterialCollectionViewLayout : UICollectionViewLayout {
*/ */
public func indexPathsOfItemsInRect(rect: CGRect) -> Array<NSIndexPath> { public func indexPathsOfItemsInRect(rect: CGRect) -> Array<NSIndexPath> {
var paths: Array<NSIndexPath> = Array<NSIndexPath>() var paths: Array<NSIndexPath> = Array<NSIndexPath>()
for (attribute, indexPath) in layoutItems! { for (attribute, indexPath) in layoutItems {
if CGRectIntersectsRect(rect, attribute.frame) { if CGRectIntersectsRect(rect, attribute.frame) {
paths.append(indexPath) paths.append(indexPath)
} }
...@@ -69,18 +71,13 @@ public class MaterialCollectionViewLayout : UICollectionViewLayout { ...@@ -69,18 +71,13 @@ public class MaterialCollectionViewLayout : UICollectionViewLayout {
public override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? { public override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
let attributes: UICollectionViewLayoutAttributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath) let attributes: UICollectionViewLayoutAttributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
let item: MaterialCollectionViewDataSourceItem = items![indexPath.section][indexPath.item]
if 0 == indexPath.row {
offset = 0
}
let item: MaterialCollectionViewDataSourceItem = items![indexPath.row]
if .Vertical == scrollDirection { if .Vertical == scrollDirection {
attributes.frame = CGRectMake(0, offset, collectionView!.bounds.width, item.size.height) attributes.frame = CGRectMake(0, offset / scale, collectionView!.bounds.width, item.size.height)
offset += item.size.height offset += item.size.height
} else { } else {
attributes.frame = CGRectMake(offset, 0, item.size.width, collectionView!.bounds.height) attributes.frame = CGRectMake(offset / scale, 0, item.size.width, collectionView!.bounds.height)
offset += item.size.width offset += item.size.width
} }
...@@ -89,7 +86,7 @@ public class MaterialCollectionViewLayout : UICollectionViewLayout { ...@@ -89,7 +86,7 @@ public class MaterialCollectionViewLayout : UICollectionViewLayout {
public override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { public override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var layoutAttributes: Array<UICollectionViewLayoutAttributes> = Array<UICollectionViewLayoutAttributes>() var layoutAttributes: Array<UICollectionViewLayoutAttributes> = Array<UICollectionViewLayoutAttributes>()
for (attribute, _) in layoutItems! { for (attribute, _) in layoutItems {
if CGRectIntersectsRect(rect, attribute.frame) { if CGRectIntersectsRect(rect, attribute.frame) {
layoutAttributes.append(attribute) layoutAttributes.append(attribute)
} }
...@@ -109,19 +106,23 @@ public class MaterialCollectionViewLayout : UICollectionViewLayout { ...@@ -109,19 +106,23 @@ public class MaterialCollectionViewLayout : UICollectionViewLayout {
let dataSource: MaterialCollectionViewDataSource = collectionView!.dataSource as! MaterialCollectionViewDataSource let dataSource: MaterialCollectionViewDataSource = collectionView!.dataSource as! MaterialCollectionViewDataSource
items = dataSource.items() items = dataSource.items()
layoutItems = Array<(UICollectionViewLayoutAttributes, NSIndexPath)>() layoutItems.removeAll()
offset = 0 offset = 0
var indexPath: NSIndexPath? var indexPath: NSIndexPath?
var count: Int = 0
for item in items! { for var i: Int = 0, l: Int = items!.count; i < l; ++i {
indexPath = NSIndexPath(forItem: count++, inSection: 0) let v: Array<MaterialCollectionViewDataSourceItem> = items![i]
layoutItems?.append((layoutAttributesForItemAtIndexPath(indexPath!)!, indexPath!)) for var j: Int = 0, k: Int = v.count; j < k; ++j {
let item: MaterialCollectionViewDataSourceItem = v[j]
indexPath = NSIndexPath(forItem: j, inSection: i)
layoutItems.append((layoutAttributesForItemAtIndexPath(indexPath!)!, indexPath!))
offset += .Vertical == scrollDirection ? item.size.height : item.size.width offset += .Vertical == scrollDirection ? item.size.height : item.size.width
} }
}
contentSize = .Vertical == scrollDirection ? CGSizeMake(collectionView!.bounds.width, offset) : CGSizeMake(offset, collectionView!.bounds.height) contentSize = .Vertical == scrollDirection ? CGSizeMake(collectionView!.bounds.width, offset / scale) : CGSizeMake(offset / scale, collectionView!.bounds.height)
offset = 0
} }
public override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint) -> CGPoint { public override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint) -> CGPoint {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment