diff --git a/Classes/Scenes/HCUPPanelScene.h b/Classes/Scenes/HCUPPanelScene.h index 2d16b89..b83aba1 100644 --- a/Classes/Scenes/HCUPPanelScene.h +++ b/Classes/Scenes/HCUPPanelScene.h @@ -19,7 +19,7 @@ int nextWorld_; BOOL transitioning_; CocosOverlayScrollView* scrollView; - TouchDelegatingView* scrollViewContainer; + TouchDelegatingView* touchDelegatingView; UIPageControl* pageControl; } diff --git a/Classes/Scenes/HCUPPanelScene.m b/Classes/Scenes/HCUPPanelScene.m index 5174aa6..d69309d 100644 --- a/Classes/Scenes/HCUPPanelScene.m +++ b/Classes/Scenes/HCUPPanelScene.m @@ -1,20 +1,19 @@ /* - * HSLevelSelectionScene2.m - * shapes + * HCUPPanelScene.m + * Jacob's Shapes * * Created by Nate Murray on 7/24/10. - * Copyright 2010 YetiApps. All rights reserved. + * Copyright 2010 LittleHiccup. All rights reserved. * + * Huge thanks to the the authors of following urls: + * http://getsetgames.com/2009/08/21/cocos2d-and-uiscrollview/ + * http://blog.proculo.de/archives/180-Paging-enabled-UIScrollView-With-Previews.html */ #include "HCUPPanelScene.h" #import "NMPanelMenu.h" #import "NMPanelMenuItem.h" #import "TouchDelegatingView.h" - -// http://getsetgames.com/2009/08/21/cocos2d-and-uiscrollview/ -// http://blog.proculo.de/archives/180-Paging-enabled-UIScrollView-With-Previews.html - @implementation HCUPPanelScene +(id) scene @@ -115,12 +114,12 @@ - (void) onEnter // // Note that we're only concerned with a horizontal iPhone. If your game is // vertical, change accordingly - scrollViewContainer = [[TouchDelegatingView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; + touchDelegatingView = [[TouchDelegatingView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; scrollView = [[CocosOverlayScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, totalPanelWidth) numPages: numberOfPages width: totalPanelWidth layer: panels]; - scrollViewContainer.scrollView = scrollView; + touchDelegatingView.scrollView = scrollView; // this is just to pre-set the scroll view to a particular panel [scrollView setContentOffset: CGPointMake(0, currentWorldOffset * totalPanelWidth) animated: NO]; @@ -128,11 +127,11 @@ - (void) onEnter // Add views to cocos2d // We called it a TouchDelegatingView, but it actually isn't containing anything at all. // In reality it is just taking up any space under our ScrollView and delegating the touches. - [[[CCDirector sharedDirector] openGLView] addSubview:scrollViewContainer]; + [[[CCDirector sharedDirector] openGLView] addSubview:touchDelegatingView]; [[[CCDirector sharedDirector] openGLView] addSubview:scrollView]; [scrollView release]; - [scrollViewContainer release]; + [touchDelegatingView release]; [super onEnter]; } @@ -158,7 +157,7 @@ - (void) visit { - (void) onExit { [scrollView removeFromSuperview]; - [scrollViewContainer removeFromSuperview]; + [touchDelegatingView removeFromSuperview]; [super onExit]; } diff --git a/README.mkd b/README.mkd new file mode 100644 index 0000000..31b1cf4 --- /dev/null +++ b/README.mkd @@ -0,0 +1,39 @@ +# Paging UIScrollView with Previews in Cocos2D + +This project shows how to create a paged UIScrollView with Previews +in Cocos2D. It looks like this: + +!Resources/iphone-preview.jpg! + +[Watch the video!](youtube). + +# The Problem + +Apple's UIScrollView paging doesn't support an arbitrary width argument. If you +make a UIScrollView with a width smaller than the whole screen, then that view +won't capture touches outside of that area. + +Additionally, we are mixing Cocoa views and Cocos2d/OpenGL, and how they interact is +not always obvious. + +# Basic Idea + +We create a subclass of `CCMenu` and add panels as items. The `CCMenu` is scrolled +as the `UIScrollView` is scrolled. The `UIScrollView` is set to be smaller than +the whole screen: this gives us previews on either side of the current page. +We create a full-screen `TouchDelegatingView` under the `UIScrollView` and use +that to delegate touches that lie outside the `UIScrollView`. + +There are a few custom classes mixed in, but that is the basic idea. + +# Credits + +Written by Nate Murray for [Jacob's Shapes](http://www.littlehiccup.com), an +iPhone game for toddlers. + +It uses ideas and code from the following sites: + + * http://getsetgames.com/2009/08/21/cocos2d-and-uiscrollview/ + * http://blog.proculo.de/archives/180-Paging-enabled-UIScrollView-With-Previews.html + + diff --git a/Resources/iphone-preview.jpg b/Resources/iphone-preview.jpg new file mode 100644 index 0000000..0e78f3f Binary files /dev/null and b/Resources/iphone-preview.jpg differ