zoukankan      html  css  js  c++  java
  • Cocos Creator scrollview添加事件的两种方法

    scrollview添加事件

    方法一
    这种方法添加的事件回调和使用编辑器添加的事件回调是一样的,通过代码添加, 你需要首先构造一个 cc.Component.EventHandler 对象,然后设置好对应的 target, component, handler 和 customEventData 参数。
    //here is your component file, file name = MyComponent.js 
    cc.Class({
    extends: cc.Component,
    properties: {},

    onLoad: function () {
        var scrollViewEventHandler = new cc.Component.EventHandler();
        scrollViewEventHandler.target = this.node; //这个 node 节点是你的事件处理代码组件所属的节点
        scrollViewEventHandler.component = "MyComponent";//这个是代码文件名
        scrollViewEventHandler.handler = "callback";
        scrollViewEventHandler.customEventData = "foobar";
    
        var scrollview = node.getComponent(cc.ScrollView);
        scrollview.scrollEvents.push(scrollViewEventHandler);
    },
    
    //注意参数的顺序和类型是固定的
    callback: function (scrollview, eventType, customEventData) {
        //这里 scrollview 是一个 Scrollview 组件对象实例
        //这里的 eventType === cc.ScrollView.EventType enum 里面的值
        //这里的 customEventData 参数就等于你之前设置的 "foobar"
    }

    });

    方法二
    通过 scrollview.node.on('scroll-to-top', ...) 的方式来添加
    //假设我们在一个组件的 onLoad 方法里面添加事件处理回调,在 callback 函数中进行事件处理:
    cc.Class({
    extends: cc.Component,
    properties: {
    scrollview: cc.ScrollView
    },

    onLoad: function () {
       this.scrollview.node.on('scroll-to-top', this.callback, this);
    },
    
    callback: function (event) {
       //这里的 event 是一个 EventCustom 对象,你可以通过 event.detail 获取 ScrollView 组件
       var scrollview = event.detail;
       //do whatever you want with scrollview
       //另外,注意这种方式注册的事件,也无法传递 customEventData
    }

    });

  • 相关阅读:
    5G NR系列(四)物理下行共享信道(PDSCH)物理层过程详解
    5G NR系列(三)PDSCH的解调参考信号(DM-RS)
    Mac上重装pycharm打不开的解决方法
    Oracle parallel理解
    V$ASM_DISKGROUP视图信息解读
    深入了解 Oracle Flex ASM 及其优点
    使用typora和印象笔记高效输出
    Centos7.6部署k8s 集群
    DBA日常职责
    利用DCLI命令实现跨机器检查
  • 原文地址:https://www.cnblogs.com/luorende/p/7882371.html
Copyright © 2011-2022 走看看