由于镜面反射涉及到openGL,不会。
h文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#ifndef __CoverView_H__
#define __CoverView_H__
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
class CoverView : public CCNode , public CCScrollViewDelegate , public CCTouchDelegate
{
public:
CoverView();
~CoverView();
/**
*
* @param scrollView的rect
* @param scrollView滚动size
* @param card之间的距离
* @param card之间的缩放
*/
static CoverView* create(CCRect swBox, CCSize slSize ,float disDistance , float disScale );
virtual bool init(CCRect swBox , CCSize slSize , float disDistance , float disScale);
virtual void onEnter();
virtual void onExit();
virtual bool ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent);
virtual void ccTouchMoved(CCTouch* pTouch, CCEvent* pEvent);
virtual void ccTouchEnded(CCTouch* pTouch, CCEvent* pEvent);
void initData();
void scrollViewDidScroll(CCScrollView* view);
void scrollViewDidZoom(CCScrollView* view);
void adjustCardScale(CCPoint adjustPoint);
void adjustScrollView(CCPoint adjustPoint);
void adjusetEndScrollView();
void cardViewEnd_callBack(CCNode* pSender);
int getCurCardIndex();
//当前中间card索引 从0开始
void addCard(CCNode * card);
void addCard(CCNode * card, int zOrder);
void addCard(CCNode* card, int zOrder, int tag);
CC_SYNTHESIZE(CCPoint , swPosition , SwPosition);
//scrollView 位置
CC_SYNTHESIZE(CCSize , swSize , SwSize);
//scrollView大小
CC_SYNTHESIZE(CCSize , slSize , SlSize);
//scrollLayer 大小
CC_SYNTHESIZE(float , disDistance , DisDistance);
//card距离间隔
CC_SYNTHESIZE(float , disScale , DisScale);
//crad缩放间隔
CC_SYNTHESIZE(CCRect , swBox , SwBox);
//scrollview 边框
CC_SYNTHESIZE(CCLayer* , scrollLayer , scrollLayer);
//scrollView的containLayer
CC_SYNTHESIZE(int , cardNum , CardNum);
//card索引
CC_PROPERTY(CCPoint , offsetPosition , OffsetPosition);
//card起始位置
private:
CCSize wSize;
CCArray* cardArray;
CCScrollView* scrollView;
};
#endif
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#include "CoverView.h"
CoverView::CoverView()
{
}
CoverView::~CoverView()
{
CC_SAFE_RELEASE_NULL(cardArray);
}
CoverView* CoverView::create(CCRect swBox, CCSize slSize , float disDistance , float disScale)
{
CoverView* cover =
new
CoverView();
if
(cover && cover->init(swBox,slSize,disDistance,disScale))
{
cover->autorelease();
return
cover;
}
CC_SAFE_DELETE(cover);
return
NULL;
}
bool CoverView::init(CCRect swBox , CCSize slSize , float disDistance , float disScale)
{
if
(!CCNode::init())
return
false
;
this
->swBox = swBox;
this
->swPosition = swBox.origin;
this
->swSize = swBox.size;
this
->slSize = slSize;
this
->disDistance = disDistance;
this
->disScale = disScale;
initData();
return
true
;
}
void CoverView::initData()
{
wSize = CCDirector::sharedDirector()->getWinSize();
cardArray = CCArray::create();
cardArray->retain();
cardNum = 0;
offsetPosition = ccp(swSize.width/2,swSize.height/2);
scrollLayer = CCLayer::create();
scrollLayer->setAnchorPoint(CCPointZero);
scrollLayer->setPosition(CCPointZero);
scrollLayer->setContentSize(slSize);
scrollView = CCScrollView::create(swSize,scrollLayer);
scrollView->setAnchorPoint(CCPointZero);
scrollView->setContentOffset(ccp(0,0));
scrollView->setTouchEnabled(
false
);
scrollView->setDelegate(
this
);
scrollView->setDirection(kCCScrollViewDirectionHorizontal);
addChild(scrollView,1);
}
void CoverView::onEnter()
{
CCNode::onEnter();
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(
this
, 0 ,
false
);
}
void CoverView::onExit()
{
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(
this
);
removeAllChildren();
CCNode::onExit();
}
bool CoverView::ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent)
{
return
true
;
}
void CoverView::ccTouchMoved(CCTouch* pTouch, CCEvent* pEvent)
{
CCPoint scroll_prepoint = pTouch->getPreviousLocation();
CCPoint scroll_movepoint = pTouch->getLocation();
if
(swBox.containsPoint(scroll_movepoint))
{
CCPoint adjustPoint = scroll_movepoint-scroll_prepoint;
adjustScrollView(adjustPoint);
adjustCardScale(adjustPoint);
}
}
void CoverView::ccTouchEnded(CCTouch* pTouch, CCEvent* pEvent)
{
CCPoint scroll_prepoint = pTouch->getPreviousLocation();
CCPoint scroll_endpoint = pTouch->getLocation();
float disX = scroll_endpoint.x - scroll_endpoint.x;
adjusetEndScrollView();
}
void CoverView::adjustCardScale(CCPoint adjustPoint)
{
float disX = adjustPoint.x;
CCObject* obj = NULL;
CCARRAY_FOREACH(cardArray,obj)
{
CCNode* card = (CCNode*) obj;
float offset = scrollView->getContentOffset().x;
float posX = card->getPositionX() + offset;
float disMid = abs(swSize.width/2-posX);
float scale = 1- disMid/disDistance*disScale;
card->setScale(scale);
int zOr = (int) (1000-disMid*0.1);
card->setZOrder(zOr);
}
}
void CoverView::adjustScrollView(CCPoint adjustPoint)
{
CCPoint endPoint = ccpAdd(scrollView->getContentOffset(),ccp(adjustPoint.x,0));
scrollView->unscheduleAllSelectors();
scrollView->setContentOffset(endPoint,
false
);
}
void CoverView::adjusetEndScrollView()
{
CCObject* obj = NULL;
float minX = wSize.height;
float midX = swSize.width/2;
//获取距离中间最小值的card
CCARRAY_FOREACH(cardArray,obj)
{
CCNode* card = (CCNode*) obj;
float offset = scrollView->getContentOffset().x;
//转化父类坐标
float posX = card->getPositionX() + offset;
float disMid = midX-posX;
if
(abs(disMid) < abs(minX)) minX = disMid;
}
CCARRAY_FOREACH(cardArray,obj)
{
CCNode* item = (CCNode*) obj;
//转化父类坐标
float offset = scrollView->getContentOffset().x;
float posX = item->getPositionX() + offset ;
//距离中间长度
float disMid = abs(midX - posX - minX);
//目标scale
float scale = 1- disMid/disDistance*disScale;
CCScaleTo* scaleBy = CCScaleTo::create(0.2f,scale);
item->runAction(scaleBy);
int zOr = (int) (1000-disMid*0.1);
item->setZOrder(zOr);
}
CCLayer* scrollLayer = (CCLayer*)scrollView->getContainer();
CCMoveBy* moveBy = CCMoveBy::create(0.2f,ccp(minX,0));
//CCCallFuncN* callFuncN = CCCallFuncN::create(this,callfuncN_selector(CoverView::cardViewEnd_callBack));
//CCSequence* seq = CCSequence::create(moveBy,callFuncN,NULL);
//scrollLayer->runAction(seq);
scrollLayer->runAction(moveBy);
}
void CoverView::cardViewEnd_callBack(CCNode* pSender)
{
getCurCardIndex();
}
void CoverView::scrollViewDidScroll(CCScrollView* view)
{
}
void CoverView::scrollViewDidZoom(CCScrollView* view)
{
}
void CoverView::addCard(CCNode * card)
{
int zOrder = 1000 - cardNum;
this
->addCard(card, zOrder, 0);
}
void CoverView::addCard(CCNode * card, int zOrder)
{
this
->addCard(card, zOrder,0);
}
void CoverView::addCard(CCNode* card, int zOrder, int tag)
{
float positionX = offsetPosition.x + disDistance*cardNum;
float scale = 1 - disScale*cardNum;
card->setPosition(ccp(positionX,offsetPosition.y));
card->setScale(scale);
cardArray->addObject(card);
scrollLayer->addChild(card , zOrder,tag);
cardNum++;
//CCLog("crad%d:[%f , %f]",cardNum,card->getPositionX(),card->getPositionY());
}
int CoverView::getCurCardIndex()
{
float distance1 = - scrollLayer->getPositionX();
float distance2 = swSize.width/2 - offsetPosition.x;
//+5 浮点数误差
int index = (distance1 + distance2 + 5) / (disDistance);
//CCLog("card index:%d distance1:%f",index,distance1);
return
index;
}
void CoverView::setOffsetPosition(CCPoint
var
)
{
offsetPosition =
var
;
CCObject* obj = NULL;
cardNum = 0;
CCARRAY_FOREACH(cardArray,obj)
{
CCNode* card = (CCNode*) obj;
float positionX = offsetPosition.x + disDistance*cardNum;
card->setPosition(ccp(positionX,offsetPosition.y));
cardNum++;
}
adjustCardScale(CCPointZero);
}
CCPoint CoverView::getOffsetPosition()
{
return
offsetPosition;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
bool HelloWorld::init()
{
if
( !CCLayer::init() )
{
return
false
;
}
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(
"image/logoLayer.plist"
);
float winWidth = CCDirector::sharedDirector()->getWinSize().width;
float winHeight = CCDirector::sharedDirector()->getWinSize().height;
CCRect swRect = CCRectMake(0.01f*winWidth,0.4f*winHeight,0.98f*winWidth,0.33f*winHeight);
CCSize slSize = CCSizeMake(2.3f*winWidth,0.33f*winHeight);
float disDistance = 0.2f*winWidth;
float disScale = 0.25f;
CoverView* coverView = CoverView::create(swRect,slSize,disDistance,disScale);
CCLog(
"add card"
);
for
(int i = 0 ; i< 11 ; i++)
{
CCSprite* player = CCSprite::createWithSpriteFrameName(
"hero_bg.png"
);
coverView->addCard(player);
}
//coverflow1.png默认第一张在coverView的正中间 coverflow2.png改变第一张的位置有卡片初始不一定在最中间 需要手动调整
//coverView->setOffsetPosition(ccp(0.1f*winWidth,swRect.size.height/2));
coverView->setPosition(swRect.origin);
addChild(coverView);
return
true
;
}
|
原文链接:http://cocos2d.9tech.cn/news/2014/0326/40111.html