ExportList.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. //前进还是后退
  42. isAdvance: boolean = true;
  43. /**
  44. * 是否在手动拖拽中
  45. */
  46. isHandScroll: boolean = false;
  47. private __items: ExportItem[];
  48. start() {
  49. this.scrollView.node.on('scroll-began', this.onScrollbegin, this);
  50. this.scrollView.node.on('scroll-ended', this.onScrollend, this);
  51. this.scrollView.node.on('scrolling', this.onScrolling, this);
  52. this.updataList();
  53. }
  54. updataList(): void {
  55. //删除老的内容
  56. this.content.removeAllChildren();
  57. this.__items = [];
  58. let adList: any[] = LiangLiangSDK.GetList(20);
  59. console.log("更新导出列表", adList);
  60. let itemNode: Node;
  61. let item: ExportItem;
  62. let data: any;
  63. for (let index = 0; index < adList.length; index++) {
  64. data = adList[index];
  65. itemNode = instantiate(this.itemPrefab);
  66. item = itemNode.getComponent(ExportItem);
  67. this.__items.push(item);
  68. this.content.addChild(itemNode);
  69. item.updateItem(data);
  70. }
  71. this.scrollView.scheduleOnce(this.__scroll.bind(this), 0.1);
  72. }
  73. /**
  74. * 滚动开始
  75. * @param scrollView
  76. */
  77. private onScrollbegin(scrollView): void {
  78. this.scrollView.stopAutoScroll();
  79. }
  80. /**
  81. * 滚动结束
  82. * @param scrollView
  83. */
  84. private onScrollend(scrollView): void {
  85. //手动控制
  86. if (this.isHandScroll) {
  87. this.scheduleOnce(this.__scroll.bind(this), 1);
  88. } else {
  89. this.isAdvance = !this.isAdvance;
  90. this.scheduleOnce(this.__scroll.bind(this), 3.0);
  91. }
  92. this.isHandScroll = false;
  93. }
  94. private __scroll(): void {
  95. let second: number;
  96. if (this.isAdvance) {
  97. if (this.scrollType == ScrollType.Horizontal) {
  98. second = (this.scrollView.getMaxScrollOffset().x - Math.abs(this.scrollView.getScrollOffset().x)) / this.scrollSpeed;
  99. } else {
  100. second = (this.scrollView.getMaxScrollOffset().y - Math.abs(this.scrollView.getScrollOffset().y)) / this.scrollSpeed;
  101. }
  102. if (second > 0) {
  103. if (this.scrollType == ScrollType.Horizontal) {
  104. this.scrollView.scrollToRight(second, false);
  105. } else {
  106. this.scrollView.scrollToBottom(second, false);
  107. }
  108. }
  109. else {
  110. this.onScrollend(null);
  111. }
  112. }
  113. else {
  114. if (this.scrollType == ScrollType.Horizontal) {
  115. second = Math.abs(this.scrollView.getScrollOffset().x) / this.scrollSpeed;
  116. } else {
  117. second = Math.abs(this.scrollView.getScrollOffset().y) / this.scrollSpeed;
  118. }
  119. if (second > 0) {
  120. if (this.scrollType == ScrollType.Horizontal) {
  121. this.scrollView.scrollToLeft(second, false);
  122. } else {
  123. this.scrollView.scrollToTop(second, false);
  124. }
  125. }
  126. else {
  127. this.onScrollend(null);
  128. }
  129. }
  130. }
  131. /**
  132. * 滚动中
  133. * @param scrollView
  134. */
  135. private onScrolling(scrollView): void {
  136. if (this.scrollView.isScrolling()) {
  137. this.isHandScroll = true;
  138. }
  139. }
  140. update(deltaTime: number) {
  141. }
  142. }