Skip to content

Commit 1878a83

Browse files
committed
modified scrollbar behavior
1 parent 45e9981 commit 1878a83

File tree

2 files changed

+126
-11
lines changed

2 files changed

+126
-11
lines changed

extensions/ccui/uiwidgets/scroll-widget/UIScrollView.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ ccui.ScrollView = ccui.Layout.extend(/** @lends ccui.ScrollView# */{
107107
this._initScrollBar();
108108

109109
this.setTouchEnabled(true);
110+
this._scrollBarEnabled && this.setSwallowTouches(false);
110111
},
111112

112113
/**
@@ -116,6 +117,8 @@ ccui.ScrollView = ccui.Layout.extend(/** @lends ccui.ScrollView# */{
116117
init: function () {
117118
if (ccui.Layout.prototype.init.call(this)) {
118119

120+
if(this._scrollBarEnabled)
121+
this.setSwallowTouches(false);
119122

120123
return true;
121124
}
@@ -1099,11 +1102,11 @@ ccui.ScrollView = ccui.Layout.extend(/** @lends ccui.ScrollView# */{
10991102

11001103
if(this._verticalScrollBar)
11011104
{
1102-
this._verticalScrollBar.onTouchBegan();
1105+
//this._verticalScrollBar.onTouchBegan();
11031106
}
11041107
if(this._horizontalScrollBar)
11051108
{
1106-
this._horizontalScrollBar.onTouchBegan();
1109+
//this._horizontalScrollBar.onTouchBegan();
11071110
}
11081111
},
11091112

@@ -1138,11 +1141,11 @@ ccui.ScrollView = ccui.Layout.extend(/** @lends ccui.ScrollView# */{
11381141

11391142
if(this._verticalScrollBar)
11401143
{
1141-
this._verticalScrollBar.onTouchEnded();
1144+
//this._verticalScrollBar.onTouchEnded();
11421145
}
11431146
if(this._horizontalScrollBar)
11441147
{
1145-
this._horizontalScrollBar.onTouchEnded();
1148+
//this._horizontalScrollBar.onTouchEnded();
11461149
}
11471150
},
11481151

@@ -1376,11 +1379,13 @@ ccui.ScrollView = ccui.Layout.extend(/** @lends ccui.ScrollView# */{
13761379

13771380
if(this._scrollBarEnabled)
13781381
{
1382+
this.setSwallowTouches(true);
13791383
this._removeScrollBar();
13801384
}
13811385
this._scrollBarEnabled = enabled;
13821386
if(this._scrollBarEnabled)
13831387
{
1388+
this.setSwallowTouches(false);
13841389
this._initScrollBar();
13851390
}
13861391
},

extensions/ccui/uiwidgets/scroll-widget/UIScrollViewBar.js

Lines changed: 117 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ ccui.ScrollViewBar = ccui.ProtectedNode.extend(/** @lends ccui.ScrollViewBar# */
4545

4646
_marginFromBoundary : 0,
4747
_marginForLength: 0,
48+
49+
_touchBeganPosition: null,
4850

4951
_touching: false,
5052

@@ -99,10 +101,64 @@ ccui.ScrollViewBar = ccui.ProtectedNode.extend(/** @lends ccui.ScrollViewBar# */
99101
this.onScrolled(cc.p(0, 0));
100102
cc.ProtectedNode.prototype.setOpacity.call(this, 0);
101103
this._autoHideRemainingTime = 0;
104+
105+
this._touchBeganPosition = cc.p(0, 0);
102106

103107
if(this._direction === ccui.ScrollView.DIR_HORIZONTAL)
104108
{
105109
this.setRotation(90);
110+
}
111+
112+
this.setTouchEnabled(true);
113+
},
114+
115+
removeFromParent: function()
116+
{
117+
ccui.ProtectedNode.prototype.removeFromParent.call(this);
118+
this.setTouchEnabled(false);
119+
},
120+
121+
setParent: function(node)
122+
{
123+
ccui.ProtectedNode.prototype.setParent.apply(this, arguments);
124+
if(node)
125+
this.setTouchEnabled(true);
126+
else
127+
this.setTouchEnabled(false);
128+
},
129+
130+
_isAncestorsVisible: function(node){
131+
if (null == node)
132+
return true;
133+
134+
var parent = node.getParent();
135+
136+
if (parent && !parent.isVisible())
137+
return false;
138+
return this._isAncestorsVisible(parent);
139+
},
140+
141+
/**
142+
* Sets whether the widget is touch enabled. The default value is false, a widget is default to touch disabled
143+
* @param {Boolean} enable true if the widget is touch enabled, false if the widget is touch disabled.
144+
*/
145+
setTouchEnabled: function (enable) {
146+
if (this._touchEnabled === enable)
147+
return;
148+
149+
this._touchEnabled = enable; //TODO need consider remove and re-add.
150+
if (this._touchEnabled) {
151+
if(!this._touchListener)
152+
this._touchListener = cc.EventListener.create({
153+
event: cc.EventListener.TOUCH_ONE_BY_ONE,
154+
swallowTouches: true,
155+
onTouchBegan: this.onTouchBegan.bind(this),
156+
onTouchMoved: this.onTouchMoved.bind(this),
157+
onTouchEnded: this.onTouchEnded.bind(this)
158+
});
159+
cc.eventManager.addListener(this._touchListener, this);
160+
} else {
161+
cc.eventManager.removeListener(this._touchListener);
106162
}
107163
},
108164

@@ -151,6 +207,7 @@ ccui.ScrollViewBar = ccui.ProtectedNode.extend(/** @lends ccui.ScrollViewBar# */
151207
*/
152208
setWidth: function(width)
153209
{
210+
this._contentSize.width = width;
154211
var scale = width / this._body.width;
155212
this._body.setScaleX(scale);
156213
this._upperHalfCircle.setScale(scale);
@@ -211,6 +268,7 @@ ccui.ScrollViewBar = ccui.ProtectedNode.extend(/** @lends ccui.ScrollViewBar# */
211268
var ratio = length / this._body.getTextureRect().height;
212269
this._body.setScaleY(ratio);
213270
this._upperHalfCircle.setPositionY(this._body.getPositionY() + length);
271+
this._contentSize.height = length;
214272
},
215273

216274
_processAutoHide: function(dt)
@@ -240,34 +298,86 @@ ccui.ScrollViewBar = ccui.ProtectedNode.extend(/** @lends ccui.ScrollViewBar# */
240298
},
241299

242300
/**
243-
* This is called by parent ScrollView when a touch is began. Don't call this directly.
301+
* The touch began event callback handler of ccui.ScrollViewBar.
302+
* @param {cc.Touch} touch
303+
* @param {cc.Event} event
304+
* @returns {boolean}
244305
*/
245-
onTouchBegan: function()
306+
onTouchBegan: function(touch, event)
246307
{
308+
this._hit = false;
309+
if (this.isVisible() && ccui.Widget.prototype._isAncestorsVisible.call(this, this)){
310+
var touchPoint = touch.getLocation();
311+
this._touchBeganPosition.x = touchPoint.x;
312+
this._touchBeganPosition.y = touchPoint.y;
313+
314+
var bb = cc.rect(0, 0, this._contentSize.width, this._contentSize.height);
315+
316+
var p = ccui.Widget.prototype.convertToNodeSpace.call(this, this._touchBeganPosition);
317+
318+
p.x += this._contentSize.width / 2;
319+
320+
if(cc.rectContainsPoint(bb, p))
321+
this._hit = true;
322+
}
323+
if (!this._hit)
324+
{
325+
return false;
326+
}
327+
328+
ccui.ScrollView.prototype.onTouchEnded.apply(this.getParent(), arguments); // just in case its already triggered
329+
330+
this.getParent()._isInterceptTouch = true;
331+
247332
if(!this._autoHideEnabled)
248333
{
249-
return;
334+
return true;
250335
}
251336
this._touching = true;
337+
338+
return true;
252339
},
253340

254341
/**
255-
* This is called by parent ScrollView when a touch is ended. Don't call this directly.
342+
* The touch ended event callback handler of ccui.ScrollViewBar.
343+
* @param {cc.Touch} touch
344+
* @param {cc.Event} event
345+
* @returns {boolean}
256346
*/
257-
onTouchEnded: function()
347+
onTouchEnded: function(touch, event)
258348
{
349+
this.getParent()._isInterceptTouch = false;
259350
if(!this._autoHideEnabled)
260351
{
261-
return;
352+
return true;
262353
}
263354
this._touching = false;
264355

265356
if(this._autoHideRemainingTime <= 0)
266357
{
267358
// If the remaining time is 0, it means that it didn't moved after touch started so scroll bar is not showing.
268-
return;
359+
return true;
269360
}
270361
this._autoHideRemainingTime = this.autoHideTime;
362+
return true;
363+
},
364+
365+
/**
366+
* The touch moved event callback handler of ccui.ScrollViewBar.
367+
* @param {cc.Touch} touch
368+
* @param {cc.Event} event
369+
*/
370+
onTouchMoved: function (touch, event) {
371+
var location = touch.getLocation();
372+
var prevLocation = touch.getPreviousLocation();
373+
374+
// swap the points: we want to follow the direction of the touch
375+
touch._point.y = prevLocation.y;
376+
touch._prevPoint.y = location.y;
377+
378+
touch._point.y += touch.getDelta().y; // scrolling with the bar should be somewhat faster
379+
380+
ccui.ScrollView.prototype._handleMoveLogic.call(this.getParent(), touch);
271381
},
272382

273383
/**

0 commit comments

Comments
 (0)