From c9337218ef1660360097928c753bde1c79775618 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Tue, 23 Aug 2011 07:52:24 -0400 Subject: Added scrolling to level selection screen Using http://www.xcombinator.com/2010/09/08/a-paging-uiscrollview-in-cocos2d-with-previews/ as a base, I was able to implement a paging scroller for the level selection screen so players can swipe through available levels and choose one to play. At this point, the level selection screen is practically done--the only other thing I want to add is a UIPageControl to interact with the scrolling and give the player an indication of how many levels there are. Refs #207 --- Classes/CocosOverlayScrollView.m | 99 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100755 Classes/CocosOverlayScrollView.m (limited to 'Classes/CocosOverlayScrollView.m') diff --git a/Classes/CocosOverlayScrollView.m b/Classes/CocosOverlayScrollView.m new file mode 100755 index 0000000..dcf5571 --- /dev/null +++ b/Classes/CocosOverlayScrollView.m @@ -0,0 +1,99 @@ +// +// CocosOverlayScrollView.m +// shapes +// +// Created by Nate Murray on 8/23/10. +// Copyright 2010 LittleHiccup. All rights reserved. +// + +#import "CocosOverlayScrollView.h" + +@implementation CocosOverlayScrollView +@synthesize targetLayer; + +// Configure your favorite UIScrollView options here +-(id) initWithFrame: (CGRect) frameRect numPages: (int) numPages width: (float) width layer: (CCNode*) layer { + if ((self = [super initWithFrame: frameRect])){ + self.contentSize = CGSizeMake(width*numPages, 320); + self.bounces = YES; + self.delaysContentTouches = NO; + self.delegate = self; + self.pagingEnabled = YES; + self.scrollsToTop = NO; + self.showsVerticalScrollIndicator = NO; + self.showsHorizontalScrollIndicator = NO; + [self setUserInteractionEnabled:TRUE]; + [self setScrollEnabled:TRUE]; + self.targetLayer = layer; + // self.canCancelContentTouches = YES; + } + return self; +} + +-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event +{ + if (!self.dragging) + { + // UITouch* touch = [[touches allObjects] objectAtIndex:0]; + // CGPoint location = [touch locationInView: [[touch view] superview]]; + // CCLOG(@"touch at l.x:%f l.y:%f", location.x, location.y); + + [self.nextResponder touchesBegan: touches withEvent:event]; + [[[CCDirector sharedDirector] openGLView] touchesBegan:touches withEvent:event]; + } + + [super touchesBegan: touches withEvent: event]; +} + +-(void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event +{ + if (!self.dragging) + { + [self.nextResponder touchesEnded: touches withEvent:event]; + [[[CCDirector sharedDirector] openGLView] touchesEnded:touches withEvent:event]; + } + + [super touchesEnded: touches withEvent: event]; +} + +-(void) touchesCancelled: (NSSet *) touches withEvent: (UIEvent *) event +{ + // if (!self.dragging) + // { + // CCLOG(@"CocosOverlayScrollView touchesEnded not dragging"); + [self.nextResponder touchesCancelled: touches withEvent:event]; + [[[CCDirector sharedDirector] openGLView] touchesCancelled:touches withEvent:event]; + // } + [super touchesCancelled: touches withEvent: event]; +} + + +- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView +{ + // TODO - Custom code for handling deceleration of the scroll view +} + +- (void)scrollViewDidScroll:(UIScrollView *)scrollView +{ + CGPoint dragPt = [scrollView contentOffset]; + dragPt = [[CCDirector sharedDirector] convertToGL:dragPt]; + + dragPt.y = dragPt.y * -1; + dragPt.x = dragPt.x * -1; + + CGPoint newLayerPosition = CGPointMake(dragPt.x, dragPt.y); + + [targetLayer setPosition:newLayerPosition]; +} + +- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView +{ + // CGPoint dragPt = [scrollView contentOffset]; + // etc. +} + +-(void) dealloc { + self.targetLayer = nil; + [super dealloc]; +} +@end \ No newline at end of file -- cgit 1.4.1