123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- cc.Class({
- extends: cc.Component,
- properties: {
- itemTemplate: { // item template to instantiate other items
- default: null,
- type: cc.Node
- },
- scrollView: {
- default: null,
- type: cc.ScrollView
- },
- spawnCount: 0, // how many items we actually spawn
- totalCount: 0, // how many items we need for the whole list
- spacing: 0, // space between each item
- bufferZone: 0, // when item is away from bufferZone, we relocate it
- lblScrollEvent: cc.Label,
- btnAddItem: cc.Button,
- btnRemoveItem: cc.Button,
- btnJumpToPosition: cc.Button,
- lblJumpPosition: cc.Label,
- lblTotalItems: cc.Label,
- totalRankUrl:"",
- timeline:"0",
- count:10,
- // item:[],
- ver:100,
- os:1,
- channel:"wexin",
- hasNext:true,
- },
- // use this for initialization
- onLoad: function () {
- this.content = this.scrollView.content;
- // console.log(this.items);
- this.updateTimer = 0;
- this.updateInterval = 0.2;
- this.lastContentPosY = 0; // use this variable to detect if we are scrolling up or down
- },
- onEnable:function(){
- if(this.items!=undefined){
- this.items.clear();
- }else{
- this.items = [];
- }
- this.getSystemInfo();
- },
- getUserInfo:function(){
- wx.getStorage({
- key:"userInfo",
- success:(res)=>{
- this.getTotalRank(res.data);
- }
- })
- },
- getSystemInfo:function(){
- if(wx!=undefined){
- wx.getSystemInfo({
- success:(res)=>{
- this.os = res.platform=="android"?2:1;
- this.getUserInfo();
- }
- })
- }
- },
- getTotalRank:function(userInfo){
- console.log(userInfo.token)
- console.log(userInfo.uid)
- if(wx!=undefined){
- var self = this;
- console.log("去网络数据");
- if(this.hasNext){
- wx.request({
- url:self.totalRankUrl,
- data:{
- token:userInfo.token,
- uid:userInfo.uid,
- channel:self.channel,
- os:self.os,
- ver:self.ver,
- timeline:self.timeline,
- count:self.count
- },
- success:(response)=>{
- console.log(response);
- var data =response.data.data;
- if(data!=undefined){
- console.log(this.users);
- console.log(data);
- this.timeline = data.timeline;
- this.hasNext = data.next>0;
- this.users = data.users;
- // this.users.concat(data.users);
- this.initialize(data.users);
- }
- },
- fail:()=>{
- wx.showToast({
- title:"网络请求出错",
- })
- }
- })
- }else{
- wx.showToast({
- title:"没有更多了~"
- })
- }
- }
- },
- initialize: function () {
- this.content.height = this.totalCount * (this.itemTemplate.height + this.spacing) + this.spacing; // get total content height
- for (let i = 0; i < this.spawnCount; ++i) { // spawn items, we only need to do this once
- let item = cc.instantiate(this.itemTemplate);
- this.content.addChild(item);
- item.setPosition(0, -item.height * (0.5 + i) - this.spacing * (i + 1));
- item.getComponent('Item').updateItem(this.users);
- this.items.push(item);
- }
- },
- getPositionInView: function (item) { // get item position in scrollview's node space
- let worldPos = item.parent.convertToWorldSpaceAR(item.position);
- let viewPos = this.scrollView.node.convertToNodeSpaceAR(worldPos);
- return viewPos;
- },
- update: function(dt) {
- this.updateTimer += dt;
- if (this.updateTimer < this.updateInterval) return; // we don't need to do the math every frame
- this.updateTimer = 0;
- let items = this.items;
- let buffer = this.bufferZone;
- let isDown = this.scrollView.content.y < this.lastContentPosY; // scrolling direction
- let offset = (this.itemTemplate.height + this.spacing) * items.length;
- for (let i = 0; i < items.length; ++i) {
- let viewPos = this.getPositionInView(items[i]);
- if (isDown) {
- // if away from buffer zone and not reaching top of content
- if (viewPos.y < -buffer && items[i].y + offset < 0) {
- items[i].setPositionY(items[i].y + offset );
- let item = items[i].getComponent('Item');
- item.updateItem(this.users[i]);
- }
- } else {
- // if away from buffer zone and not reaching bottom of content
- if (viewPos.y > buffer && items[i].y - offset > -this.content.height) {
- items[i].setPositionY(items[i].y - offset );
- let item = items[i].getComponent('Item');
- item.updateItem(this.users[i]);
- }
- }
- }
- // update lastContentPosY
- this.lastContentPosY = this.scrollView.content.y;
- // this.lblTotalItems.textKey = "Total Items: " + this.totalCount;
- },
- scrollEvent: function(sender, event) {
- switch(event) {
- case 0:
- this.lblScrollEvent.string = "Scroll to Top";
- break;
- case 1:
- this.lblScrollEvent.string = "Scroll to Bottom";
- break;
- case 2:
- this.lblScrollEvent.string = "Scroll to Left";
- break;
- case 3:
- this.lblScrollEvent.string = "Scroll to Right";
- break;
- case 4:
- this.lblScrollEvent.string = "Scrolling";
- break;
- case 5:
- this.lblScrollEvent.string = "Bounce Top";
- break;
- case 6:
- this.lblScrollEvent.string = "Bounce bottom";
- break;
- case 7:
- this.lblScrollEvent.string = "Bounce left";
- break;
- case 8:
- this.lblScrollEvent.string = "Bounce right";
- break;
- case 9:
- this.lblScrollEvent.string = "Auto scroll ended";
- break;
- }
- },
- addItem: function() {
- this.content.height = (this.totalCount + 1) * (this.itemTemplate.height + this.spacing) + this.spacing; // get total content height
- this.totalCount = this.totalCount + 1;
- },
- removeItem: function() {
- if (this.totalCount - 1 < 30) {
- cc.error("can't remove item less than 30!");
- return;
- }
- this.content.height = (this.totalCount - 1) * (this.itemTemplate.height + this.spacing) + this.spacing; // get total content height
- this.totalCount = this.totalCount - 1;
- },
- scrollToFixedPosition: function () {
- this.scrollView.scrollToOffset(cc.p(0, 500), 2);
- }
- });
|