Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
Material
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dmitriy Stepanets
Material
Commits
db20b06a
Commit
db20b06a
authored
Feb 24, 2016
by
Daniel Dahan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
scrollDirection added to MaterialCollectionViewLayout
parent
e8baec48
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
30 deletions
+56
-30
Sources/MaterialCollectionViewDataSource.swift
+5
-0
Sources/MaterialCollectionViewDataSourceItem.swift
+4
-3
Sources/MaterialCollectionViewLayout.swift
+47
-27
No files found.
Sources/MaterialCollectionViewDataSource.swift
View file @
db20b06a
...
@@ -29,5 +29,9 @@
...
@@ -29,5 +29,9 @@
*/
*/
public
protocol
MaterialCollectionViewDataSource
:
MaterialDelegate
,
UICollectionViewDataSource
{
public
protocol
MaterialCollectionViewDataSource
:
MaterialDelegate
,
UICollectionViewDataSource
{
/**
/// Retrieves the items for the collectionView.
- Returns: An Array of MaterialCollectionViewDataSourceItem objects.
*/
func
items
()
->
Array
<
MaterialCollectionViewDataSourceItem
>
func
items
()
->
Array
<
MaterialCollectionViewDataSourceItem
>
}
}
\ No newline at end of file
Sources/MaterialCollectionViewDataSourceItem.swift
View file @
db20b06a
...
@@ -30,10 +30,10 @@
...
@@ -30,10 +30,10 @@
public
struct
MaterialCollectionViewDataSourceItem
{
public
struct
MaterialCollectionViewDataSourceItem
{
var
data
:
AnyObject
var
data
:
AnyObject
var
height
:
CGFloat
var
size
:
CGSize
public
init
(
data
:
AnyObject
,
height
:
CGFloat
)
{
public
init
(
data
:
AnyObject
,
size
:
CGSize
)
{
self
.
data
=
data
self
.
data
=
data
self
.
height
=
height
self
.
size
=
size
}
}
}
}
\ No newline at end of file
Sources/MaterialCollectionViewLayout.swift
View file @
db20b06a
...
@@ -30,29 +30,59 @@
...
@@ -30,29 +30,59 @@
import
UIKit
import
UIKit
public
enum
MaterialCollectionViewLayoutScrollDirection
{
case
Vertical
case
Horizontal
}
public
class
MaterialCollectionViewLayout
:
UICollectionViewLayout
{
public
class
MaterialCollectionViewLayout
:
UICollectionViewLayout
{
/// Size of the content.
private
var
contentSize
:
CGSize
=
CGSizeZero
private
var
contentSize
:
CGSize
=
CGSizeZero
/// Layout attribute items.
private
var
layoutItems
:
Array
<
(
UICollectionViewLayoutAttributes
,
NSIndexPath
)
>
?
private
var
layoutItems
:
Array
<
(
UICollectionViewLayoutAttributes
,
NSIndexPath
)
>
?
/// Used to calculate the dimensions of the cells.
private
var
offset
:
CGFloat
=
0
private
var
offset
:
CGFloat
=
0
/// Cell items.
private
var
items
:
Array
<
MaterialCollectionViewDataSourceItem
>
?
/// Scroll direction.
public
var
scrollDirection
:
MaterialCollectionViewLayoutScrollDirection
=
.
Vertical
/**
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.
- Returns: An Array of NSIndexPath objects.
*/
public
func
indexPathsOfItemsInRect
(
rect
:
CGRect
)
->
Array
<
NSIndexPath
>
{
var
paths
:
Array
<
NSIndexPath
>
=
Array
<
NSIndexPath
>
()
for
(
attribute
,
indexPath
)
in
layoutItems
!
{
if
CGRectIntersectsRect
(
rect
,
attribute
.
frame
)
{
paths
.
append
(
indexPath
)
}
}
return
paths
}
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
dataSource
:
MaterialCollectionViewDataSource
=
collectionView
!.
dataSource
as!
MaterialCollectionViewDataSource
let
items
:
Array
<
MaterialCollectionViewDataSourceItem
>
=
dataSource
.
items
()
if
0
==
indexPath
.
row
{
if
0
==
indexPath
.
row
{
offset
=
0
offset
=
0
}
}
let
item
:
MaterialCollectionViewDataSourceItem
=
items
!
[
indexPath
.
row
]
let
item
:
MaterialCollectionViewDataSourceItem
=
items
[
indexPath
.
row
]
if
.
Vertical
==
scrollDirection
{
let
w
:
CGFloat
=
collectionView
!.
bounds
.
width
attributes
.
frame
=
CGRectMake
(
0
,
offset
,
collectionView
!.
bounds
.
width
,
item
.
size
.
height
)
let
h
:
CGFloat
=
item
.
height
offset
+=
item
.
size
.
height
}
else
{
attributes
.
frame
=
CGRectMake
(
0
,
offset
,
w
,
h
)
attributes
.
frame
=
CGRectMake
(
offset
,
0
,
item
.
size
.
width
,
collectionView
!.
bounds
.
height
)
offset
+=
item
.
size
.
width
offset
+=
h
}
return
attributes
return
attributes
}
}
...
@@ -68,7 +98,7 @@ public class MaterialCollectionViewLayout : UICollectionViewLayout {
...
@@ -68,7 +98,7 @@ public class MaterialCollectionViewLayout : UICollectionViewLayout {
}
}
public
override
func
shouldInvalidateLayoutForBoundsChange
(
newBounds
:
CGRect
)
->
Bool
{
public
override
func
shouldInvalidateLayoutForBoundsChange
(
newBounds
:
CGRect
)
->
Bool
{
return
newBounds
.
width
!=
collectionView
!.
bounds
.
width
return
.
Vertical
==
scrollDirection
?
newBounds
.
width
!=
collectionView
!.
bounds
.
width
:
newBounds
.
height
!=
collectionView
!.
bounds
.
height
}
}
public
override
func
collectionViewContentSize
()
->
CGSize
{
public
override
func
collectionViewContentSize
()
->
CGSize
{
...
@@ -77,34 +107,24 @@ public class MaterialCollectionViewLayout : UICollectionViewLayout {
...
@@ -77,34 +107,24 @@ public class MaterialCollectionViewLayout : UICollectionViewLayout {
public
override
func
prepareLayout
()
{
public
override
func
prepareLayout
()
{
let
dataSource
:
MaterialCollectionViewDataSource
=
collectionView
!.
dataSource
as!
MaterialCollectionViewDataSource
let
dataSource
:
MaterialCollectionViewDataSource
=
collectionView
!.
dataSource
as!
MaterialCollectionViewDataSource
let
items
:
Array
<
MaterialCollectionViewDataSourceItem
>
=
dataSource
.
items
()
items
=
dataSource
.
items
()
layoutItems
=
Array
<
(
UICollectionViewLayoutAttributes
,
NSIndexPath
)
>
()
layoutItems
=
Array
<
(
UICollectionViewLayoutAttributes
,
NSIndexPath
)
>
()
offset
=
0
var
indexPath
:
NSIndexPath
?
var
indexPath
:
NSIndexPath
?
var
count
:
Int
=
0
var
count
:
Int
=
0
var
height
:
CGFloat
=
0
for
item
in
items
{
for
item
in
items
!
{
indexPath
=
NSIndexPath
(
forItem
:
count
++
,
inSection
:
0
)
indexPath
=
NSIndexPath
(
forItem
:
count
++
,
inSection
:
0
)
layoutItems
?
.
append
((
layoutAttributesForItemAtIndexPath
(
indexPath
!
)
!
,
indexPath
!
))
layoutItems
?
.
append
((
layoutAttributesForItemAtIndexPath
(
indexPath
!
)
!
,
indexPath
!
))
height
+=
item
.
height
offset
+=
.
Vertical
==
scrollDirection
?
item
.
size
.
height
:
item
.
size
.
width
}
}
let
w
:
CGFloat
=
collectionView
!.
bounds
.
width
contentSize
=
.
Vertical
==
scrollDirection
?
CGSizeMake
(
collectionView
!.
bounds
.
width
,
offset
)
:
CGSizeMake
(
offset
,
collectionView
!.
bounds
.
height
)
contentSize
=
CGSizeMake
(
w
,
CGFloat
(
layoutItems
!.
count
)
*
(
height
+
offset
))
}
}
public
override
func
targetContentOffsetForProposedContentOffset
(
proposedContentOffset
:
CGPoint
)
->
CGPoint
{
public
override
func
targetContentOffsetForProposedContentOffset
(
proposedContentOffset
:
CGPoint
)
->
CGPoint
{
return
proposedContentOffset
return
proposedContentOffset
}
}
internal
func
indexPathsOfItemsInRect
(
rect
:
CGRect
)
->
Array
<
NSIndexPath
>
{
var
paths
:
Array
<
NSIndexPath
>
=
Array
<
NSIndexPath
>
()
for
(
attribute
,
indexPath
)
in
layoutItems
!
{
if
CGRectIntersectsRect
(
rect
,
attribute
.
frame
)
{
paths
.
append
(
indexPath
)
}
}
return
paths
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment