From 67e7835a745b80a043c8012a4444e79ff9c99353 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Thu, 7 Jul 2016 17:46:40 +0800 Subject: [PATCH] hidden scrollbar when content size smaller than scrollview --- cocos2d/core/components/CCScrollBar.js | 7 ++- cocos2d/core/components/CCScrollView.js | 57 ++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/cocos2d/core/components/CCScrollBar.js b/cocos2d/core/components/CCScrollBar.js index 2246cc185f..4f5da41e32 100644 --- a/cocos2d/core/components/CCScrollBar.js +++ b/cocos2d/core/components/CCScrollBar.js @@ -327,11 +327,16 @@ var Scrollbar = cc.Class({ } }, - reset: function() { + hide: function() { this._autoHideRemainingTime = 0; this._setOpacity(0); }, + show: function() { + this._autoHideRemainingTime = 0; + this._setOpacity(this._opacity); + }, + update: function(dt) { this._processAutoHide(dt); } diff --git a/cocos2d/core/components/CCScrollView.js b/cocos2d/core/components/CCScrollView.js index 807641e612..43e5e3898e 100644 --- a/cocos2d/core/components/CCScrollView.js +++ b/cocos2d/core/components/CCScrollView.js @@ -527,7 +527,6 @@ var ScrollView = cc.Class({ * @return {Vec2} - A Vec2 value indicate the current scroll offset. */ getScrollOffset: function() { - var maxScrollOffset = this.getMaxScrollOffset(); var topDelta = this._topBoundary - this._getContentTopBoundary(); var leftDeta = this._getContentLeftBoundary() - this._leftBoundary; @@ -769,6 +768,45 @@ var ScrollView = cc.Class({ var topRightPosition = this._convertToContentParentSpace(cc.p(scrollViewSize.width, scrollViewSize.height)); this._rightBoundary = topRightPosition.x; this._topBoundary = topRightPosition.y; + + var contentSize = this.content.getContentSize(); + + var bottomDeta = this._getContentBottomBoundary() - this._bottomBoundary; + bottomDeta = -bottomDeta; + var moveDelta = cc.p(0, 0); + var totalScrollDelta = 0; + + var leftDeta = this._getContentLeftBoundary() - this._leftBoundary; + leftDeta = -leftDeta; + + if (contentSize.height < scrollViewSize.height) { + totalScrollDelta = contentSize.height - scrollViewSize.height; + moveDelta.y = bottomDeta - totalScrollDelta; + + if (this.verticalScrollBar) { + this.verticalScrollBar.hide(); + } + } else { + if (this.verticalScrollBar) { + this.verticalScrollBar.show(); + } + } + + if (contentSize.width < scrollViewSize.width) { + totalScrollDelta = contentSize.width - scrollViewSize.width; + moveDelta.x = leftDeta; + + if (this.horizontalScrollBar) { + this.horizontalScrollBar.hide(); + } + + } else { + if (this.horizontalScrollBar) { + this.horizontalScrollBar.show(); + } + } + + this._moveContent(moveDelta); } }, @@ -891,7 +929,6 @@ var ScrollView = cc.Class({ _handlePressLogic: function() { this._autoScrolling = false; - this._calculateBoundary(); this._touchMovePreviousTimestamp = getTimeInMilliseconds(); this._touchMoveDisplacements = []; @@ -1229,6 +1266,9 @@ var ScrollView = cc.Class({ if (!CC_EDITOR) { this._registerEvent(); this.node.on('size-changed', this._calculateBoundary, this); + if(this.content) { + this.content.on('size-changed', this._calculateBoundary, this); + } } }, @@ -1238,16 +1278,23 @@ var ScrollView = cc.Class({ onDestroy: function() { this.node.off('size-changed', this._calculateBoundary, this); + if(this.content) { + this.content.off('size-changed', this._calculateBoundary, this); + } }, - onDisable: function() { + _hideScrollbar: function () { if (this.horizontalScrollBar) { - this.horizontalScrollBar.reset(); + this.horizontalScrollBar.hide(); } if (this.verticalScrollBar) { - this.verticalScrollBar.reset(); + this.verticalScrollBar.hide(); } + }, + + onDisable: function() { + this._hideScrollbar(); this._autoScrolling = false; this._autoScrollAccumulatedTime = this._autoScrollTotalTime; },