【首创】完美解决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');
? ? ? ? ? ? ? ? }
? ? ? ? });
這些代碼是經過好幾天的研究得到的,請大家尊重別人的勞動成果,轉載標明出處,謝謝。
轉載于:https://www.cnblogs.com/cosiray/archive/2013/05/05/3061143.html
總結
以上是生活随笔為你收集整理的【首创】完美解决scrollview与menu的兼容问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用于检测敏感词的 PHP 扩展
- 下一篇: SMTP 错误代码大全