SVBetter.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Learn cc.Class:
  2. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
  4. // Learn Attribute:
  5. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
  10. cc.Class({
  11. extends: cc.Component,
  12. properties: {
  13. // foo: {
  14. // // ATTRIBUTES:
  15. // default: null, // The default value will be used only when the component attaching
  16. // // to a node for the first time
  17. // type: cc.SpriteFrame, // optional, default is typeof default
  18. // serializable: true, // optional, default is true
  19. // },
  20. // bar: {
  21. // get () {
  22. // return this._bar;
  23. // },
  24. // set (value) {
  25. // this._bar = value;
  26. // }
  27. // },
  28. item: cc.Prefab,
  29. content: cc.Node,
  30. scrollView: cc.Node,
  31. maskNode: cc.Node
  32. },
  33. // LIFE-CYCLE CALLBACKS:
  34. // onLoad () {},
  35. start() {
  36. this.scrollview = this.node.getChildByName("scrollview")
  37. this.view = this.scrollview.getChildByName("view")
  38. this.more = this.view.getChildByName("more")
  39. this.morett = this.more.getComponent(cc.Label)
  40. this.scrollView.on('scroll-to-bottom', function () {
  41. cc.log('滚动到列表底部了');
  42. // this.morett.string = "松手加载更多"
  43. if (this.onMoreListener) {
  44. this.onMoreListener()
  45. }
  46. }.bind(this));
  47. this.scrollView.on('scrolling', function (event) {
  48. cc.log('正在滚动',this.content.y,"高",this.content.height,"原本的高度",
  49. this.oldY,"计算的高度",this.content.y+this.oldY);
  50. if ( this.content.y+this.oldY >= this.content.height*0.98) {
  51. cc.log('现在要显示哦');
  52. if (this.more.active == false) {
  53. this.more.active = true
  54. // this.morett.string = "上拉加载更多"
  55. }
  56. }else{
  57. this.more.active = false
  58. }
  59. }.bind(this));
  60. },
  61. onMoreListener(){
  62. },
  63. setMoreListener(onMoreListener){
  64. this.onMoreListener = onMoreListener
  65. },
  66. createList(data) {
  67. this.data = data;
  68. this.itemHeight = 110;//设置每个item的高
  69. this.topIndex = 0;//最上面的item索引id
  70. this.bottomIndex = 9;//最下面的item索引id
  71. this.offsetY = 200;//上下临界坐标补充,点击查看原理,值太小会出现item在边界闪动的情况,因为item可能此时在上下两边时均符合移动的情况,所以就会无限循环移动,此值建议自行调整
  72. //如果觉得这种获取临界坐标的方式比较麻烦,可以多创建几个item,指定屏幕的上下边为边界
  73. let scrollViewPos = this.scrollView.position;//scrollView以屏幕中心为原点的坐标,请自行计算出来
  74. this.topExtremeDistance = scrollViewPos.y + this.scrollView.height / 2 + this.offsetY;//获取item能到达的屏幕上边界y坐标
  75. this.bottomExtremeDistance = scrollViewPos.y - this.scrollView.height / 2 - this.offsetY;//获取item能到达的屏幕下边界y坐标
  76. this.itemsArr = [];//item存储arr
  77. for (let i = 0; i < this.data.length; i++) {
  78. let listItem = cc.instantiate(this.item);
  79. listItem.parent = this.content;
  80. this.updateItem(listItem, this.data[i], i);
  81. }
  82. this.oldY = this.content.y
  83. this.content.height = (data.length ) * this.itemHeight + 50;
  84. },
  85. moreItem(data){
  86. var oldindex = this.data.length
  87. this.data = this.data.concat(data);
  88. cc.log('加载更多 data i',this.data);
  89. // cc.log('加载更多 data',this.data);
  90. // for (let i = oldindex; i < this.data.length; i++) {
  91. // let listItem = cc.instantiate(this.item);
  92. // listItem.parent = this.content;
  93. // this.updateItem(listItem, this.data[i], i);
  94. // }
  95. //
  96. this.oldY = this.content.y
  97. this.content.height = (oldindex ) * this.itemHeight + 50;
  98. },
  99. updateItem(listItem, data, i) {
  100. listItem.y = -i * this.itemHeight - this.itemHeight / 2;
  101. listItem.getComponent('SVItem').updateUI(data);
  102. this.itemsArr[i] = listItem;
  103. },
  104. updateItemsPos(dt) {
  105. if (!!this.itemsArr && !!this.itemsArr[this.bottomIndex]) {
  106. //获取上下item当前的坐标
  107. // let topPos = cc.pSub(this.itemsArr[this.topIndex].convertToWorldSpaceAR(cc.v2(0, 0)), cc.v2(cc.winSize.width / 2, cc.winSize.height / 2));
  108. // let bottomPos = cc.pSub(this.itemsArr[this.bottomIndex].convertToWorldSpaceAR(cc.v2(0, 0)), cc.v2(cc.winSize.width / 2, cc.winSize.height / 2));
  109. let topPos = this.itemsArr[this.topIndex].convertToWorldSpaceAR(cc.v2(0, 0)).sub(cc.v2(cc.winSize.width / 2, cc.winSize.height / 2))
  110. let bottomPos = this.itemsArr[this.bottomIndex].convertToWorldSpaceAR(cc.v2(0, 0)).sub(cc.v2(cc.winSize.width / 2, cc.winSize.height / 2))
  111. //检测上item是否超过边界
  112. if (topPos.y > this.topExtremeDistance) {
  113. if (this.bottomIndex >= this.data.length - 1) {
  114. return;
  115. }
  116. this.updateItem(this.itemsArr[this.topIndex], this.data[this.bottomIndex + 1], this.bottomIndex + 1);
  117. this.topIndex++;
  118. this.bottomIndex++;
  119. //检测下item是否超过边界
  120. } else if (bottomPos.y < this.bottomExtremeDistance) {
  121. if (this.topIndex < 1) {
  122. return;
  123. }
  124. this.updateItem(this.itemsArr[this.bottomIndex], this.data[this.topIndex - 1], this.topIndex - 1);
  125. this.topIndex--;
  126. this.bottomIndex--;
  127. }
  128. }
  129. },
  130. onBtnClk: function (event, param) {
  131. switch (param) {
  132. case 'back':
  133. this.node.destroy();
  134. break;
  135. case 'mask':
  136. this.maskNode.getComponent(cc.Mask).enabled = !this.maskNode.getComponent(cc.Mask).enabled;
  137. break;
  138. }
  139. },
  140. update(dt) {
  141. this.updateItemsPos(dt);
  142. },
  143. });