ExportList.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { CCBoolean, CCInteger, CCObject, Component, Enum, find, instantiate, Node, Prefab, ScrollViewComponent, _decorator } from 'cc';
  2. import { LiangLiangSDK } from '../../../libs/liangliangSDK';
  3. import { ExportItem } from './ExportItem';
  4. const { ccclass, property } = _decorator;
  5. export enum ScrollType {
  6. Horizontal = 1,
  7. Vertical = 2
  8. }
  9. @ccclass('ExportList')
  10. export class ExportList extends Component {
  11. @property({
  12. type: ScrollViewComponent,
  13. tooltip: "项预制体"
  14. })
  15. scrollView: ScrollViewComponent = null;
  16. @property({
  17. type: Node,
  18. tooltip: "项预制体"
  19. })
  20. content: Node = null;
  21. @property({
  22. type: Prefab,
  23. tooltip: "项预制体"
  24. })
  25. itemPrefab: Prefab = null;
  26. @property({
  27. type: Enum(ScrollType),
  28. tooltip: "滚动方式"
  29. })
  30. scrollType: ScrollType = ScrollType.Horizontal;
  31. @property({
  32. type: CCBoolean,
  33. tooltip: "是否开启自动滚动"
  34. })
  35. autoScroll:boolean = true;
  36. @property({
  37. type: CCInteger,
  38. tooltip: "滚动速度"
  39. })
  40. scrollSpeed: number = 50;
  41. @property({
  42. type: CCBoolean,
  43. tooltip: "取消是否到全屏导出"
  44. })
  45. isShowFull:boolean = true;
  46. //前进还是后退
  47. isAdvance: boolean = true;
  48. /**
  49. * 是否在手动拖拽中
  50. */
  51. isHandScroll: boolean = false;
  52. private __items: ExportItem[];
  53. start() {
  54. this.scrollView.node.on('scroll-began', this.onScrollbegin, this);
  55. this.scrollView.node.on('scroll-ended', this.onScrollend, this);
  56. this.scrollView.node.on('scrolling', this.onScrolling, this);
  57. this.updataList();
  58. }
  59. updataList(): void {
  60. //删除老的内容
  61. this.content.removeAllChildren();
  62. this.__items = [];
  63. let adList: any[] = LiangLiangSDK.GetList();
  64. console.log("更新导出列表", adList);
  65. let itemNode: Node;
  66. let item: ExportItem;
  67. let data: any;
  68. for (let index = 0; index < adList.length; index++) {
  69. data = adList[index];
  70. itemNode = instantiate(this.itemPrefab);
  71. item = itemNode.getComponent(ExportItem);
  72. this.__items.push(item);
  73. this.content.addChild(itemNode);
  74. item.updateItem(data, this.isShowFull);
  75. }
  76. this.scrollView.scheduleOnce(this.__scroll.bind(this), 0.5);
  77. }
  78. /**
  79. * 滚动开始
  80. * @param scrollView
  81. */
  82. private onScrollbegin(scrollView): void {
  83. this.scrollView.stopAutoScroll();
  84. }
  85. /**
  86. * 滚动结束
  87. * @param scrollView
  88. */
  89. private onScrollend(scrollView): void {
  90. //手动控制
  91. if (this.isHandScroll) {
  92. this.scheduleOnce(this.__scroll.bind(this), 1);
  93. } else {
  94. this.isAdvance = !this.isAdvance;
  95. this.scheduleOnce(this.__scroll.bind(this), 3.0);
  96. }
  97. this.isHandScroll = false;
  98. }
  99. private __scroll(): void {
  100. let second: number;
  101. if (this.isAdvance) {
  102. if (this.scrollType == ScrollType.Horizontal) {
  103. second = (this.scrollView.getMaxScrollOffset().x - Math.abs(this.scrollView.getScrollOffset().x)) / this.scrollSpeed;
  104. } else {
  105. second = (this.scrollView.getMaxScrollOffset().y - Math.abs(this.scrollView.getScrollOffset().y)) / this.scrollSpeed;
  106. }
  107. if (second > 0) {
  108. if (this.scrollType == ScrollType.Horizontal) {
  109. this.scrollView.scrollToRight(second, false);
  110. } else {
  111. this.scrollView.scrollToBottom(second, false);
  112. }
  113. }
  114. else {
  115. this.onScrollend(null);
  116. }
  117. }
  118. else {
  119. if (this.scrollType == ScrollType.Horizontal) {
  120. second = Math.abs(this.scrollView.getScrollOffset().x) / this.scrollSpeed;
  121. } else {
  122. second = Math.abs(this.scrollView.getScrollOffset().y) / this.scrollSpeed;
  123. }
  124. if (second > 0) {
  125. if (this.scrollType == ScrollType.Horizontal) {
  126. this.scrollView.scrollToLeft(second, false);
  127. } else {
  128. this.scrollView.scrollToTop(second, false);
  129. }
  130. }
  131. else {
  132. this.onScrollend(null);
  133. }
  134. }
  135. }
  136. /**
  137. * 滚动中
  138. * @param scrollView
  139. */
  140. private onScrolling(scrollView): void {
  141. if (this.scrollView.isScrolling()) {
  142. this.isHandScroll = true;
  143. }
  144. }
  145. update(deltaTime: number) {
  146. }
  147. }