ScrollWithPage.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. var t = require;
  2. var e = module;
  3. var i = exports;
  4. e =
  5. (this && this.__decorate) ||
  6. function(t, e, i, s) {
  7. var o,
  8. a = arguments.length,
  9. n = a < 3 ? e : null === s ? (s = Object.getOwnPropertyDescriptor(e, i)) : s;
  10. if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) n = Reflect.decorate(t, e, i, s);
  11. else
  12. for (var r = t.length - 1; 0 <= r; r--)
  13. (o = t[r]) && (n = (a < 3 ? o(n) : 3 < a ? o(e, i, n) : o(e, i)) || n);
  14. return 3 < a && n && Object.defineProperty(e, i, n), n;
  15. };
  16. Object.defineProperty(i, "__esModule", { value: !0 });
  17. const s = t("ItemRenderer"),
  18. { ccclass: o, property: a, menu: n } = cc._decorator;
  19. t = class extends cc.ScrollView {
  20. constructor() {
  21. super(...arguments),
  22. (this.itemRender = null),
  23. (this._curPageIdx = 0),
  24. (this._lastPageIdx = 0),
  25. (this.pageTurningSpeed = 0.3),
  26. (this.scrollThreshold = 0.5),
  27. (this.autoPageTurningThreshold = 100),
  28. (this.pages = null),
  29. (this.scaleCenterDist = 10),
  30. (this.scaleMin = 0.7),
  31. (this.alphaMin = 127.5),
  32. (this.items = []),
  33. (this.itemsPool = []);
  34. }
  35. setData(t) {
  36. var i = this;
  37. (this.pages = t),
  38. (this.itemRendererClass = this.itemRendererClass || s.default),
  39. this.items.map(function(t) {
  40. t.removeFromParent(!1), i.itemsPool.push(t);
  41. }),
  42. (this.items = []),
  43. this.pages.map(function(t) {
  44. var e = null;
  45. 0 < i.itemsPool.length ?
  46. ((e = i.itemsPool[0]), i.itemsPool.splice(0, 1)) :
  47. (e = cc.instantiate(i.itemRender)),
  48. i.items.push(e),
  49. (e.y = 0),
  50. i.content.addChild(e),
  51. e.getComponent(i.itemRendererClass).setData(t);
  52. }),
  53. (this.node.width = this.itemRender.width),
  54. this.updatePageView();
  55. }
  56. updatePageView() {
  57. var t = this.content.getComponent(cc.Layout);
  58. t && t.enabled && t.updateLayout();
  59. t = this.pages.length;
  60. this._curPageIdx >= t && ((this._curPageIdx = 0 === t ? 0 : t - 1), (this._lastPageIdx = this._curPageIdx));
  61. }
  62. setCurrentPageIndex(t) {
  63. this.scrollToPage(t, void 0);
  64. }
  65. scrollToPage(t, e) {
  66. t < 0 ||
  67. t >= this.pages.length ||
  68. ((e = void 0 !== e ? e : 0.3),
  69. (this._curPageIdx = t),
  70. this.scrollToOffset(this._moveOffsetValue(t), e, !0));
  71. }
  72. getCurrentPage() {
  73. return this._curPageIdx;
  74. }
  75. getCurrentPageData() {
  76. return this.pages[this._curPageIdx];
  77. }
  78. onEnable() {
  79. this.registerEvent(), super.onEnable();
  80. }
  81. onDisable() {
  82. this.unregisterEvent(), super.onDisable();
  83. }
  84. _moveOffsetValue(t) {
  85. var e = cc.v2(0, 0),
  86. i = this.content.getComponent(cc.Layout);
  87. return (e.x = t * (this.node.width + i.spacingX)), e;
  88. }
  89. registerEvent() {
  90. this.node.on(cc.Node.EventType.TOUCH_START, this._onTouchBegan, this, !0),
  91. this.node.on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this, !0),
  92. this.node.on(cc.Node.EventType.TOUCH_END, this._onTouchEnded, this, !0),
  93. this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._onTouchCancelled, this, !0),
  94. this.node.on("scroll-ended-with-threshold", this.dispatchPageTurningEvent, this),
  95. this.node.on("scrolling", this.onScrolling, this);
  96. }
  97. unregisterEvent() {
  98. this.node.off(cc.Node.EventType.TOUCH_START, this._onTouchBegan, this, !0),
  99. this.node.off(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this, !0),
  100. this.node.off(cc.Node.EventType.TOUCH_END, this._onTouchEnded, this, !0),
  101. this.node.off(cc.Node.EventType.TOUCH_CANCEL, this._onTouchCancelled, this, !0),
  102. this.node.off("scroll-ended-with-threshold", this.dispatchPageTurningEvent, this),
  103. this.node.off("scrolling", this.onScrolling, this);
  104. }
  105. getDragDirection(t) {
  106. return 0 === t.x ? 0 : 0 < t.x ? 1 : -1;
  107. }
  108. dispatchPageTurningEvent() {
  109. this._lastPageIdx !== this._curPageIdx &&
  110. ((this._lastPageIdx = this._curPageIdx), this.node.emit("page-turning", this));
  111. }
  112. isScrollable(t, e, i) {
  113. return Math.abs(t.x) >= this.node.width * this.scrollThreshold;
  114. }
  115. isQuicklyScrollable(t) {
  116. return Math.abs(t.x) > this.autoPageTurningThreshold;
  117. }
  118. autoScrollToPage() {
  119. var t = this._startBounceBackIfNeeded(),
  120. e = this._touchBeganPosition.sub(this._touchEndPosition);
  121. if (t) {
  122. var i = this.getDragDirection(e);
  123. 0 !== i && (this._curPageIdx = 0 < i ? this.pages.length - 1 : 0);
  124. } else {
  125. var s = this._curPageIdx,
  126. t = s + this.getDragDirection(e),
  127. i = this.pageTurningSpeed * Math.abs(s - t);
  128. if (t < this.pages.length) {
  129. if (this.isScrollable(e, s, t)) return void this.scrollToPage(t, i);
  130. e = this._calculateTouchMoveVelocity();
  131. if (this.isQuicklyScrollable(e)) return void this.scrollToPage(t, i);
  132. }
  133. this.scrollToPage(s, i);
  134. }
  135. }
  136. _handleReleaseLogic(t) {
  137. this.autoScrollToPage(),
  138. this._scrolling && ((this._scrolling = !1), this._autoScrolling || this._dispatchEvent("scroll-ended"));
  139. }
  140. _onTouchBegan(t, e) {
  141. (this._touchBeganPosition = t.touch.getLocation()),
  142. super._onTouchBegan(t, e),
  143. this.node.emit("startScroll", this);
  144. }
  145. _onTouchMoved(t, e) {
  146. super._onTouchMoved(t, e);
  147. }
  148. _onTouchEnded(t, e) {
  149. (this._touchEndPosition = t.touch.getLocation()), super._onTouchEnded(t, e);
  150. }
  151. _onTouchCancelled(t, e) {
  152. (this._touchEndPosition = t.touch.getLocation()), super._onTouchCancelled(t, e);
  153. }
  154. emitItems(e, i, s, o, a, n) {
  155. this.items.map(function(t) {
  156. t.emit(e, i, s, o, a, n);
  157. });
  158. }
  159. onScrolling() {
  160. var n = this;
  161. this.items.map(function(t) {
  162. var e, i, s, o, a;
  163. t.isValid &&
  164. ((a = t.getComponent(n.itemRendererClass)),
  165. (e = n.node.convertToNodeSpaceAR(t.convertToWorldSpaceAR(t.position))),
  166. (o = n.content.getComponent(cc.Layout)),
  167. (s = (a.data.id - 1) * (n.node.width + o.spacingX) - n.content.width / 2 + n.node.width / 2),
  168. (i = 1),
  169. (a = 255),
  170. (s = Math.abs(e.x - s)),
  171. (o = n.node.width + o.spacingX),
  172. (a =
  173. s <= n.scaleCenterDist ?
  174. ((i = 1), 255) :
  175. o <= s ?
  176. ((i = n.scaleMin), n.alphaMin) :
  177. ((i = n.scaleMin + ((1 - n.scaleMin) / o) * (o - s)),
  178. n.alphaMin + ((255 - n.alphaMin) / o) * (o - s))),
  179. (t.scale = i),
  180. (t.opacity = a));
  181. });
  182. }
  183. };
  184. e([a(cc.Node)], t.prototype, "itemRender", void 0), (t = e([o], t)), (i.default = t);