BlindsTransitionNodes.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import {_decorator, Component, Node, UITransform, tween, Vec3, SpriteFrame, Sprite} from 'cc';
  2. const {ccclass, property} = _decorator;
  3. @ccclass('BlindsTransitionNodes')
  4. export class BlindsTransitionNodes extends Component {
  5. @property
  6. blindsCount: number = 8;
  7. @property
  8. duration: number = 1.0;
  9. @property
  10. isHorizontal: boolean = true;
  11. @property(SpriteFrame)
  12. private icon: SpriteFrame = null
  13. private blinds: Node[] = [];
  14. onLoad() {
  15. this.createBlinds();
  16. }
  17. createBlinds() {
  18. // 清除现有的百叶窗
  19. this.blinds.forEach(blind => blind.destroy());
  20. this.blinds = [];
  21. const uiTransform = this.node.getComponent(UITransform);
  22. const width = uiTransform.width;
  23. const height = uiTransform.height;
  24. for (let i = 0; i < this.blindsCount; i++) {
  25. const blind = new Node('Blind_' + i);
  26. const blindTransform = blind.addComponent(UITransform);
  27. const sp: Sprite = blind.addComponent(Sprite)
  28. sp.spriteFrame = this.icon
  29. if (this.isHorizontal) {
  30. blindTransform.width = width;
  31. blindTransform.height = height / this.blindsCount;
  32. blind.setPosition(0, (i - this.blindsCount / 2 + 0.5) * blindTransform.height);
  33. } else {
  34. blindTransform.width = width / this.blindsCount;
  35. blindTransform.height = height;
  36. blind.setPosition((i - this.blindsCount / 2 + 0.5) * blindTransform.width, 0);
  37. }
  38. // 设置背景颜色或纹理
  39. // 这里可以添加Sprite组件并设置纹理
  40. this.node.addChild(blind);
  41. this.blinds.push(blind);
  42. // 初始状态设置为隐藏
  43. // if (this.isHorizontal) {
  44. // blind.setScale(1, 0);
  45. // } else {
  46. // blind.setScale(0, 1);
  47. // }
  48. this.setHide()
  49. }
  50. }
  51. /**
  52. * 隐藏百叶窗
  53. * @param f
  54. */
  55. setHide(f: boolean = true) {
  56. this.blinds.forEach((blind) => {
  57. if (f) {
  58. if (this.isHorizontal) {
  59. blind.setScale(1, 0);
  60. } else {
  61. blind.setScale(0, 1);
  62. }
  63. } else {
  64. if (this.isHorizontal) {
  65. blind.setScale(1, 1);
  66. } else {
  67. blind.setScale(1, 1);
  68. }
  69. }
  70. })
  71. }
  72. /**
  73. * 播放百叶窗打开动画
  74. * @param callback
  75. */
  76. play(callback: Function = null) {
  77. this.blinds.forEach((blind, index) => {
  78. const delay = index * (this.duration / this.blindsCount) * 0.5;
  79. tween(blind)
  80. .delay(delay)
  81. .to(this.duration * 0.5,
  82. {scale: new Vec3(1, 1, 1)},
  83. {easing: 'sineOut'}
  84. )
  85. .start();
  86. });
  87. this.scheduleOnce(function () {
  88. if (null != callback) {
  89. callback()
  90. }
  91. }, this.duration);
  92. }
  93. /**
  94. * 一张一张的单独播放百叶窗动画,并回调。
  95. * @param callback
  96. * @param index
  97. */
  98. playByStep(callback: Function, index: number = 0) {
  99. if (index < this.blinds.length) {
  100. const delay = index * (this.duration / this.blindsCount) * 0.5;
  101. tween(this.blinds[index])
  102. .delay(delay)
  103. .to(this.duration * 0.5,
  104. {scale: new Vec3(1, 1, 1)},
  105. {easing: 'sineOut'}
  106. )
  107. .call(() => {
  108. // console.log("*********");
  109. })
  110. .start();
  111. this.playByStep(callback, index + 1)
  112. } else {
  113. this.scheduleOnce(function () {
  114. callback()
  115. }, this.duration);
  116. }
  117. }
  118. /**
  119. * 播放百叶窗关闭动画
  120. * @param callback
  121. */
  122. playReverse(callback: Function = null) {
  123. this.blinds.forEach((blind, index) => {
  124. const delay = index * (this.duration / this.blindsCount) * 0.5;
  125. tween(blind)
  126. .delay(delay)
  127. .to(this.duration * 0.5,
  128. {
  129. scale: this.isHorizontal ?
  130. new Vec3(1, 0, 1) :
  131. new Vec3(0, 1, 1)
  132. },
  133. {easing: 'sineIn'}
  134. )
  135. .start();
  136. });
  137. this.scheduleOnce(function () {
  138. if (null != callback) {
  139. callback()
  140. }
  141. }, this.duration);
  142. }
  143. // 重新创建百叶窗
  144. refreshBlinds() {
  145. this.createBlinds();
  146. }
  147. }