Improve PagerNode sample project

This commit is contained in:
Michael Schneider 2016-04-27 15:47:07 -07:00
parent 1c6686e5d6
commit 1f55ef8945
2 changed files with 31 additions and 13 deletions

View File

@ -11,6 +11,6 @@
#import <AsyncDisplayKit/AsyncDisplayKit.h>
@interface ViewController : ASViewController
@interface ViewController : ASViewController<ASPagerNode *>
@end

View File

@ -15,9 +15,14 @@
#import "PageNode.h"
@interface ViewController () <ASPagerNodeDataSource>
static UIColor *randomColor() {
CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}
- (ASPagerNode *)node;
@interface ViewController () <ASPagerNodeDataSource>
@end
@ -25,19 +30,30 @@
- (instancetype)init
{
if (!(self = [super initWithNode:[[ASPagerNode alloc] init]]))
return nil;
self = [super initWithNode:[[ASPagerNode alloc] init]];
if (self == nil) {
return self;
}
[self node].dataSource = self;
self.title = @"Pages";
self.node.dataSource = self;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(scrollToNextPage:)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Previous" style:UIBarButtonItemStylePlain target:self action:@selector(scrollToPreviousPage:)];
return self;
}
- (ASPagerNode *)node
#pragma mark - Actions
- (void)scrollToNextPage:(id)sender
{
return (ASPagerNode *)[super node];
[self.node scrollToPageAtIndex:self.node.currentPageIndex+1 animated:YES];
}
- (void)scrollToPreviousPage:(id)sender
{
[self.node scrollToPageAtIndex:self.node.currentPageIndex-1 animated:YES];
}
#pragma mark - ASPagerNodeDataSource
@ -47,11 +63,13 @@
return 5;
}
- (ASCellNode *)pagerNode:(ASPagerNode *)pagerNode nodeAtIndex:(NSInteger)index
- (ASCellNodeBlock)pagerNode:(ASPagerNode *)pagerNode nodeBlockAtIndex:(NSInteger)index
{
PageNode *page = [[PageNode alloc] init];
page.backgroundColor = [UIColor blueColor];
return page;
return ^{
PageNode *page = [[PageNode alloc] init];
page.backgroundColor = randomColor();
return page;
};
}
@end