ab5e3153-67e6-4c8c-8b91-68aeb4490cff.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. "use strict";
  2. cc._RF.push(module, 'ab5e3FTZ+ZMjIuRaK60SQz/', 'YZ_ListView');
  3. // common-plugin/Scripts/YZ_ListView.ts
  4. "use strict";
  5. var __extends = (this && this.__extends) || (function () {
  6. var extendStatics = function (d, b) {
  7. extendStatics = Object.setPrototypeOf ||
  8. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  9. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  10. return extendStatics(d, b);
  11. };
  12. return function (d, b) {
  13. extendStatics(d, b);
  14. function __() { this.constructor = d; }
  15. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  16. };
  17. })();
  18. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  19. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  20. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  21. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  22. return c > 3 && r && Object.defineProperty(target, key, r), r;
  23. };
  24. Object.defineProperty(exports, "__esModule", { value: true });
  25. var YZ_Constant_1 = require("./YZ_Constant");
  26. var CompatibleTool_1 = require("./CompatibleTool");
  27. var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
  28. var YZ_ListView = /** @class */ (function (_super) {
  29. __extends(YZ_ListView, _super);
  30. function YZ_ListView() {
  31. var _this = _super !== null && _super.apply(this, arguments) || this;
  32. _this._scrollView = null;
  33. _this._content = null;
  34. _this._itemObj = null;
  35. _this._visibleCount = 5;
  36. _this._totalCount = 0;
  37. _this._itemSize = cc.size(0, 0);
  38. _this._spaceX = 0;
  39. _this._bannerSize = cc.size(0, 0);
  40. _this._itemArray = [];
  41. _this._curOffsetX = 0;
  42. _this._minOffsetX = 0;
  43. _this._maxOffsetX = 0;
  44. // 起始位置的索引值
  45. _this._startIndex = 0;
  46. // 滑动方向 -1 向左, 1 向右
  47. _this._scrollDir = 0;
  48. // 交叉推广数据,数组
  49. _this._recommendData = null;
  50. _this._dataDirty = false;
  51. _this._isInit = false;
  52. _this._isScorllBar = false; //是否滚动条
  53. _this._location = null; //显示位置
  54. _this._itemPosX = 0;
  55. _this._itemtmp = null;
  56. _this._itemToCenterLength = 0;
  57. _this._percent = 1;
  58. _this._canAutoScroll = true;
  59. _this._autoScrollInterval = 2.5;
  60. _this._timeTmp = 0;
  61. return _this;
  62. }
  63. YZ_ListView.prototype.onLoad = function () {
  64. this._scrollView = this.getComponent(cc.ScrollView);
  65. this._content = this._scrollView.content;
  66. this._itemObj = cc.find("GamePage", this._content);
  67. this._itemSize = this._itemObj.getContentSize();
  68. this._startIndex = 0;
  69. this._bannerSize = this.node.getContentSize();
  70. };
  71. YZ_ListView.prototype.onEnable = function () {
  72. this._scrollView.node.on("scrolling", this.OnScroll, this);
  73. this._scrollView.node.on("scroll-ended", this.onScrollEnded, this);
  74. };
  75. YZ_ListView.prototype.onDisable = function () {
  76. this._scrollView.node.targetOff(this);
  77. };
  78. YZ_ListView.prototype.update = function (dt) {
  79. if (this._isInit) {
  80. if (this._scrollView) {
  81. if (this._scrollView.isScrolling) {
  82. for (var i = 0; i < this._itemArray.length; i++) {
  83. this._itemtmp = this._itemArray[i];
  84. this._itemToCenterLength = Math.abs(this._itemtmp.x + this._content.x - this._bannerSize.width * 0.5);
  85. if ((this._itemSize.width - this._itemToCenterLength) > 0) {
  86. this._percent = (this._itemSize.width - this._itemToCenterLength) / this._itemSize.width;
  87. // if (!this._isScorllBar) {
  88. // this._itemtmp.scale = 1 + this._percent * 0.2;
  89. // }
  90. }
  91. else {
  92. if (!this._isScorllBar) {
  93. this._itemtmp.scale = 1;
  94. }
  95. }
  96. }
  97. }
  98. }
  99. this.autoScroll(dt);
  100. }
  101. if (this._dataDirty) {
  102. this._dataDirty = false;
  103. this._updateContent();
  104. }
  105. };
  106. YZ_ListView.prototype.init = function (data, isScorllBar) {
  107. if (isScorllBar === void 0) { isScorllBar = false; }
  108. if (data) {
  109. this._recommendData = data;
  110. this._totalCount = data.length;
  111. this._dataDirty = true;
  112. this._isScorllBar = isScorllBar;
  113. if (this._isScorllBar) {
  114. this._location = YZ_Constant_1.SubLocation.isScrollbar;
  115. this._autoScrollInterval = 0.3;
  116. }
  117. else {
  118. this._location = YZ_Constant_1.SubLocation.isYzBanner;
  119. }
  120. }
  121. };
  122. YZ_ListView.prototype._updateContent = function () {
  123. // 初始化content大小
  124. var num = this._recommendData.length;
  125. this._content.setContentSize(cc.size(this._itemSize.width * num, this._itemSize.height));
  126. this.InitObjs();
  127. };
  128. YZ_ListView.prototype.InitObjs = function () {
  129. var obj = null;
  130. for (var i = 0; i < this._totalCount; i++) {
  131. obj = cc.instantiate(this._itemObj);
  132. obj.x = this._spaceX * 0.5 + this._itemSize.width * 0.5 + this._itemSize.width * i + this._spaceX * i;
  133. obj.y = 0;
  134. obj.getComponentInChildren("GameItem").init(this._recommendData[i], this._location);
  135. this._content.addChild(obj);
  136. this._itemArray.push(obj);
  137. }
  138. var unvisibleCount = this._totalCount - this._visibleCount;
  139. this._maxOffsetX = (this._spaceX + this._itemSize.width) * unvisibleCount * -1;
  140. this._itemObj.destroy();
  141. this._isInit = true;
  142. };
  143. YZ_ListView.prototype.OnScroll = function (scrollview, eventType, customEventData) {
  144. this._content.stopAllActions();
  145. this._canAutoScroll = false;
  146. if (this._scrollView.getScrollOffset().x < this._curOffsetX) {
  147. this._scrollDir = -1;
  148. }
  149. else {
  150. this._scrollDir = 1;
  151. }
  152. this._curOffsetX = this._scrollView.getScrollOffset().x;
  153. if (this._scrollView.getScrollOffset().x == 0) {
  154. // utils.showLog("到达最左端......");
  155. // 向右滑动,到达最左端
  156. this._arriveLeft();
  157. }
  158. else if (this._scrollView.getScrollOffset().x == this._maxOffsetX) {
  159. // utils.showLog("到达最右端......");
  160. // 向左滑动,到达最右端
  161. this._arriveRight();
  162. }
  163. };
  164. YZ_ListView.prototype.onScrollEnded = function (scrollview, eventType, customEventData) {
  165. var stopIdx = 0;
  166. var tmp = Math.abs(this._curOffsetX) % (this._itemSize.width + this._spaceX);
  167. if (tmp > (this._itemSize.width + this._spaceX) * 0.5) {
  168. // 前进一格
  169. stopIdx = Math.ceil(Math.abs(this._curOffsetX) / (this._itemSize.width + this._spaceX));
  170. }
  171. else {
  172. // 后退一格
  173. stopIdx = Math.floor(Math.abs(this._curOffsetX) / (this._itemSize.width + this._spaceX));
  174. }
  175. var posX = (this._itemSize.width + this._spaceX) * stopIdx * -1;
  176. this._curOffsetX = posX;
  177. this.scroll(posX, 0.3);
  178. };
  179. YZ_ListView.prototype._arriveLeft = function () {
  180. this._scrollView.setContentPosition(CompatibleTool_1.default.position(this._maxOffsetX, 0));
  181. this._curOffsetX = this._maxOffsetX;
  182. this._startIndex = (this._visibleCount + this._startIndex) % this._totalCount;
  183. this.refreshItem();
  184. };
  185. YZ_ListView.prototype._arriveRight = function () {
  186. this._scrollView.setContentPosition(CompatibleTool_1.default.position(0, 0));
  187. this._curOffsetX = 0;
  188. this._startIndex = (this._startIndex + this._totalCount - this._visibleCount) % this._totalCount;
  189. this.refreshItem();
  190. };
  191. YZ_ListView.prototype.refreshItem = function () {
  192. var posIdx = 0;
  193. var obj = null;
  194. for (var i = 0; i < this._totalCount; i++) {
  195. posIdx = (this._totalCount - this._startIndex + i) % this._totalCount;
  196. obj = this._itemArray[i];
  197. obj.x = this._spaceX * 0.5 + this._itemSize.width * 0.5 + this._spaceX * posIdx + this._itemSize.width * posIdx;
  198. }
  199. };
  200. YZ_ListView.prototype.autoScroll = function (dt) {
  201. if (this._canAutoScroll) {
  202. this._timeTmp += dt;
  203. if (this._timeTmp >= this._autoScrollInterval) {
  204. this._timeTmp = 0;
  205. var posX = this._curOffsetX - this._itemSize.width - this._spaceX;
  206. this._curOffsetX = posX;
  207. this._scrollDir = -1;
  208. if (this._isScorllBar) {
  209. this.scroll(posX, 0);
  210. }
  211. else {
  212. this.scroll(posX, 0.5);
  213. }
  214. }
  215. }
  216. else {
  217. this._timeTmp = 0;
  218. }
  219. };
  220. YZ_ListView.prototype.scroll = function (posX, duration) {
  221. var _this = this;
  222. this._content.runAction(cc.sequence(cc.moveTo(duration, CompatibleTool_1.default.position(posX, 0)), cc.callFunc(function () {
  223. _this._canAutoScroll = true;
  224. if (_this._scrollDir == 1 && _this._curOffsetX == 0) {
  225. _this._arriveLeft();
  226. }
  227. else if (_this._scrollDir == -1 && _this._curOffsetX == _this._maxOffsetX) {
  228. _this._arriveRight();
  229. }
  230. })));
  231. };
  232. YZ_ListView = __decorate([
  233. ccclass
  234. ], YZ_ListView);
  235. return YZ_ListView;
  236. }(cc.Component));
  237. exports.default = YZ_ListView;
  238. cc._RF.pop();