Skip to content

Instantly share code, notes, and snippets.

@sdabet
Created December 18, 2012 12:51
Show Gist options
  • Save sdabet/4327693 to your computer and use it in GitHub Desktop.
Save sdabet/4327693 to your computer and use it in GitHub Desktop.
ElasticScrollLayer.m
#import "ElasticScrollLayer.h"
@implementation ElasticScrollLayer
@synthesize actionDuration, bouncePeriod;
+(id) nodeWithLayers:(NSArray *)layers widthOffset: (int) widthOffset actionDuration:(float)duration bouncePeriod:(float)period
{
ElasticScrollLayer *layer = [[[self alloc] initWithLayers: layers widthOffset:widthOffset] autorelease];
layer.actionDuration = duration;
layer.bouncePeriod = period;
return layer;
}
- (CGPoint) positionForPageWithNumber: (int) pageNumber
{
return ccp( - pageNumber * (self.contentSize.width - self.pagesWidthOffset), 0.0f );
}
-(void) moveToPage:(int)page
{
if (page < 0 || page >= [layers_ count]) {
CCLOGERROR(@"CCScrollLayer#moveToPage: %d - wrong page number, out of bounds. ", page);
return;
}
id changePage = [CCMoveTo actionWithDuration:actionDuration position: [self positionForPageWithNumber: page]];
changePage = [CCEaseElasticOut actionWithAction:changePage period:bouncePeriod];
changePage = [CCSequence actions: changePage,[CCCallFunc actionWithTarget:self selector:@selector(moveToPageEnded)], nil];
[self runAction:changePage];
currentScreen_ = page;
}
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
[self stopAllActions];
return [super ccTouchBegan:touch withEvent:event];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment