同步操作将从 向前/ScrollViewNesting 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
ScrollView/PageView nesting scheme.
Based on CocosCreator2.4.x
If you look at the source code, you will find that every function that handles touch (ignoring the wheel) has these two lines at the beginning.
_onTouchBegan(event, captureListeners) {
if (!this.enabledInHierarchy) return;
if (this._hasNestedViewGroup(event, captureListeners)) return;
...
}
The reason why nesting cannot be done lies in the _hasNestedViewGroup function
...
if (event.target.getComponent(cc.ViewGroup)) {
return true;
}
...
At this time, most friends may inherit ScrollView or PageView, and then rewrite some methods to solve the nesting problem.
But let's try a different angle here. For example, we manually emit a fake event so that it will not be filtered out by _hasNestedViewGroup, and our goal is to make it a separate component.
There is not much code, just upload the source code (freshly released, no bugs tested)
Demo project address: https://gitee.com/cocos2d-zp/scrollview-nesting
const { ccclass, property } = cc._decorator;
interface EventTouch extends cc.Event.EventTouch {
simulate?: boolean
sham?: boolean
}
@ccclass
export default class ViewGroupNesting extends cc.Component {
private events: EventTouch[] = [];
onLoad() {
this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchHandle, this, true);
this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchHandle, this, true);
this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchHandle, this, true);
this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchHandle, this, true);
}
private onTouchHandle(event: EventTouch) {
if (event.sham || event.simulate || event.target === this.node) return;
const cancelEvent: EventTouch = new cc.Event.EventTouch(event.getTouches(), event.bubbles);
cancelEvent.type = event.type;
cancelEvent.touch = event.touch;
cancelEvent.sham = true;
// Question: Why not dispatchEvent directly here?
// Answer: The ScrollView must consume the real event first, and then we can launch the fake one.
// You can go to CCNode.js to find a _doDispatchEvent function, which uses a global variable like _cachedArray.
// If the false is emitted first, the true data will be cleared.
this.events.push(cancelEvent);
}
update() {
if (this.events.length === 0) return;
for (let index = 0; index < this.events.length; index++) {
this.node.dispatchEvent(this.events[index]);
}
this.events.length = 0;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。