当前位置:   article > 正文

ArkTs编写虚拟摇杆

ArkTs编写虚拟摇杆
  1. @Entry
  2. @Component
  3. struct All{
  4. @State offsetX: number = 0
  5. @State offsetY: number = 0
  6. @State positionX: number = 0
  7. @State positionY: number = 0
  8. @State offsetX1: number = 0
  9. @State offsetY1: number = 0
  10. @State positionX1: number = 0
  11. @State positionY1: number = 0
  12. private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.All })
  13. private panOption1: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Horizontal })
  14. build(){
  15. Stack({}) {
  16. Row() {
  17. Stack() {
  18. Column() {
  19. Arc1({ radius: 60, x: 80, y: 600, globalAlpha:0.3 })
  20. }
  21. Column() {
  22. Arc1({ radius: 25, x: 80, y: 600, globalAlpha:0.7 })
  23. }
  24. .translate({ x: this.offsetX, y: this.offsetY, z: 0 }) // 以组件左上角为坐标原点进行移动
  25. // 左右拖动触发该手势事件
  26. // 添加 PanGesture 手势,实现左右拖动触发事件
  27. .gesture(
  28. PanGesture(this.panOption)
  29. .onActionStart((event?: GestureEvent) => {
  30. console.info('Pan start')
  31. })
  32. .onActionUpdate((event?: GestureEvent) => {
  33. if (event) {
  34. // 计算拖动的相对坐标
  35. let x = event.offsetX;
  36. let y = event.offsetY;
  37. // 计算相对圆心的距离
  38. let distance = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
  39. // 如果超过半径,将位置限制在圆的边缘
  40. if (distance > 60) {
  41. let angle = Math.atan2(y, x);
  42. x = Math.cos(angle) * 60;
  43. y = Math.sin(angle) * 60;
  44. }
  45. // 更新偏移量
  46. this.offsetX = this.positionX + x;
  47. this.offsetY = this.positionY + y;
  48. }
  49. })
  50. .onActionEnd(() => {
  51. this.offsetX = 0;
  52. this.offsetY = 0;
  53. // this.positionX = this.offsetX;
  54. // this.positionY = this.offsetY;
  55. console.info('Pan end');
  56. })
  57. )
  58. }
  59. .width('50%')
  60. .height('100%')
  61. // Column() {
  62. Stack() {
  63. square()
  64. // }
  65. // Column() {
  66. // Arc1({ radius: 30, x: 276, y: 615 })
  67. Arc1({ radius: 30, x: 85, y: 615, globalAlpha:0.7})
  68. // }
  69. .translate({ x: this.offsetX1, y: this.offsetY1, z: 0 }) // 以组件左上角为坐标原点进行移动
  70. // 左右拖动触发该手势事件
  71. // 添加 PanGesture 手势,实现左右拖动触发事件
  72. .gesture(
  73. PanGesture(this.panOption1)
  74. .onActionStart((event?: GestureEvent) => {
  75. console.info('Pan start')
  76. })
  77. .onActionUpdate((event?: GestureEvent) => {
  78. if (event) {
  79. // 计算拖动的相对坐标
  80. let x1 = event.offsetX;
  81. let y1 = event.offsetY;
  82. // 限制水平拖动范围在长方形内
  83. if (x1 > 60) {
  84. x1 = 60;
  85. } else if (x1 < -60) {
  86. x1 = -60;
  87. }
  88. // 更新偏移量
  89. this.offsetX1 = this.positionX1 + x1;
  90. this.offsetY1 = this.positionY1 + y1;
  91. }
  92. })
  93. .onActionEnd(() => {
  94. this.offsetX1 = 0;
  95. this.offsetY1 = 0;
  96. // this.positionX = this.offsetX;
  97. // this.positionY = this.offsetY;
  98. console.info('Pan end');
  99. })
  100. )
  101. }
  102. .width('50%')
  103. .height('100%')
  104. }
  105. }
  106. .width('100%')
  107. .height('100%')
  108. }
  109. }
  110. @Component
  111. struct Arc1{
  112. private settings: RenderingContextSettings = new RenderingContextSettings(true)
  113. private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  114. private radius: number
  115. private x: number
  116. private y:number
  117. private globalAlpha: number
  118. build() {
  119. Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
  120. Canvas(this.context)
  121. .width('100%')
  122. .height('100%')
  123. // .backgroundColor('#ffff00')
  124. .onReady(() => {
  125. this.context.beginPath()
  126. this.context.arc(this.x, this.y, this.radius, 0, 6.28)
  127. this.context.globalAlpha = this.globalAlpha
  128. this.context.fillStyle = '#ffa6a6ea'
  129. this.context.fill()
  130. })
  131. }
  132. .width('100%')
  133. .height('100%')
  134. }
  135. }
  136. @Component
  137. struct square{
  138. private settings: RenderingContextSettings = new RenderingContextSettings(true)
  139. private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  140. build(){
  141. Column() {
  142. Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
  143. Canvas(this.context)
  144. .width('100%')
  145. .height('100%')
  146. // .backgroundColor('#ffff00')
  147. .onReady(() => {
  148. let ctx = new Path2D();
  149. let x = 0
  150. let y = 600
  151. let arcRadius = 15
  152. let height = 30
  153. let width = 170
  154. // 左上角
  155. ctx.moveTo(x + arcRadius, y);
  156. ctx.arcTo(x, y, x, y + arcRadius, arcRadius);
  157. // 左下角
  158. ctx.lineTo(x, y + height - arcRadius);
  159. ctx.arcTo(x, y + height, x + arcRadius, y + height, arcRadius);
  160. // 右下角
  161. ctx.lineTo(x + width - arcRadius, y + height);
  162. ctx.arcTo(x + width, y + height, x + width, y + height - arcRadius, arcRadius);
  163. // 右上角
  164. ctx.lineTo(x + width, y + arcRadius);
  165. ctx.arcTo(x + width, y, x + width - arcRadius, y, arcRadius);
  166. ctx.closePath();
  167. this.context.fillStyle = '#0097D4';
  168. // 填充
  169. this.context.fill(ctx);
  170. })
  171. }
  172. }
  173. }
  174. }

效果图

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/231875
推荐阅读
相关标签
  

闽ICP备14008679号