Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed offset pos calculation error when page view dynamically added p… #4556

Merged
merged 2 commits into from Jun 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 15 additions & 16 deletions cocos2d/core/components/CCPageView.js
Expand Up @@ -100,6 +100,7 @@ var PageView = cc.Class({
this._curPageIdx = 0;
this._lastPageIdx = 0;
this._pages = [];
this._initContentPos = cc.v2();
this._scrollCenterOffsetX = []; // 每一个页面居中时需要的偏移量(X)
this._scrollCenterOffsetY = []; // 每一个页面居中时需要的偏移量(Y)
},
Expand Down Expand Up @@ -394,20 +395,15 @@ var PageView = cc.Class({
if (!this.content) { return; }
var layout = this.content.getComponent(cc.Layout);
if (layout) {
if (this._pages.length === 0) {
layout.padding = 0;
}
else {
if (this.sizeMode === SizeMode.Free && this._pages.length > 0) {
var lastPage = this._pages[this._pages.length - 1];
if (this.sizeMode === SizeMode.Free) {
if (this.direction === Direction.Horizontal) {
layout.paddingLeft = (this._view.width - this._pages[0].width) / 2;
layout.paddingRight = (this._view.width - lastPage.width) / 2;
}
else if (this.direction === Direction.Vertical) {
layout.paddingTop = (this._view.height - this._pages[0].height) / 2;
layout.paddingBottom = (this._view.height - lastPage.height) / 2;
}
if (this.direction === Direction.Horizontal) {
layout.paddingLeft = (this._view.width - this._pages[0].width) / 2;
layout.paddingRight = (this._view.width - lastPage.width) / 2;
}
else if (this.direction === Direction.Vertical) {
layout.paddingTop = (this._view.height - this._pages[0].height) / 2;
layout.paddingBottom = (this._view.height - lastPage.height) / 2;
}
}
layout.updateLayout();
Expand All @@ -429,13 +425,15 @@ var PageView = cc.Class({
this._lastPageIdx = this._curPageIdx;
}
// 进行排序
var contentPos = this._initContentPos;
for (var i = 0; i < pageCount; ++i) {
this._pages[i].setSiblingIndex(i);
var page = this._pages[i];
page.setSiblingIndex(i);
if (this.direction === Direction.Horizontal) {
this._scrollCenterOffsetX[i] = Math.abs(this.content.x + this._pages[i].x);
this._scrollCenterOffsetX[i] = Math.abs(contentPos.x + page.x);
}
else {
this._scrollCenterOffsetY[i] = Math.abs(this.content.y + this._pages[i].y);
this._scrollCenterOffsetY[i] = Math.abs(contentPos.y + page.y);
}
}

Expand All @@ -460,6 +458,7 @@ var PageView = cc.Class({
// 初始化页面
_initPages: function () {
if (!this.content) { return; }
this._initContentPos = this.content.position;
jareguo marked this conversation as resolved.
Show resolved Hide resolved
var children = this.content.children;
for (var i = 0; i < children.length; ++i) {
var page = children[i];
Expand Down