js 触摸事件

来源:互联网 发布:域名不续费会怎么样 编辑:程序博客网 时间:2024/06/05 04:49

1.触摸事件 -- 单点

onTouchBegan:function(touch,event){

        var touchPoint = touch.getLocation();
        return true;//不会向下执行其他事件了
    },

onTouchMoved:function (touch, event) {
    },

onTouchEnded:function (touch, event) {
    },


注册事件层

onEnter:function(){
        cc.registerTargettedDelegate(cc.MENU_HANDLER_PRIORITY-1, true, this); //注册层次大于 0 不管用
        this._super();
    },
    onExit:function(){
        cc.unregisterTouchDelegate(this);
        this._super();
    }


2.触摸事件 -- 多点

onTouchesBegan:function (touches, event) {
    },
onTouchesMoved:function (touches, event) {
            if (touches) {
                 cc.p(touches[0].getLocation().x, touches[0].getLocation().y);  //获得触摸地点坐标

                var delta = touches[0].getDelta();   //delta.y 触摸动距离,根据这个可以移动控件
            }
    },
onTouchesEnded:function (touches, event) {},
onTouchesCancelled:function (touches, event) {}




3.屏蔽层

1.简单的屏蔽

(1)按钮实现:在一个 layer 上放一个和屏幕一样大的按钮 menu,设成透明的,所有低于这个按钮的事件都会被这个按钮所接受,不会向下传递。

(2)屏蔽层实现:

onEnter:function(){
        cc.registerTargettedDelegate(cc.MENU_HANDLER_PRIORITY-1, true, this); //设成比按钮的层次低
        this._super();
    },
    onExit:function(){
        cc.unregisterTouchDelegate(this);
        this._super();
    }


onTouchBegan:function(touch,event){
        var pos = this.Menu.convertTouchToNodeSpace(touch);//按钮的 Menu


        var ret = true;
        if(cc.rectContainsPoint(this.ItemMenu.rect(), pos1))//按钮的ItemMenu 
        {
            this._close.activate();//可无
            ret = false;
        }

      return ret;//返回 true 屏蔽所有
    },


2.屏蔽层上又 tableview、scrollview 。。。

按钮实现:这些 UI 事件都低于按钮事件,很多事件都会在设的按钮屏蔽层处理掉,tableview 。。。事件不会响应。

                    table.setTouchPriority(cc.MENU_HANDLER_PRIORITY-2);  //设成比按钮处理事件层高就行了。


3.scrollview上有tableview,滑动tableview时scrollview不动

(1)tableview 所在的 layer 

this.setTouchPriority(cc.MENU_HANDLER_PRIORITY-2);
this.setTouchMode(cc.TOUCH_ONE_BY_ONE);
this.setTouchEnabled(true); 


tableView.setTouchPriority(cc.MENU_HANDLER_PRIORITY-3);


onTouchBegan:function (touch, event) {
        var pos = touch.getLocation();

        if(cc.rectContainsPoint(rect, pos)){//触摸在tableview 上不向下传递则scrollview 无法响应  
            return true;
        }


        return false;
    },


4.通过移除和添加屏蔽层,实现一些屏蔽


4.按钮屏蔽问题

按钮做底层屏蔽一些事件,会影响到有限级别低于按钮的事件,大部分事件都在按钮层处理了。


1.单触摸事件

this.setTouchPriority(cc.MENU_HANDLER_PRIORITY-2);  //层设的比按钮低

this.setTouchEnabled(true);

this.setTouchMode(cc.TOUCH_ONE_BY_ONE);


onTouchBegan:function (touch, event) {  //必须返回 true onTouchMoved才能接到事件

    return true;

},

onTouchMoved 才会响应


层上的其他按钮好使

设置按钮 menu.setTouchPriority(cc.MENU_HANDLER_PRIORITY-3); 




方法

this.setTouchPriority(cc.MENU_HANDLER_PRIORITY-2);
this.setTouchMode(cc.TOUCH_ONE_BY_ONE);
this.setTouchEnabled(true);

0 0