日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/8 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【首创】完美解决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的兼容问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。