FloatingComp.ets 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { UserInfo, userInfo } from 'basic';
  2. @Component
  3. export struct FloatingComp {
  4. @Prop src: ResourceStr = $r('[basic].media.app_icon')
  5. initPosition: Position | Edges | LocalizedEdges = { right: '50%', bottom: 20 }
  6. imageWidth: number = 48
  7. @State private cumulativeX: number = 0;
  8. @State private cumulativeY: number = 0;
  9. @StorageProp(UserInfo.KEY) private userInfo: UserInfo = userInfo
  10. private startX: number = 0;
  11. private startY: number = 0;
  12. click = () => {
  13. }
  14. build() {
  15. if (this.userInfo.checkLogin()) {
  16. Image(this.src)
  17. .width(this.imageWidth)
  18. .aspectRatio(1)
  19. .borderRadius(9999)
  20. .gesture(
  21. PanGesture({ distance: 1 })
  22. .onActionStart(() => {
  23. this.startX = this.cumulativeX;
  24. this.startY = this.cumulativeY;
  25. })
  26. .onActionUpdate(event => {
  27. // 记录起始位置
  28. this.cumulativeX = this.startX + event.offsetX;
  29. this.cumulativeY = this.startY + event.offsetY;
  30. })
  31. )
  32. .translate({ x: this.cumulativeX, y: this.cumulativeY })
  33. .position(this.initPosition)
  34. .markAnchor({ x: -this.imageWidth / 2 })
  35. .onClick(this.click)
  36. }
  37. }
  38. }