您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d下实现UIScrollView/UIPageController效果

2013-06-27 01:06 549 查看
以前cocos2d项目中总是用UIScrollView和UIPageController实现滑屏效果,这次做移车位项目的时候采用网上的一个例子(study_ScrollLayer)实现的滑屏,效果还不错。下面请让我给大家介绍一下它是如何实现的。
一) 数据结构:
//CCScrollLayer.h中定义的数据结构
// Holds the current height and width of the screen
int scrollHeight;
int scrollWidth;
分别表示滑屏每屏屏宽和屏高,Layer的构造函数中设定这两个值;
// Holds the height and width of the screen when the class was inited
int startHeight;
int startWidth;
分别表示scroll view中默认显示的起始位置;
// Holds the current page being displayed
int currentScreen;
存放scroller view的显示的当前屏索引;
// A count of the total screens available
int totalScreens;
能够显示的最大屏数;
// The initial point the user starts their swipe
int startSwipe;
存放手指滑动的起始位置横坐标;
NSInvocation *onPageMoved;
自实现的滑屏pageController的delegage,后面会详细说明;
//HelloWorldScene.h中定义的数据结构:
NSMutableArray *pageIndexArray;
数组用来存放模拟pageController的标签(用标签动态模拟pageController的点突显示效果)
二)主要函数说明
//CCScrollLayer.m
1)构造函数
-(id) initWithLayers:(NSMutableArray *)layers widthOffset: (int) widthOffset
layers传入使用者预先构造好的pages,widthOffSet表示每屏相对于手机屏幕被裁剪掉的宽度。
构造函数代码实现注释写得很清楚,跟往UIScrollerView里添加页面大致是相当的。
a)// Make sure the layer accepts touches
b)// Set up the starting variables
c)// offset added to show preview of next/previous screens
d)// Loop through the array and add the screens
2)滑屏相关函数
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
startSwipe = touchPoint.x;
记录每次滑动的起始横坐标;
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
self.position = ccp((-(currentScreen-1)*scrollWidth)+(touchPoint.x-startSwipe),0);
实现屏幕中心随着手指滑动效果;
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
if ( (newX – startSwipe) < -100 && (currentScreen+1) <= totalScreens )
{[self moveToNextPage];}
else if ( (newX – startSwipe) > 100 && (currentScreen-1) > 0 )
{[self moveToPreviousPage];}
else{[self moveToPage:currentScreen];}
当滑动距离超过正向100,滑向下一页;当滑动距离超过负向100,滑向前一页;当滑动距离处于两者之间,恢复显示当前屏。其中100为用户切屏灵敏度。
3)滑动动画效果函数
-(void) moveToNextPage
-(void) moveToPreviousPage
-(void) moveToPage:(int)page
这几个函数很好理解,这里就不赘述。
通过上面部分的介绍,我们能够初步了解苹果封装的一些控件的实现思路,有助于更好更恰当的使用这些控件,知其然更知其所以然。
//HelloWorldScene.m
这里重点介绍一下如何实现pageControl delegate
// page moved delegate
NSMethodSignature* signature =
[[self class] instanceMethodSignatureForSelector:@selector(onPageMoved:)];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:self];
[invocation setSelector:@selector(onPageMoved:)];
scroller.onPageMoved = invocation;
我以前对delegate的理解是:用户预先传递对象指针,然后通过这个指针调用委托方法。看了上述这段代码大致理解了苹果的底层实现原理应该跟这个类似吧:HelloWorldScene的onPageMoved方法注册为NSInvocation后传给scrollLayer;scroller收到onPageMoved消息后负责invocate HelloScrollLayer的onPageMoved方法,并且负责讲函数参数传给它(下面这段代码)。
//CCScrollLayer.m
- (void)onPageMoved:(id)sender
[self.onPageMoved setArgument:&self atIndex:2];
[self.onPageMoved invoke];
总结:通过这个例子更进一步了解了UIScrollView的底层实现思路,同时也更深理解delegate的实现原理。
study_ScrollerLayer的源码网上很容易找到,大家可以加入调试信息之后运行一下看看实际效果。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: