From 5de39df14e3e3fbca7430ce3a11ecea9deddfd8a Mon Sep 17 00:00:00 2001 From: Nan Wang Date: Tue, 31 May 2016 17:16:19 +0800 Subject: [PATCH] update scrollview with scrollToOffSet api --- cocos2d/core/components/CCScrollView.js | 53 ++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/components/CCScrollView.js b/cocos2d/core/components/CCScrollView.js index 5a802b1ff3..9c1bd966ca 100644 --- a/cocos2d/core/components/CCScrollView.js +++ b/cocos2d/core/components/CCScrollView.js @@ -484,6 +484,57 @@ var ScrollView = cc.Class({ } }, + + /** + * !#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} 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. + * @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(offset, timeInSecond, attenuated) { + var maxScrollOffset = this.getMaxScrollOffset(); + + 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 positive offset value corresponds to the content's top left boundary. + * !#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); + var maxScrollOffset = this.getMaxScrollOffset(); + + return cc.p(leftDeta, maxScrollOffset.y - bottomDeta); + }, + + /** + * !#en Get the maximize available scroll offset + * !#zh 获取滚动视图最大可以滚动的偏移量 + * @method getMaxScrollOffset + * @return {Vec2} - 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(); + + return cc.p(contentSize.width - scrollSize.width, contentSize.height - scrollSize.height); + }, + /** * !#en Scroll the content to the horizontal percent position of ScrollView. * !#zh 视图内容在规定时间内将滚动到 ScrollView 水平方向的百分比位置上。 @@ -1183,4 +1234,4 @@ var ScrollView = cc.Class({ }, }); -cc.ScrollView = module.exports = ScrollView; +cc.ScrollView = module.exports = ScrollView; \ No newline at end of file