Skip to content

Commit

Permalink
- added layouts for cells
Browse files Browse the repository at this point in the history
- added collection view & PinterestVC
- added images
  • Loading branch information
KhrystynaShevchuk committed Jul 4, 2017
1 parent 77f05ba commit be746a2
Show file tree
Hide file tree
Showing 77 changed files with 1,140 additions and 51 deletions.
50 changes: 42 additions & 8 deletions PinterestLayout.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
objects = {

/* Begin PBXBuildFile section */
640095F71F0BC37F006AE391 /* CollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 640095F61F0BC37F006AE391 /* CollectionViewCell.swift */; };
644F27AA1F0BF039004AD5AD /* RoundedCornersView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 644F27A91F0BF039004AD5AD /* RoundedCornersView.swift */; };
B5AFCBAC1F0BB16F00CB71B5 /* PinterestLayout.h in Headers */ = {isa = PBXBuildFile; fileRef = B5AFCBAA1F0BB16F00CB71B5 /* PinterestLayout.h */; settings = {ATTRIBUTES = (Public, ); }; };
B5CF9EDE1F0BB19500DAA3B7 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CF9EDD1F0BB19500DAA3B7 /* AppDelegate.swift */; };
B5CF9EE01F0BB19500DAA3B7 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CF9EDF1F0BB19500DAA3B7 /* ViewController.swift */; };
B5CF9EE01F0BB19500DAA3B7 /* PinterestVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CF9EDF1F0BB19500DAA3B7 /* PinterestVC.swift */; };
B5CF9EE31F0BB19500DAA3B7 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B5CF9EE11F0BB19500DAA3B7 /* Main.storyboard */; };
B5CF9EE51F0BB19500DAA3B7 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B5CF9EE41F0BB19500DAA3B7 /* Assets.xcassets */; };
B5CF9EE81F0BB19500DAA3B7 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B5CF9EE61F0BB19500DAA3B7 /* LaunchScreen.storyboard */; };
Expand Down Expand Up @@ -43,14 +45,16 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
640095F61F0BC37F006AE391 /* CollectionViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionViewCell.swift; sourceTree = "<group>"; };
644F27A91F0BF039004AD5AD /* RoundedCornersView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RoundedCornersView.swift; sourceTree = "<group>"; };
B5AFCBA71F0BB16F00CB71B5 /* PinterestLayout.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = PinterestLayout.framework; sourceTree = BUILT_PRODUCTS_DIR; };
B5AFCBAA1F0BB16F00CB71B5 /* PinterestLayout.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PinterestLayout.h; sourceTree = "<group>"; };
B5AFCBAB1F0BB16F00CB71B5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
B5CF9EDB1F0BB19500DAA3B7 /* PinterestLayoutExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PinterestLayoutExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
B5CF9EDD1F0BB19500DAA3B7 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
B5CF9EDF1F0BB19500DAA3B7 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
B5CF9EDF1F0BB19500DAA3B7 /* PinterestVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PinterestVC.swift; sourceTree = "<group>"; };
B5CF9EE21F0BB19500DAA3B7 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
B5CF9EE41F0BB19500DAA3B7 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
B5CF9EE41F0BB19500DAA3B7 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Assets.xcassets; path = PinterestLayoutExample/Assets.xcassets; sourceTree = SOURCE_ROOT; };
B5CF9EE71F0BB19500DAA3B7 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
B5CF9EE91F0BB19500DAA3B7 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
B5CF9EF21F0BB1D200DAA3B7 /* PinterestLayout.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinterestLayout.swift; sourceTree = "<group>"; };
Expand All @@ -75,6 +79,34 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
640095F41F0BC340006AE391 /* UI */ = {
isa = PBXGroup;
children = (
640095F51F0BC34D006AE391 /* Views */,
B5CF9EE11F0BB19500DAA3B7 /* Main.storyboard */,
B5CF9EE61F0BB19500DAA3B7 /* LaunchScreen.storyboard */,
);
name = UI;
sourceTree = "<group>";
};
640095F51F0BC34D006AE391 /* Views */ = {
isa = PBXGroup;
children = (
640095F61F0BC37F006AE391 /* CollectionViewCell.swift */,
644F27A91F0BF039004AD5AD /* RoundedCornersView.swift */,
);
name = Views;
sourceTree = "<group>";
};
640095F81F0BC3E8006AE391 /* Recourses */ = {
isa = PBXGroup;
children = (
B5CF9EE41F0BB19500DAA3B7 /* Assets.xcassets */,
);
name = Recourses;
path = ../PinterestLayout;
sourceTree = "<group>";
};
B5AFCB9D1F0BB16F00CB71B5 = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -107,10 +139,9 @@
isa = PBXGroup;
children = (
B5CF9EDD1F0BB19500DAA3B7 /* AppDelegate.swift */,
B5CF9EDF1F0BB19500DAA3B7 /* ViewController.swift */,
B5CF9EE11F0BB19500DAA3B7 /* Main.storyboard */,
B5CF9EE41F0BB19500DAA3B7 /* Assets.xcassets */,
B5CF9EE61F0BB19500DAA3B7 /* LaunchScreen.storyboard */,
B5CF9EDF1F0BB19500DAA3B7 /* PinterestVC.swift */,
640095F41F0BC340006AE391 /* UI */,
640095F81F0BC3E8006AE391 /* Recourses */,
B5CF9EE91F0BB19500DAA3B7 /* Info.plist */,
);
path = PinterestLayoutExample;
Expand Down Expand Up @@ -242,8 +273,10 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
B5CF9EE01F0BB19500DAA3B7 /* ViewController.swift in Sources */,
B5CF9EE01F0BB19500DAA3B7 /* PinterestVC.swift in Sources */,
B5CF9EDE1F0BB19500DAA3B7 /* AppDelegate.swift in Sources */,
640095F71F0BC37F006AE391 /* CollectionViewCell.swift in Sources */,
644F27AA1F0BF039004AD5AD /* RoundedCornersView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -479,6 +512,7 @@
B5CF9EEB1F0BB19500DAA3B7 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
125 changes: 125 additions & 0 deletions PinterestLayout/PinterestLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,131 @@
import UIKit


//MARK: CollectionViewLayoutDelegate

public protocol CollectionViewLayoutDelegate {

func collectionView(collectionView: UICollectionView, heightForImageAtIndexPath indexPath: NSIndexPath, withWidth: CGFloat) -> CGFloat

func collectionView(collectionView: UICollectionView, heightForAnnotationAtIndexPath indexPath: NSIndexPath, withWidth: CGFloat) -> CGFloat
}


//MARK: CollectionViewLayoutAttributes

public class CollectionViewLayoutAttributes: UICollectionViewLayoutAttributes {

public var imageHeight: CGFloat = 0

override public func copy(with zone: NSZone? = nil) -> Any {
let copy = super.copy(with: zone) as! CollectionViewLayoutAttributes
copy.imageHeight = imageHeight
return copy
}

override public func isEqual(_ object: Any?) -> Bool {
if let attributes = object as? CollectionViewLayoutAttributes {
if attributes.imageHeight == imageHeight {
return super.isEqual(object)
}
}
return false
}
}


//MARK: CollectionViewLayout

public class PinterestLayout: UICollectionViewLayout {

public var delegate: CollectionViewLayoutDelegate!
public var numberOfColumns: Int = 1
public var cellPadding: CGFloat = 0
public var numberOfSections: Int = 1
var insets = UIEdgeInsets.zero

private var cache = [CollectionViewLayoutAttributes]()
private var contentHeight: CGFloat = 0
private var width: CGFloat {
get {
insets = collectionView!.contentInset
let bounds = collectionView!.bounds
return bounds.width - insets.left - insets.right
}
}

override public var collectionViewContentSize: CGSize {
get {
return CGSize(width: width, height: contentHeight)
}
}

override public class var layoutAttributesClass: AnyClass {
return CollectionViewLayoutAttributes.self
}

override public func prepare() {

if cache.isEmpty {
let collumnWidth = width / CGFloat(numberOfColumns)
let cellWidth = collumnWidth - (cellPadding * 2)

var xOffsets = [CGFloat]()

for collumn in 0..<numberOfColumns {
xOffsets.append(CGFloat(collumn) * collumnWidth)
}

var yOffsets = [CGFloat](repeating: 0, count: numberOfColumns)
for section in 0..<numberOfSections {

var column = 0
var oneColumn = Int(collectionView!.numberOfItems(inSection: section)/numberOfColumns)
if collectionView!.numberOfItems(inSection: section) % numberOfColumns != 0 {
oneColumn += 1
}
for item in 0..<collectionView!.numberOfItems(inSection: section) {

let indexPath = IndexPath(item: item, section: section)

column = Int(item/(oneColumn))

let imageHeight = delegate.collectionView(collectionView: collectionView!, heightForImageAtIndexPath: indexPath as NSIndexPath, withWidth: cellWidth)
let annotationHeight = delegate.collectionView(collectionView: collectionView!, heightForAnnotationAtIndexPath: indexPath as NSIndexPath, withWidth: cellWidth)
let cellHeight = cellPadding + imageHeight + annotationHeight + cellPadding

let frame = CGRect(
x: xOffsets[column],
y: yOffsets[column],
width: collumnWidth,
height: cellHeight)
let insetFrame = UIEdgeInsetsInsetRect(frame, insets)

let attributes = CollectionViewLayoutAttributes(
forCellWith: indexPath
)
attributes.frame = insetFrame
attributes.imageHeight = imageHeight
cache.append(attributes)

let cgrectFrame = frame
contentHeight = max(contentHeight, cgrectFrame.maxY)
yOffsets[column] = yOffsets[column] + cellHeight
}
}
}
}

override public func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

var layoutAttributes = [UICollectionViewLayoutAttributes]()

for attributes in cache {
if attributes.frame.intersects(rect) {
layoutAttributes.append(attributes)
}
}

return layoutAttributes
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions PinterestLayoutExample/Assets.xcassets/10.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions PinterestLayoutExample/Assets.xcassets/11.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions PinterestLayoutExample/Assets.xcassets/12.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions PinterestLayoutExample/Assets.xcassets/13.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions PinterestLayoutExample/Assets.xcassets/2.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions PinterestLayoutExample/Assets.xcassets/3.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions PinterestLayoutExample/Assets.xcassets/4.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit be746a2

Please sign in to comment.