• 【首创】完美解决scrollview与menu的兼容问题


    经过一段时间的学习,才发现CH5里scrollview的例子很少,也没有相关的SAMPLE,于是乎,开始投入研究。大多数scrollview的例子只有在cocos2d-x里才用到,那么CH5里要用到滚动条怎么理呢?有人说用tableview,OMG,这个玩意不但复杂而且累赘,用一个简单的功能要写一大堆代码。OK,哥与scrollview卯上了,最后终于完美解决。估计应该是全论坛首创,因此转载要注明出处。分享研究代码:

    主要解决俩大难题:
    1)滑动优先的问题,如果在scrollview里放Menu不能滑动,并且触发menu事件。
    2)当menu滑动出ScrollView的时候,还可以点击。

    解决方案:
    1)重写Menu
    var MyScrollMenu = cc.Menu.extend({
                    ctor : function () {
                            this._super();
                            cc.associateWithNative(this, cc.Layer);
                            if ('touches' in sys.capabilities || sys.platform == "browser")
                                    this.setTouchEnabled(true);
                            else if ('mouse' in sys.capabilities)
                                    this.setMouseEnabled(true);
                    },
                    registerWithTouchDispatcher : function () {
                            Global.director.getTouchDispatcher().addTargetedDelegate(this, cc.MENU_HANDLER_PRIORITY + 1000, true);
                    },
                    onTouchBegan : function (touch, e) {
                            this.touchPY1 = touch.getLocation().y;
                            if (this._state != cc.MENU_STATE_WAITING || !this._visible || !this._enabled) {
                                    return false;
                            }
                            for (var c = this._parent; c != null; c = c.getParent()) {
                                    if (!c.isVisible()) {
                                            return false;
                                    }
                            }
                            this._selectedItem = this._itemForTouch(touch);
                            if (this._selectedItem) {
                                    this._state = cc.MENU_STATE_TRACKING_TOUCH;
                                    this._selectedItem.selected();
                                    return true;
                            }
                    },
                    onTouchMoved : function (touch, e) {
                            this.touchPY2 = touch.getLocation().y;
                            if (Math.abs(this.touchPY1 - this.touchPY2) > 0 && this._selectedItem) {
                                    this._selectedItem.unselected();
                                    this._selectedItem = null;
                            }
                    },
                    onTouchEnded : function (touch, e) {
                            if (this._selectedItem) {
                                    this._selectedItem.unselected();
                                    this._selectedItem.activate();
                                    Global.audioEngine.playEffect(Res.Sounds.Main.click);
                            }
                            this._state = cc.MENU_STATE_WAITING;
                    }
            });

    MyScrollMenu.create = function () {
            var ret = new MyScrollMenu();

            if (arguments.length == 0) {
                    ret.initWithItems(null, null);
            } else if (arguments.length == 1) {
                    if (arguments[0]instanceof Array) {
                            ret.initWithArray(arguments[0]);
                            return ret;
                    }
            }
            ret.initWithItems(arguments);
            return ret;
    };


    2)添加点击范围判断:
    var scrollViewTestLayer = cc.Layer.extend({
                    ctor : function () {
                            this._super();
                            cc.associateWithNative(this, cc.Layer);

                            if ('touches' in sys.capabilities || sys.platform == "browser")
                                    this.setTouchEnabled(true);
                            else if ('mouse' in sys.capabilities)
                                    this.setMouseEnabled(true);

                            var container = cc.LayerColor.create(cc.c4b(0, 0, 255, 255), 320, 360);
                            container.addChild(new MyScrollMenu() );

                            var scrollView = cc.ScrollView.create(cc.size(320, 300), container);
                            scrollView.setBounceable(true);
                            scrollView.setDirection(1);
                            scrollView.updateInset();

                            scrollView.setPosition(cc.p(0, 120));
                            scrollView.setContentOffset(cc.p(0, 0), true);

                            //scrollView.ignoreAnchorPointForPosition(false);

                            scrollView.setDelegate(this);

                            this.addChild(scrollView);
                    },
                    registerWithTouchDispatcher : function () {
                            Global.director.getTouchDispatcher().addTargetedDelegate(this, cc.MENU_HANDLER_PRIORITY - 100, true);
                    },
                    onTouchBegan : function (touch, e) {
                            //cc.log(touch.getLocation());
                            if (cc.rectContainsPoint(cc.rect(0, 120, 320, 300), touch.getLocation())) {
                                    //cc.log(111111);
                                    return false;
                            } else {
                                    //cc.log(222222);
                                    return true;
                            }
                    },
                    scrollViewDidScroll : function (view) {
                            //cc.log('scrollViewDidScroll');
                    },
                    scrollViewDidZoom : function (view) {
                            //cc.log('scrollViewDidZoom');
                    }
            });


    这些代码是经过好几天的研究得到的,请大家尊重别人的劳动成果,转载标明出处,谢谢。

  • 相关阅读:
    python设计模式-单例模式
    bash脚本条件测试总结
    Python网络编程:IO多路复用
    Python面向对象高级编程:@property--把方法变为属性
    Sql Server存储过程基本语法
    接口幂等性
    [转载]使用消息队列实现分布式事务-公认较为理想的分布式事务解决方案
    C#分布式事务解决方案-TransactionScope
    SOA架构和微服务架构的区别
    Atlas实现数据库读写分离
  • 原文地址:https://www.cnblogs.com/cosiray/p/3061143.html
Copyright © 2020-2023  润新知