CocosCreator 2.x 重写scrollview支持scrollview嵌套

// 重写外层部分方法  ------------------------------------------------------------  start

resetBaseFunction(){

this.scrollView._onTouchMoved = (event, captureListeners)=> {

if (!this._scrollViewCanMove)return;

        if (!this.scrollView.enabledInHierarchy)return;

        if (this.scrollView._hasNestedViewGroup &&this.scrollView._hasNestedViewGroup(event, captureListeners))return;

        let touch = event.touch;

        if (this.scrollView.content) {

this.scrollView._handleMoveLogic(touch);

        }

// Do not prevent touch events in inner nodes

// if (!this.scrollView.cancelInnerEvents) {

//    return;

// }

        let deltaMove = touch.getLocation().sub(touch.getStartLocation());

        if (this.scrollView.horizontal &&Math.abs(deltaMove.x) >7 )this.scrollViewIsMoving =true;

        if (this.scrollView.vertical &&Math.abs(deltaMove.y) >7 )this.scrollViewIsMoving =true;

        //FIXME: touch move delta should be calculated by DPI.

        if (deltaMove.mag() >7) {

if (!this.scrollView._touchMoved && event.target !==this.scrollView.node) {

// Simulate touch cancel for target node

                let cancelEvent =new cc.Event.EventTouch(event.getTouches(), event.bubbles);

                cancelEvent.type =cc.Node.EventType.TOUCH_CANCEL;

                cancelEvent.touch = event.touch;

                cancelEvent.simulate =true;

                event.target.dispatchEvent(cancelEvent);

                this.scrollView._touchMoved =true;

            }

}

this.scrollView._stopPropagationIfTargetIsMe(event);

    }

this.scrollView._registerEvent = ()=> {

this.scrollViewNode.on(cc.Node.EventType.TOUCH_START, this.scrollView._onTouchBegan, this.scrollView, true);

        this.scrollViewNode.on(cc.Node.EventType.TOUCH_MOVE, this.scrollView._onTouchMoved, this.scrollView, true);

        this.scrollViewNode.on(cc.Node.EventType.TOUCH_END, (event, captureListeners)=>{

this.scrollView._onTouchEnded(event, captureListeners);

            this.scrollViewIsMoving =false;

        }, this.scrollView, true);

        this.scrollViewNode.on(cc.Node.EventType.TOUCH_CANCEL, (event, captureListeners)=>{

this.scrollView._onTouchCancelled(event, captureListeners);

            this.scrollViewIsMoving =false;

        }, this.scrollView, true);

        // this.scrollViewNode.on(cc.Node.EventType.TOUCH_CANCEL, this.scrollView._onTouchCancelled, this.scrollView, true);

        this.scrollViewNode.on(cc.Node.EventType.MOUSE_WHEEL, this.scrollView._onMouseWheel, this.scrollView, true);

        this.scrollViewNode._touchListener.setSwallowTouches(false);

    }

},

setOutScrollViewCanMove(bool){

this._scrollViewCanMove = bool;

},

// 内层scrollview.host = this;

// 重写外层部分方法  ------------------------------------------------------------  end

// 重写内层部分方法  ------------------------------------------------------------  start

resetBaseFunction(){

this.scrollView._stopPropagationIfTargetIsMe = ()=>{};

this.scrollView._onTouchMoved = (event, captureListeners)=> {

if (this.host.scrollViewIsMoving)return;

        if (!this.scrollView.enabledInHierarchy)return;

        if (this.scrollView._hasNestedViewGroup &&this.scrollView._hasNestedViewGroup(event, captureListeners))return;

        let touch = event.touch;

        if (this.scrollView.content) {

this.scrollView._handleMoveLogic(touch);

        }

// Do not prevent touch events in inner nodes

        if (!this.scrollView.cancelInnerEvents) {

return;

        }

let deltaMove = touch.getLocation().sub(touch.getStartLocation());

        if (this.scrollView.horizontal &&Math.abs(deltaMove.x) >7 )this.host.setOutScrollViewCanMove(false);

        if (this.scrollView.vertical &&Math.abs(deltaMove.y) >7 )this.host.setOutScrollViewCanMove(false);

        //FIXME: touch move delta should be calculated by DPI.

        if (deltaMove.mag() >7) {

if (!this.scrollView._touchMoved && event.target !==this.scrollView.node) {

// Simulate touch cancel for target node

                let cancelEvent =new cc.Event.EventTouch(event.getTouches(), event.bubbles);

                cancelEvent.type =cc.Node.EventType.TOUCH_CANCEL;

                cancelEvent.touch = event.touch;

                cancelEvent.simulate =true;

                event.target.dispatchEvent(cancelEvent);

                this.scrollView._touchMoved =true;

            }

}

this.scrollView._stopPropagationIfTargetIsMe(event);

    }

this.scrollView._registerEvent = ()=> {

this.scrollViewNode.on(cc.Node.EventType.TOUCH_START, this.scrollView._onTouchBegan, this.scrollView, true);

        this.scrollViewNode.on(cc.Node.EventType.TOUCH_MOVE, this.scrollView._onTouchMoved, this.scrollView, true);

        this.scrollViewNode.on(cc.Node.EventType.TOUCH_END, (event, captureListeners)=>{

this.scrollView._onTouchEnded(event, captureListeners);

            this.host.setOutScrollViewCanMove(true);

        }, this.scrollView, true);

        this.scrollViewNode.on(cc.Node.EventType.TOUCH_CANCEL, (event, captureListeners)=>{

this.scrollView._onTouchCancelled(event, captureListeners);

            this.host.setOutScrollViewCanMove(true);

        }, this.scrollView, true);

        // this.scrollViewNode.on(cc.Node.EventType.TOUCH_CANCEL, this.scrollView._onTouchCancelled, this.scrollView, true);

        this.scrollViewNode.on(cc.Node.EventType.MOUSE_WHEEL, this.scrollView._onMouseWheel, this.scrollView, true);

        this.scrollViewNode._touchListener.setSwallowTouches(false);

    }

},

// 重写内层部分方法  ------------------------------------------------------------  end

推荐阅读更多精彩内容