From 97f7d95074ca76e38ecffe6fa5f72f9d0a742a63 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Mon, 30 May 2016 20:00:58 +0800 Subject: [PATCH 1/3] add scroll to fixed position API --- cocos2d/core/components/CCScrollView.js | 38 +++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/cocos2d/core/components/CCScrollView.js b/cocos2d/core/components/CCScrollView.js index 5a802b1ff3..593889af50 100644 --- a/cocos2d/core/components/CCScrollView.js +++ b/cocos2d/core/components/CCScrollView.js @@ -484,6 +484,44 @@ var ScrollView = cc.Class({ } }, + + /** + * !#en Scroll with an offset related to the ScrollView's origin. + * !#zh 视图内容在规定时间内将滚动到 ScrollView 相对偏移位置上。 + * @method scrollToOffset + * @param {Vec2} position - A Vec2, the value of which each axis between 0 and (contentSize.width/height - scrollSize.width/height). + * @param {Number} [timeInSecond=0] - Scroll time in second, if you don't pass timeInSecond, + * the content will jump to the specific offset of ScrollView immediately. + * @param {Boolean} [attenuated=true] - Whether the scroll acceleration attenuated, default is true. + * @example + * // Scroll to middle position in 0.1 second in x-axis + * var maxScrollOffset = this.getMaxScrollOffset(); + * scrollView.scrollToOffset(cc.p(maxScrollOffset.x / 2, 0), 0.1); + */ + scrollToOffset: function(position, timeInSecond, attenuated) { + var maxScrollOffset = this.getMaxScrollOffset(); + + var anchor = cc.pClamp(cc.p(position.x / maxScrollOffset.x, (maxScrollOffset.y - position.y ) / maxScrollOffset.y), + cc.p(0, 0), cc.p(1, 1)); + + this.scrollTo(anchor, timeInSecond, attenuated); + }, + + getScrollOffset: function() { + var bottomDeta = Math.abs(this._getContentBottomBoundary() - this._bottomBoundary); + var leftDeta = Math.abs(this._getContentLeftBoundary() - this._leftBoundary); + var maxScrollOffset = this.getMaxScrollOffset(); + + return cc.p(leftDeta, maxScrollOffset.y - bottomDeta); + }, + + getMaxScrollOffset: function() { + var scrollSize = this.node.getContentSize(); + var contentSize = this.content.getContentSize(); + + return cc.p(contentSize.width - scrollSize.width, contentSize.height - scrollSize.height); + }, + /** * !#en Scroll the content to the horizontal percent position of ScrollView. * !#zh 视图内容在规定时间内将滚动到 ScrollView 水平方向的百分比位置上。 From 0118566038952287c41bdce4a2c68526f82e6c99 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Tue, 31 May 2016 09:46:04 +0800 Subject: [PATCH 2/3] add API docs --- cocos2d/core/components/CCScrollView.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/cocos2d/core/components/CCScrollView.js b/cocos2d/core/components/CCScrollView.js index 593889af50..5e55fdc4ca 100644 --- a/cocos2d/core/components/CCScrollView.js +++ b/cocos2d/core/components/CCScrollView.js @@ -486,10 +486,11 @@ var ScrollView = cc.Class({ /** - * !#en Scroll with an offset related to the ScrollView's origin. - * !#zh 视图内容在规定时间内将滚动到 ScrollView 相对偏移位置上。 + * !#en Scroll with an offset related to the ScrollView's top left origin, if timeInSecond is omitted, then it will jump to the + * specific offset immediately. + * !#zh 视图内容在规定时间内将滚动到 ScrollView 相对左上角原点的偏移位置, 如果 timeInSecond参数不传,则立即滚动到指定偏移位置。 * @method scrollToOffset - * @param {Vec2} position - A Vec2, the value of which each axis between 0 and (contentSize.width/height - scrollSize.width/height). + * @param {Vec2} offset - A Vec2, the value of which each axis between 0 and maxScrollOffset * @param {Number} [timeInSecond=0] - Scroll time in second, if you don't pass timeInSecond, * the content will jump to the specific offset of ScrollView immediately. * @param {Boolean} [attenuated=true] - Whether the scroll acceleration attenuated, default is true. @@ -498,15 +499,21 @@ var ScrollView = cc.Class({ * var maxScrollOffset = this.getMaxScrollOffset(); * scrollView.scrollToOffset(cc.p(maxScrollOffset.x / 2, 0), 0.1); */ - scrollToOffset: function(position, timeInSecond, attenuated) { + scrollToOffset: function(offset, timeInSecond, attenuated) { var maxScrollOffset = this.getMaxScrollOffset(); - var anchor = cc.pClamp(cc.p(position.x / maxScrollOffset.x, (maxScrollOffset.y - position.y ) / maxScrollOffset.y), + var anchor = cc.pClamp(cc.p(offset.x / maxScrollOffset.x, (maxScrollOffset.y - offset.y ) / maxScrollOffset.y), cc.p(0, 0), cc.p(1, 1)); this.scrollTo(anchor, timeInSecond, attenuated); }, + /** + * !#en Get the current scroll offset related to scrollview 's top left origin. + * !#zh 获取滚动视图相对于左上角原点的当前滚动偏移 + * @method getScrollOffset + * @return {Vec2} - A Vec2 value indicate the current scroll offset. + */ getScrollOffset: function() { var bottomDeta = Math.abs(this._getContentBottomBoundary() - this._bottomBoundary); var leftDeta = Math.abs(this._getContentLeftBoundary() - this._leftBoundary); @@ -515,6 +522,12 @@ var ScrollView = cc.Class({ return cc.p(leftDeta, maxScrollOffset.y - bottomDeta); }, + /** + * !#en Get the maximize available scroll offset + * !#zh 获取滚动视图最大可以滚动的偏移量 + * @method getMaxScrollOffset + * @return {Ve2c} - A Vec2 value indicate the maximize scroll offset in x and y axis. + */ getMaxScrollOffset: function() { var scrollSize = this.node.getContentSize(); var contentSize = this.content.getContentSize(); From 341633201f8e0d1de86d19d05e32e6365a9dc37a Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Tue, 31 May 2016 10:29:46 +0800 Subject: [PATCH 3/3] improve docs --- cocos2d/core/components/CCScrollView.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/components/CCScrollView.js b/cocos2d/core/components/CCScrollView.js index 5e55fdc4ca..ab04c87812 100644 --- a/cocos2d/core/components/CCScrollView.js +++ b/cocos2d/core/components/CCScrollView.js @@ -509,7 +509,7 @@ var ScrollView = cc.Class({ }, /** - * !#en Get the current scroll offset related to scrollview 's top left origin. + * !#en Get the positive offset value corresponds to the content's top left boundary. * !#zh 获取滚动视图相对于左上角原点的当前滚动偏移 * @method getScrollOffset * @return {Vec2} - A Vec2 value indicate the current scroll offset. @@ -526,7 +526,7 @@ var ScrollView = cc.Class({ * !#en Get the maximize available scroll offset * !#zh 获取滚动视图最大可以滚动的偏移量 * @method getMaxScrollOffset - * @return {Ve2c} - A Vec2 value indicate the maximize scroll offset in x and y axis. + * @return {Vec2} - A Vec2 value indicate the maximize scroll offset in x and y axis. */ getMaxScrollOffset: function() { var scrollSize = this.node.getContentSize();