Last active
December 11, 2015 06:19
-
-
Save sdabet/4558401 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation CustomScrollLayer | |
@synthesize customPageIndicators; | |
+(id) nodeWithLayers:(NSArray *)layers widthOffset: (int) widthOffset pageSpriteFrameName:(NSString*)pageSpriteFrameName | |
{ | |
return [[[self alloc] initWithLayers: layers widthOffset:widthOffset pageSpriteFrameName:pageSpriteFrameName] autorelease]; | |
} | |
/* | |
* Constructor which provides custom sprites for page indicators | |
*/ | |
-(id) initWithLayers:(NSArray *)layers widthOffset:(int)widthOffset pageSpriteFrameName:(NSString*)pageSpriteFrameName { | |
if(self = [self initWithLayers:layers widthOffset:widthOffset]) { | |
self.customPageIndicators = [CCNode node]; | |
// Use default position | |
customPageIndicators.position = self.pagesIndicatorPosition; | |
// Create sprites | |
CGFloat n = (CGFloat)self.totalScreens; //< Total points count in CGFloat. | |
CGFloat d = 16.0f; //< Distance between points. | |
for (int i=0; i < self.totalScreens; ++i) | |
{ | |
CCSprite *pageSprite = [CCSprite spriteWithSpriteFrameName:pageSpriteFrameName]; | |
pageSprite.position = ccp(d * ( (CGFloat)i - 0.5f*(n-1.0f) ), 0); | |
[customPageIndicators addChild:pageSprite]; | |
} | |
[self addChild:customPageIndicators]; | |
[self schedule:@selector(updatePageIndicators)]; | |
// Hide default page indicators | |
self.pagesIndicatorNormalColor = ccc4(0, 0, 0, 0); | |
self.pagesIndicatorSelectedColor = ccc4(0, 0, 0, 0); | |
} | |
return self; | |
} | |
-(void) setShowPagesIndicator:(BOOL)showPagesIndicator { | |
[super setShowPagesIndicator:showPagesIndicator]; | |
// Make sure custom page indicators are hidden | |
customPageIndicators.visible = showPagesIndicator; | |
} | |
-(void) setPagesIndicatorPosition:(CGPoint)pagesIndicatorPosition { | |
[super setPagesIndicatorPosition:pagesIndicatorPosition]; | |
customPageIndicators.position = pagesIndicatorPosition; | |
} | |
/* | |
* Scheduled method to update position of custom page indicator sprites | |
*/ | |
-(void) updatePageIndicators { | |
customPageIndicators.position = ccp(self.pagesIndicatorPosition.x - self.position.x, self.pagesIndicatorPosition.y); | |
for(int i=0; i<customPageIndicators.children.count; i++) { | |
CCSprite *indicator = [customPageIndicators.children objectAtIndex:i]; | |
// Non-current page are semi-transparent | |
if(i == self.currentScreen) { | |
[indicator setOpacity:255]; | |
} | |
else { | |
[indicator setOpacity:128]; | |
} | |
} | |
} | |
- (void)dealloc | |
{ | |
self.customPageIndicators = nil; | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment