win_horizontal_slide.lua 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. --[[
  2. @module win_horizontal_slide
  3. @summary 横向滑动页面演示模块
  4. @version 1.0.0
  5. @date 2025.11.28
  6. @author 江访
  7. @usage
  8. 本文件为横向滑动页面演示模块,核心业务逻辑为:
  9. 1、创建窗口容器并设置白色背景;
  10. 2、启用横向滑动功能;
  11. 3、创建两页内容并水平排列;
  12. 4、实现横向滑动切换页面效果;
  13. 5、启动UI渲染循环持续刷新显示;
  14. 本文件没有对外接口;
  15. ]]
  16. local function ui_main()
  17. -- 显示触摸初始化
  18. hw_font_drv.init()
  19. -- 设置主题
  20. ui.sw_init({ theme = "light" })
  21. -- 创建窗口容器
  22. local win = ui.window({ background_color = ui.COLOR_WHITE })
  23. -- 启用横向滚动,将两页内容并排布置
  24. local page_w, page_h = lcd.getSize()
  25. local totalW = page_w * 2
  26. -- 创建横向滑动窗口
  27. win:enable_scroll({
  28. direction = "horizontal",
  29. content_width = totalW,
  30. threshold = 8,
  31. page_width = page_w
  32. })
  33. -- 创建网格按钮函数
  34. local function makeGrid(offset_x, label_prefix)
  35. local cols, rows = 3, 3
  36. local bw, bh = 90, 80
  37. local mx, my = 20 + offset_x, 60
  38. local gapx, gapy = 10, 10
  39. local n = 1
  40. for r = 0, rows - 1 do
  41. for c = 0, cols - 1 do
  42. local x = mx + c * (bw + gapx)
  43. local y = my + r * (bh + gapy)
  44. local btn = ui.button({
  45. x = x, y = y,
  46. w = bw, h = bh,
  47. text = string.format("%s-%d", label_prefix, n)
  48. })
  49. win:add(btn)
  50. n = n + 1
  51. end
  52. end
  53. end
  54. -- 创建左页和右页内容
  55. makeGrid(0, "P1") -- 第一页
  56. makeGrid(page_w, "P2") -- 第二页
  57. -- 注册窗口到UI系统
  58. ui.add(win)
  59. end
  60. sys.taskInit(ui_main)