forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.m
More file actions
193 lines (161 loc) · 6.81 KB
/
Copy pathViewController.m
File metadata and controls
193 lines (161 loc) · 6.81 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//
// ViewController.m
// Texture
//
// Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
// Changes after 4/13/2017 are: Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import "ViewController.h"
#import "AppDelegate.h"
#import <AsyncDisplayKit/AsyncDisplayKit.h>
#import "SupplementaryNode.h"
#import "ItemNode.h"
#define ASYNC_COLLECTION_LAYOUT 0
static CGSize const kItemSize = (CGSize){180, 90};
@interface ViewController () <ASCollectionDataSource, ASCollectionDelegateFlowLayout, ASCollectionGalleryLayoutPropertiesProviding>
@property (nonatomic, strong) ASCollectionNode *collectionNode;
@property (nonatomic, strong) NSMutableArray<NSMutableArray<NSString *> *> *data;
@property (nonatomic, strong) UILongPressGestureRecognizer *moveRecognizer;
@end
@implementation ViewController
#pragma mark - Lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.moveRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress)];
[self.view addGestureRecognizer:self.moveRecognizer];
#if ASYNC_COLLECTION_LAYOUT
ASCollectionGalleryLayoutDelegate *layoutDelegate = [[ASCollectionGalleryLayoutDelegate alloc] initWithScrollableDirections:ASScrollDirectionVerticalDirections];
layoutDelegate.propertiesProvider = self;
self.collectionNode = [[ASCollectionNode alloc] initWithLayoutDelegate:layoutDelegate layoutFacilitator:nil];
#else
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.headerReferenceSize = CGSizeMake(50.0, 50.0);
layout.footerReferenceSize = CGSizeMake(50.0, 50.0);
layout.itemSize = kItemSize;
self.collectionNode = [[ASCollectionNode alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
[self.collectionNode registerSupplementaryNodeOfKind:UICollectionElementKindSectionHeader];
[self.collectionNode registerSupplementaryNodeOfKind:UICollectionElementKindSectionFooter];
#endif
self.collectionNode.dataSource = self;
self.collectionNode.delegate = self;
self.collectionNode.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.collectionNode.backgroundColor = [UIColor whiteColor];
[self.view addSubnode:self.collectionNode];
self.collectionNode.frame = self.view.bounds;
#if !SIMULATE_WEB_RESPONSE
self.navigationItem.leftItemsSupplementBackButton = YES;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:@selector(reloadTapped)];
[self loadData];
#else
__weak typeof(self) weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf handleSimulatedWebResponse];
});
#endif
}
- (void)handleSimulatedWebResponse
{
[self.collectionNode performBatchUpdates:^{
[self loadData];
[self.collectionNode insertSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, self.data.count)]];
} completion:nil];
}
- (void)loadData
{
// Form our data array
typeof(self.data) data = [NSMutableArray array];
for (NSInteger s = 0; s < 100; s++) {
NSMutableArray *items = [NSMutableArray array];
for (NSInteger i = 0; i < 10; i++) {
items[i] = [NSString stringWithFormat:@"[%zd.%zd] says hi", s, i];
}
data[s] = items;
}
self.data = data;
}
#pragma mark - Button Actions
- (void)reloadTapped
{
// This method is deprecated because we reccommend using ASCollectionNode instead of ASCollectionView.
// This functionality & example project remains for users who insist on using ASCollectionView.
[self.collectionNode reloadData];
}
#pragma mark - ASCollectionGalleryLayoutPropertiesProviding
- (CGSize)galleryLayoutDelegate:(ASCollectionGalleryLayoutDelegate *)delegate sizeForElements:(ASElementMap *)elements
{
ASDisplayNodeAssertMainThread();
return kItemSize;
}
- (void)handleLongPress
{
UICollectionView *collectionView = self.collectionNode.view;
CGPoint location = [self.moveRecognizer locationInView:collectionView];
switch (self.moveRecognizer.state) {
case UIGestureRecognizerStateBegan: {
NSIndexPath *indexPath = [collectionView indexPathForItemAtPoint:location];
if (indexPath) {
[collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
}
break;
}
case UIGestureRecognizerStateChanged:
[collectionView updateInteractiveMovementTargetPosition:location];
break;
case UIGestureRecognizerStateEnded:
[collectionView endInteractiveMovement];
break;
case UIGestureRecognizerStateFailed:
case UIGestureRecognizerStateCancelled:
[collectionView cancelInteractiveMovement];
break;
case UIGestureRecognizerStatePossible:
// nop
break;
}
}
#pragma mark - ASCollectionDataSource
- (ASCellNodeBlock)collectionNode:(ASCollectionNode *)collectionNode nodeBlockForItemAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = self.data[indexPath.section][indexPath.item];
return ^{
return [[ItemNode alloc] initWithString:text];
};
}
- (ASCellNode *)collectionNode:(ASCollectionNode *)collectionNode nodeForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [kind isEqualToString:UICollectionElementKindSectionHeader] ? @"Header" : @"Footer";
SupplementaryNode *node = [[SupplementaryNode alloc] initWithText:text];
BOOL isHeaderSection = [kind isEqualToString:UICollectionElementKindSectionHeader];
node.backgroundColor = isHeaderSection ? [UIColor blueColor] : [UIColor redColor];
return node;
}
- (NSInteger)collectionNode:(ASCollectionNode *)collectionNode numberOfItemsInSection:(NSInteger)section
{
return self.data[section].count;
}
- (NSInteger)numberOfSectionsInCollectionNode:(ASCollectionNode *)collectionNode
{
return self.data.count;
}
- (BOOL)collectionNode:(ASCollectionNode *)collectionNode canMoveItemWithNode:(ASCellNode *)node
{
return YES;
}
- (void)collectionNode:(ASCollectionNode *)collectionNode moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
__auto_type sectionArray = self.data[sourceIndexPath.section];
__auto_type object = sectionArray[sourceIndexPath.item];
[sectionArray removeObjectAtIndex:sourceIndexPath.item];
[self.data[destinationIndexPath.section] insertObject:object atIndex:destinationIndexPath.item];
}
#pragma mark - ASCollectionDelegate
- (void)collectionNode:(ASCollectionNode *)collectionNode willBeginBatchFetchWithContext:(ASBatchContext *)context
{
NSLog(@"fetch additional content");
[context completeBatchFetching:YES];
}
@end