win_vertical_slide.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. --[[
  2. @module win_vertical_slide
  3. @summary 纵向滑动页面演示模块
  4. @version 1.0.0
  5. @date 2025.12.9
  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 total_h = page_h * 2
  26. -- 创建纵向滑动窗口
  27. win:enable_scroll({
  28. direction = "vertical",
  29. content_height = total_h,
  30. threshold = 10,
  31. page_height = page_h
  32. })
  33. -- 创建按钮布局函数
  34. local function makebuttons(offset_y, label_prefix)
  35. -- 竖直等间距排列:1列4行,水平居中
  36. local cols, rows = 1, 4
  37. local bw, bh = 300, 70
  38. local mx = math.floor((page_w - bw) / 2) -- 居中
  39. local gap = math.floor((page_h - rows * bh) / (rows + 1))
  40. if gap < 20 then gap = 20 end
  41. local n = 1
  42. -- 添加页面标题
  43. local page_title = ui.label({
  44. x = mx, y = offset_y + gap - 40,
  45. text = "页面 " .. label_prefix,
  46. color = ui.COLOR_BLACK,
  47. size = 22
  48. })
  49. win:add(page_title)
  50. for r = 0, rows - 1 do
  51. local x = mx
  52. local y = offset_y + gap + r * (bh + gap)
  53. local btn = ui.button({
  54. x = x, y = y,
  55. w = bw, h = bh,
  56. text = string.format("%s-按钮%d", label_prefix, n),
  57. size = 18
  58. })
  59. win:add(btn)
  60. n = n + 1
  61. end
  62. end
  63. -- 创建总标题
  64. local title = ui.label({
  65. x = 320, y = 20,
  66. text = "纵向滑动页面演示",
  67. color = ui.COLOR_BLACK,
  68. size = 24
  69. })
  70. win:add(title)
  71. -- 添加上页和下页内容
  72. makebuttons(0, "一") -- 第一页
  73. makebuttons(page_h, "二") -- 第二页
  74. -- 添加提示标签
  75. local hint = ui.label({
  76. x = 320, y = 450,
  77. text = "上下滑动切换页面",
  78. color = ui.COLOR_GRAY,
  79. size = 16
  80. })
  81. win:add(hint)
  82. -- 注册窗口到UI系统
  83. ui.add(win)
  84. end
  85. sys.taskInit(ui_main)