UI-ScrollView制作ListView
const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.ScrollView)
    scrollview:cc.ScrollView = null

    @property(cc.Node)
    itemPrefab:cc.Node = null

    @property(cc.Node)
    content:cc.Node = null

    start () {
        let itemNum = 20
        let space = this.content.getComponent(cc.Layout).spacingY
        //要重新计算内容的高度 内容添加一个Layout组件就不用自己设定高度位置
        //设置Layout的ResizeMode为自动计算大小也就不用自己计算内容大小了
        this.content.height = (this.itemPrefab.height + space) * itemNum
        for(let i = 0;i < itemNum;i++)
        {
            let item = cc.instantiate(this.itemPrefab)
            this.content.addChild(item)
            item.x = 0
        }

        let action = new cc.Tween()
        action.target(this).delay(5).call(()=>{
            //2秒滑动到指定位置
            this.scrollview.scrollToOffset(new cc.Vec2(0,800),2)
        }).start()
    }
}

首页 我的博客
粤ICP备17103704号