Add custom collection view layout example project
18
examples/CustomCollectionView/Sample/AppDelegate.h
Normal file
@@ -0,0 +1,18 @@
|
||||
/* This file provided by Facebook is for non-commercial testing and evaluation
|
||||
* purposes only. Facebook reserves all rights not expressly granted.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface AppDelegate : UIResponder <UIApplicationDelegate>
|
||||
|
||||
@property (strong, nonatomic) UIWindow *window;
|
||||
|
||||
@end
|
||||
29
examples/CustomCollectionView/Sample/AppDelegate.m
Normal file
@@ -0,0 +1,29 @@
|
||||
/* This file provided by Facebook is for non-commercial testing and evaluation
|
||||
* purposes only. Facebook reserves all rights not expressly granted.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#import "AppDelegate.h"
|
||||
|
||||
#import "ViewController.h"
|
||||
|
||||
@implementation AppDelegate
|
||||
|
||||
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
|
||||
{
|
||||
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
|
||||
self.window.backgroundColor = [UIColor whiteColor];
|
||||
self.window.rootViewController = [[ViewController alloc] init];
|
||||
|
||||
[self.window makeKeyAndVisible];
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
@end
|
||||
15
examples/CustomCollectionView/Sample/ImageCellNode.h
Normal file
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// ImageCellNode.h
|
||||
// Sample
|
||||
//
|
||||
// Created by McCallum, Levi on 11/22/15.
|
||||
// Copyright (c) 2015 Facebook. All rights reserved.
|
||||
//
|
||||
|
||||
#import <AsyncDisplayKit/AsyncDisplayKit.h>
|
||||
|
||||
@interface ImageCellNode : ASCellNode
|
||||
|
||||
- (instancetype)initWithImage:(UIImage *)image;
|
||||
|
||||
@end
|
||||
37
examples/CustomCollectionView/Sample/ImageCellNode.m
Normal file
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// ImageCellNode.m
|
||||
// Sample
|
||||
//
|
||||
// Created by McCallum, Levi on 11/22/15.
|
||||
// Copyright (c) 2015 Facebook. All rights reserved.
|
||||
//
|
||||
|
||||
#import "ImageCellNode.h"
|
||||
|
||||
@implementation ImageCellNode {
|
||||
ASImageNode *_imageNode;
|
||||
}
|
||||
|
||||
- (id)initWithImage:(UIImage *)image
|
||||
{
|
||||
self = [super init];
|
||||
if (self != nil) {
|
||||
_imageNode = [[ASImageNode alloc] init];
|
||||
_imageNode.image = image;
|
||||
[self addSubnode:_imageNode];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (CGSize)calculateSizeThatFits:(CGSize)constrainedSize
|
||||
{
|
||||
[_imageNode measure:constrainedSize];
|
||||
return constrainedSize;
|
||||
}
|
||||
|
||||
- (void)layout
|
||||
{
|
||||
_imageNode.frame = CGRectMake(0, 0, _imageNode.calculatedSize.width, _imageNode.calculatedSize.height);
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"orientation" : "portrait",
|
||||
"idiom" : "iphone",
|
||||
"filename" : "Default-568h@2x.png",
|
||||
"minimum-system-version" : "7.0",
|
||||
"subtype" : "retina4",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"scale" : "1x",
|
||||
"orientation" : "portrait"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"scale" : "2x",
|
||||
"orientation" : "portrait"
|
||||
},
|
||||
{
|
||||
"orientation" : "portrait",
|
||||
"idiom" : "iphone",
|
||||
"filename" : "Default-568h@2x.png",
|
||||
"subtype" : "retina4",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"orientation" : "portrait",
|
||||
"idiom" : "iphone",
|
||||
"minimum-system-version" : "7.0",
|
||||
"scale" : "2x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
|
After Width: | Height: | Size: 17 KiB |
21
examples/CustomCollectionView/Sample/Images.xcassets/image_0.imageset/Contents.json
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "1x",
|
||||
"filename" : "image_0.jpg"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "3x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
BIN
examples/CustomCollectionView/Sample/Images.xcassets/image_0.imageset/image_0.jpg
vendored
Normal file
|
After Width: | Height: | Size: 1.2 MiB |
21
examples/CustomCollectionView/Sample/Images.xcassets/image_1.imageset/Contents.json
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "1x",
|
||||
"filename" : "image_1.jpg"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "3x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
BIN
examples/CustomCollectionView/Sample/Images.xcassets/image_1.imageset/image_1.jpg
vendored
Normal file
|
After Width: | Height: | Size: 114 KiB |
21
examples/CustomCollectionView/Sample/Images.xcassets/image_10.imageset/Contents.json
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "1x",
|
||||
"filename" : "image_10.jpg"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "3x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
BIN
examples/CustomCollectionView/Sample/Images.xcassets/image_10.imageset/image_10.jpg
vendored
Normal file
|
After Width: | Height: | Size: 516 KiB |
21
examples/CustomCollectionView/Sample/Images.xcassets/image_11.imageset/Contents.json
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "1x",
|
||||
"filename" : "image_11.jpg"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "3x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
BIN
examples/CustomCollectionView/Sample/Images.xcassets/image_11.imageset/image_11.jpg
vendored
Normal file
|
After Width: | Height: | Size: 434 KiB |
21
examples/CustomCollectionView/Sample/Images.xcassets/image_12.imageset/Contents.json
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "1x",
|
||||
"filename" : "image_12.jpg"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "3x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
BIN
examples/CustomCollectionView/Sample/Images.xcassets/image_12.imageset/image_12.jpg
vendored
Normal file
|
After Width: | Height: | Size: 78 KiB |
21
examples/CustomCollectionView/Sample/Images.xcassets/image_13.imageset/Contents.json
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "1x",
|
||||
"filename" : "image_13.jpg"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "3x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
BIN
examples/CustomCollectionView/Sample/Images.xcassets/image_13.imageset/image_13.jpg
vendored
Normal file
|
After Width: | Height: | Size: 168 KiB |
21
examples/CustomCollectionView/Sample/Images.xcassets/image_2.imageset/Contents.json
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "1x",
|
||||
"filename" : "image_2.jpg"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "3x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
BIN
examples/CustomCollectionView/Sample/Images.xcassets/image_2.imageset/image_2.jpg
vendored
Normal file
|
After Width: | Height: | Size: 540 KiB |
21
examples/CustomCollectionView/Sample/Images.xcassets/image_3.imageset/Contents.json
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "1x",
|
||||
"filename" : "image_3.jpg"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "3x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
BIN
examples/CustomCollectionView/Sample/Images.xcassets/image_3.imageset/image_3.jpg
vendored
Normal file
|
After Width: | Height: | Size: 1.1 MiB |
21
examples/CustomCollectionView/Sample/Images.xcassets/image_4.imageset/Contents.json
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "1x",
|
||||
"filename" : "image_4.jpg"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "3x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
BIN
examples/CustomCollectionView/Sample/Images.xcassets/image_4.imageset/image_4.jpg
vendored
Normal file
|
After Width: | Height: | Size: 111 KiB |
21
examples/CustomCollectionView/Sample/Images.xcassets/image_5.imageset/Contents.json
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "1x",
|
||||
"filename" : "image_5.jpg"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "3x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
BIN
examples/CustomCollectionView/Sample/Images.xcassets/image_5.imageset/image_5.jpg
vendored
Normal file
|
After Width: | Height: | Size: 892 KiB |
21
examples/CustomCollectionView/Sample/Images.xcassets/image_6.imageset/Contents.json
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "1x",
|
||||
"filename" : "image_6.jpg"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "3x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
BIN
examples/CustomCollectionView/Sample/Images.xcassets/image_6.imageset/image_6.jpg
vendored
Normal file
|
After Width: | Height: | Size: 494 KiB |
21
examples/CustomCollectionView/Sample/Images.xcassets/image_7.imageset/Contents.json
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "1x",
|
||||
"filename" : "image_7.jpg"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "3x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
BIN
examples/CustomCollectionView/Sample/Images.xcassets/image_7.imageset/image_7.jpg
vendored
Normal file
|
After Width: | Height: | Size: 250 KiB |
21
examples/CustomCollectionView/Sample/Images.xcassets/image_8.imageset/Contents.json
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "1x",
|
||||
"filename" : "image_8.jpg"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "3x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
BIN
examples/CustomCollectionView/Sample/Images.xcassets/image_8.imageset/image_8.jpg
vendored
Normal file
|
After Width: | Height: | Size: 98 KiB |
21
examples/CustomCollectionView/Sample/Images.xcassets/image_9.imageset/Contents.json
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "1x",
|
||||
"filename" : "image_9.jpg"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"scale" : "3x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
BIN
examples/CustomCollectionView/Sample/Images.xcassets/image_9.imageset/image_9.jpg
vendored
Normal file
|
After Width: | Height: | Size: 429 KiB |
49
examples/CustomCollectionView/Sample/Info.plist
Normal file
@@ -0,0 +1,49 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>en</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleIcons</key>
|
||||
<dict/>
|
||||
<key>CFBundleIcons~ipad</key>
|
||||
<dict/>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>com.facebook.AsyncDisplayKit.$(PRODUCT_NAME:rfc1034identifier)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>$(PRODUCT_NAME)</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>APPL</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
<key>LSRequiresIPhoneOS</key>
|
||||
<true/>
|
||||
<key>UILaunchStoryboardName</key>
|
||||
<string>Launchboard</string>
|
||||
<key>UIRequiredDeviceCapabilities</key>
|
||||
<array>
|
||||
<string>armv7</string>
|
||||
</array>
|
||||
<key>UISupportedInterfaceOrientations</key>
|
||||
<array>
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
<string>UIInterfaceOrientationLandscapeLeft</string>
|
||||
<string>UIInterfaceOrientationLandscapeRight</string>
|
||||
</array>
|
||||
<key>UISupportedInterfaceOrientations~ipad</key>
|
||||
<array>
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
<string>UIInterfaceOrientationPortraitUpsideDown</string>
|
||||
<string>UIInterfaceOrientationLandscapeLeft</string>
|
||||
<string>UIInterfaceOrientationLandscapeRight</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
|
||||
<dependencies>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6204"/>
|
||||
</dependencies>
|
||||
<scenes/>
|
||||
</document>
|
||||
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// MosaicCollectionViewLayout.h
|
||||
// Sample
|
||||
//
|
||||
// Created by McCallum, Levi on 11/22/15.
|
||||
// Copyright (c) 2015 Facebook. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import <AsyncDisplayKit/AsyncDisplayKit.h>
|
||||
|
||||
@interface MosaicCollectionViewLayout : UICollectionViewLayout
|
||||
|
||||
@property (assign, nonatomic) NSUInteger numberOfColumns;
|
||||
@property (assign, nonatomic) CGFloat columnSpacing;
|
||||
@property (assign, nonatomic) UIEdgeInsets sectionInset;
|
||||
@property (assign, nonatomic) UIEdgeInsets interItemSpacing;
|
||||
@property (assign, nonatomic) CGFloat headerHeight;
|
||||
|
||||
@end
|
||||
|
||||
@protocol MosaicCollectionViewLayoutDelegate <ASCollectionViewDelegate>
|
||||
|
||||
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(MosaicCollectionViewLayout *)layout originalItemSizeAtIndexPath:(NSIndexPath *)indexPath;
|
||||
|
||||
@end
|
||||
|
||||
@interface MosaicCollectionViewLayoutInspector : NSObject <ASCollectionViewLayoutInspecting>
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,230 @@
|
||||
//
|
||||
// MosaicCollectionViewLayout.m
|
||||
// Sample
|
||||
//
|
||||
// Created by McCallum, Levi on 11/22/15.
|
||||
// Copyright (c) 2015 Facebook. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MosaicCollectionViewLayout.h"
|
||||
|
||||
@implementation MosaicCollectionViewLayout {
|
||||
NSMutableArray *_columnHeights;
|
||||
NSMutableArray *_itemAttributes;
|
||||
NSMutableDictionary *_headerAttributes;
|
||||
NSMutableArray *_allAttributes;
|
||||
}
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
self = [super init];
|
||||
if (self != nil) {
|
||||
self.numberOfColumns = 3;
|
||||
self.columnSpacing = 10.0;
|
||||
self.sectionInset = UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0);
|
||||
self.interItemSpacing = UIEdgeInsetsMake(10.0, 0, 10.0, 0);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)prepareLayout
|
||||
{
|
||||
_itemAttributes = [NSMutableArray array];
|
||||
_columnHeights = [NSMutableArray array];
|
||||
_allAttributes = [NSMutableArray array];
|
||||
_headerAttributes = [NSMutableDictionary dictionary];
|
||||
|
||||
CGFloat top = 0;
|
||||
|
||||
NSInteger numberOfSections = [self.collectionView numberOfSections];
|
||||
for (NSUInteger section = 0; section < numberOfSections; section++) {
|
||||
NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:section];
|
||||
|
||||
top += _sectionInset.top;
|
||||
|
||||
if (_headerHeight > 0) {
|
||||
CGSize headerSize = [self _headerSizeForSection:section];
|
||||
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes
|
||||
layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader
|
||||
withIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];
|
||||
attributes.frame = CGRectMake(_sectionInset.left, top, headerSize.width, headerSize.height);
|
||||
_headerAttributes[@(section)] = attributes;
|
||||
[_allAttributes addObject:attributes];
|
||||
top = CGRectGetMaxY(attributes.frame);
|
||||
}
|
||||
|
||||
[_columnHeights addObject:[NSMutableArray array]];
|
||||
for (NSUInteger idx = 0; idx < self.numberOfColumns; idx++) {
|
||||
[_columnHeights[section] addObject:@(top)];
|
||||
}
|
||||
|
||||
CGFloat columnWidth = [self _columnWidthForSection:section];
|
||||
[_itemAttributes addObject:[NSMutableArray array]];
|
||||
for (NSUInteger idx = 0; idx < numberOfItems; idx++) {
|
||||
NSUInteger columnIndex = [self _shortestColumnIndexInSection:section];
|
||||
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:idx inSection:section];
|
||||
|
||||
CGSize itemSize = [self _itemSizeAtIndexPath:indexPath];
|
||||
CGFloat xOffset = _sectionInset.left + (columnWidth + _columnSpacing) * columnIndex;
|
||||
CGFloat yOffset = [_columnHeights[section][columnIndex] floatValue];
|
||||
|
||||
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes
|
||||
layoutAttributesForCellWithIndexPath:indexPath];
|
||||
attributes.frame = CGRectMake(xOffset, yOffset, itemSize.width, itemSize.height);
|
||||
|
||||
_columnHeights[section][columnIndex] = @(CGRectGetMaxY(attributes.frame) + _interItemSpacing.bottom);
|
||||
|
||||
[_itemAttributes[section] addObject:attributes];
|
||||
[_allAttributes addObject:attributes];
|
||||
}
|
||||
|
||||
NSUInteger columnIndex = [self _tallestColumnIndexInSection:section];
|
||||
top = [_columnHeights[section][columnIndex] floatValue] - _interItemSpacing.bottom + _sectionInset.bottom;
|
||||
|
||||
for (NSUInteger idx = 0; idx < [_columnHeights[section] count]; idx++) {
|
||||
_columnHeights[section][idx] = @(top);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
|
||||
{
|
||||
NSMutableArray *includedAttributes = [NSMutableArray array];
|
||||
// Slow search for small batches
|
||||
for (UICollectionViewLayoutAttributes *attributes in _allAttributes) {
|
||||
if (CGRectIntersectsRect(attributes.frame, rect)) {
|
||||
[includedAttributes addObject:attributes];
|
||||
}
|
||||
}
|
||||
return includedAttributes;
|
||||
}
|
||||
|
||||
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
if (indexPath.section >= _itemAttributes.count) {
|
||||
return nil;
|
||||
} else if (indexPath.item >= [_itemAttributes[indexPath.section] count]) {
|
||||
return nil;
|
||||
}
|
||||
return _itemAttributes[indexPath.section][indexPath.item];
|
||||
}
|
||||
|
||||
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
if ([elementKind isEqualToString:UICollectionElementKindSectionHeader]) {
|
||||
return _headerAttributes[@(indexPath.section)];
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
|
||||
{
|
||||
if (!CGRectEqualToRect(self.collectionView.bounds, newBounds)) {
|
||||
return YES;
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (CGFloat)_widthForSection:(NSUInteger)section
|
||||
{
|
||||
return self.collectionView.bounds.size.width - _sectionInset.left - _sectionInset.right;
|
||||
}
|
||||
|
||||
- (CGFloat)_columnWidthForSection:(NSUInteger)section
|
||||
{
|
||||
return ([self _widthForSection:section] - ((_numberOfColumns - 1) * _columnSpacing)) / _numberOfColumns;
|
||||
}
|
||||
|
||||
- (CGSize)_itemSizeAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
CGSize size = CGSizeMake([self _columnWidthForSection:indexPath.section], 0);
|
||||
CGSize originalSize = [[self _delegate] collectionView:self.collectionView layout:self originalItemSizeAtIndexPath:indexPath];
|
||||
if (originalSize.height > 0 && originalSize.width > 0) {
|
||||
size.height = originalSize.height / originalSize.width * size.width;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
- (CGSize)_headerSizeForSection:(NSUInteger)section
|
||||
{
|
||||
return CGSizeMake([self _widthForSection:section], _headerHeight);
|
||||
}
|
||||
|
||||
- (CGSize)collectionViewContentSize
|
||||
{
|
||||
CGFloat height = [[[_columnHeights lastObject] firstObject] floatValue];
|
||||
return CGSizeMake(self.collectionView.bounds.size.width, height);
|
||||
}
|
||||
|
||||
- (NSUInteger)_tallestColumnIndexInSection:(NSUInteger)section
|
||||
{
|
||||
__block NSUInteger index = 0;
|
||||
__block CGFloat tallestHeight = 0;
|
||||
[_columnHeights[section] enumerateObjectsUsingBlock:^(NSNumber *height, NSUInteger idx, BOOL *stop) {
|
||||
if (height.floatValue > tallestHeight) {
|
||||
index = idx;
|
||||
tallestHeight = height.floatValue;
|
||||
}
|
||||
}];
|
||||
return index;
|
||||
}
|
||||
|
||||
- (NSUInteger)_shortestColumnIndexInSection:(NSUInteger)section
|
||||
{
|
||||
__block NSUInteger index = 0;
|
||||
__block CGFloat shortestHeight = CGFLOAT_MAX;
|
||||
[_columnHeights[section] enumerateObjectsUsingBlock:^(NSNumber *height, NSUInteger idx, BOOL *stop) {
|
||||
if (height.floatValue < shortestHeight) {
|
||||
index = idx;
|
||||
shortestHeight = height.floatValue;
|
||||
}
|
||||
}];
|
||||
return index;
|
||||
}
|
||||
|
||||
- (id<MosaicCollectionViewLayoutDelegate>)_delegate
|
||||
{
|
||||
return (id<MosaicCollectionViewLayoutDelegate>)self.collectionView.delegate;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation MosaicCollectionViewLayoutInspector
|
||||
|
||||
- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
MosaicCollectionViewLayout *layout = (MosaicCollectionViewLayout *)[collectionView collectionViewLayout];
|
||||
return ASSizeRangeMake(CGSizeZero, [layout _itemSizeAtIndexPath:indexPath]);
|
||||
}
|
||||
|
||||
- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
MosaicCollectionViewLayout *layout = (MosaicCollectionViewLayout *)[collectionView collectionViewLayout];
|
||||
return ASSizeRangeMake(CGSizeZero, [layout _headerSizeForSection:indexPath.section]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asks the inspector for the number of supplementary sections in the collection view for the given kind.
|
||||
*/
|
||||
- (NSUInteger)collectionView:(ASCollectionView *)collectionView numberOfSectionsForSupplementaryNodeOfKind:(NSString *)kind
|
||||
{
|
||||
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
|
||||
return [[collectionView asyncDataSource] numberOfSectionsInCollectionView:collectionView];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asks the inspector for the number of supplementary views for the given kind in the specified section.
|
||||
*/
|
||||
- (NSUInteger)collectionView:(ASCollectionView *)collectionView supplementaryNodesOfKind:(NSString *)kind inSection:(NSUInteger)section
|
||||
{
|
||||
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
18
examples/CustomCollectionView/Sample/SupplementaryNode.h
Normal file
@@ -0,0 +1,18 @@
|
||||
/* This file provided by Facebook is for non-commercial testing and evaluation
|
||||
* purposes only. Facebook reserves all rights not expressly granted.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#import <AsyncDisplayKit/AsyncDisplayKit.h>
|
||||
|
||||
@interface SupplementaryNode : ASCellNode
|
||||
|
||||
- (instancetype)initWithText:(NSString *)text;
|
||||
|
||||
@end
|
||||
52
examples/CustomCollectionView/Sample/SupplementaryNode.m
Normal file
@@ -0,0 +1,52 @@
|
||||
/* This file provided by Facebook is for non-commercial testing and evaluation
|
||||
* purposes only. Facebook reserves all rights not expressly granted.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#import "SupplementaryNode.h"
|
||||
|
||||
#import <AsyncDisplayKit/ASDisplayNode+Subclasses.h>
|
||||
#import <AsyncDisplayKit/ASInsetLayoutSpec.h>
|
||||
#import <AsyncDisplayKit/ASCenterLayoutSpec.h>
|
||||
|
||||
@implementation SupplementaryNode {
|
||||
ASTextNode *_textNode;
|
||||
}
|
||||
|
||||
- (instancetype)initWithText:(NSString *)text
|
||||
{
|
||||
self = [super init];
|
||||
if (self != nil) {
|
||||
_textNode = [[ASTextNode alloc] init];
|
||||
_textNode.attributedString = [[NSAttributedString alloc] initWithString:text
|
||||
attributes:[self textAttributes]];
|
||||
[self addSubnode:_textNode];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
|
||||
{
|
||||
ASCenterLayoutSpec *center = [[ASCenterLayoutSpec alloc] init];
|
||||
center.centeringOptions = ASCenterLayoutSpecCenteringY;
|
||||
center.child = _textNode;
|
||||
return center;
|
||||
}
|
||||
|
||||
#pragma mark - Text Formatting
|
||||
|
||||
- (NSDictionary *)textAttributes
|
||||
{
|
||||
return @{
|
||||
NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline],
|
||||
NSForegroundColorAttributeName: [UIColor grayColor],
|
||||
};
|
||||
}
|
||||
|
||||
@end
|
||||
16
examples/CustomCollectionView/Sample/ViewController.h
Normal file
@@ -0,0 +1,16 @@
|
||||
/* This file provided by Facebook is for non-commercial testing and evaluation
|
||||
* purposes only. Facebook reserves all rights not expressly granted.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface ViewController : UIViewController
|
||||
|
||||
@end
|
||||
117
examples/CustomCollectionView/Sample/ViewController.m
Normal file
@@ -0,0 +1,117 @@
|
||||
/* This file provided by Facebook is for non-commercial testing and evaluation
|
||||
* purposes only. Facebook reserves all rights not expressly granted.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#import "ViewController.h"
|
||||
|
||||
#import <AsyncDisplayKit/AsyncDisplayKit.h>
|
||||
#import "MosaicCollectionViewLayout.h"
|
||||
#import "SupplementaryNode.h"
|
||||
#import "ImageCellNode.h"
|
||||
|
||||
static NSUInteger kNumberOfImages = 14;
|
||||
|
||||
@interface ViewController () <ASCollectionViewDataSource, MosaicCollectionViewLayoutDelegate>
|
||||
{
|
||||
NSMutableArray *_sections;
|
||||
ASCollectionView *_collectionView;
|
||||
MosaicCollectionViewLayoutInspector *_layoutInspector;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation ViewController
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark UIViewController.
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
if (!(self = [super init]))
|
||||
return nil;
|
||||
|
||||
_sections = [NSMutableArray array];
|
||||
[_sections addObject:[NSMutableArray array]];
|
||||
for (NSUInteger idx = 0, section = 0; idx < kNumberOfImages; idx++) {
|
||||
NSString *name = [NSString stringWithFormat:@"image_%lu.jpg", (unsigned long)idx];
|
||||
[_sections[section] addObject:[UIImage imageNamed:name]];
|
||||
if ((idx + 1) % 5 == 0 && idx < kNumberOfImages - 1) {
|
||||
section++;
|
||||
[_sections addObject:[NSMutableArray array]];
|
||||
}
|
||||
}
|
||||
|
||||
MosaicCollectionViewLayout *layout = [[MosaicCollectionViewLayout alloc] init];
|
||||
layout.numberOfColumns = 2;
|
||||
layout.headerHeight = 44.0;
|
||||
|
||||
_layoutInspector = [[MosaicCollectionViewLayoutInspector alloc] init];
|
||||
|
||||
_collectionView = [[ASCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout asyncDataFetching:YES];
|
||||
_collectionView.asyncDataSource = self;
|
||||
_collectionView.asyncDelegate = self;
|
||||
_collectionView.layoutInspector = _layoutInspector;
|
||||
_collectionView.backgroundColor = [UIColor whiteColor];
|
||||
|
||||
[_collectionView registerSupplementaryNodeOfKind:UICollectionElementKindSectionHeader];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)viewDidLoad
|
||||
{
|
||||
[super viewDidLoad];
|
||||
|
||||
[self.view addSubview:_collectionView];
|
||||
}
|
||||
|
||||
- (void)viewWillLayoutSubviews
|
||||
{
|
||||
_collectionView.frame = self.view.bounds;
|
||||
}
|
||||
|
||||
- (BOOL)prefersStatusBarHidden
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (void)reloadTapped
|
||||
{
|
||||
[_collectionView reloadData];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark ASCollectionView data source.
|
||||
|
||||
- (ASCellNode *)collectionView:(ASCollectionView *)collectionView nodeForItemAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
return [[ImageCellNode alloc] initWithImage:_sections[indexPath.section][indexPath.item]];
|
||||
}
|
||||
|
||||
- (ASCellNode *)collectionView:(ASCollectionView *)collectionView nodeForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
NSString *text = [NSString stringWithFormat:@"Section %d", (int)indexPath.section + 1];
|
||||
return [[SupplementaryNode alloc] initWithText:text];
|
||||
}
|
||||
|
||||
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
|
||||
return _sections.count;
|
||||
}
|
||||
|
||||
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
|
||||
return [_sections[section] count];
|
||||
}
|
||||
|
||||
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout originalItemSizeAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
return [(UIImage *)_sections[indexPath.section][indexPath.item] size];
|
||||
}
|
||||
|
||||
@end
|
||||
19
examples/CustomCollectionView/Sample/main.m
Normal file
@@ -0,0 +1,19 @@
|
||||
/* This file provided by Facebook is for non-commercial testing and evaluation
|
||||
* purposes only. Facebook reserves all rights not expressly granted.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import "AppDelegate.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
@autoreleasepool {
|
||||
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
|
||||
}
|
||||
}
|
||||