awtk控件实现上下滚动效果

该代码示例展示了如何在AWTK框架下创建一个包含三个按钮的视图,并实现按钮的上下滚动效果。通过`on_button_click`和`on_button1_click`函数处理点击事件,动态改变按钮的位置来模拟滚动。
摘要由CSDN通过智能技术生成

awtk控件实现上下滚动效果

#include "awtk.h"
#include "../common/navigator.h"

static widget_t *second_view = NULL;
static widget_t *btn1 = NULL;
static widget_t *btn2 = NULL;
static widget_t *btn3 = NULL;
static widget_t *win_main = NULL;
//创建控件对象
void initBtn()
{
  if (!second_view)
  {
    second_view = view_create(win_main, 20, 20, 100, 200);
    btn1 = button_create(second_view, 0, 0, second_view->w, 60);
    btn2 = button_create(second_view, 0, 60, second_view->w, 60);
    btn3 = button_create(second_view, 0, 120, second_view->w, 60);
    widget_set_text_utf8(btn1, "按钮1");
    widget_set_text_utf8(btn2, "按钮2");
    widget_set_text_utf8(btn3, "按钮3");
  }
}
static ret_t on_button_click(void *ctx, event_t *e)
{
  for (int i = 0; i < 3; i++)
  {
    widget_t *label = widget_get_child(second_view, i);
    int y = label->y - 60; //减去控件的高度
    if (y < 0)
      y = 120;
    widget_move(label, 0, y);
  }
  return RET_OK;
}
static ret_t on_button1_click(void *ctx, event_t *e)
{
  for (int i = 0; i < 3; i++)
  {
    widget_t *label = widget_get_child(second_view, i);
    int y = label->y + 60;
    if (y > 120)
      y = 0;
    widget_move(label, 0, y);
  }
  return RET_OK;
}
/**
 * 初始化窗口的子控件
 */
static ret_t visit_init_child(void *ctx, const void *iter)
{
  widget_t *win = WIDGET(ctx);
  widget_t *widget = WIDGET(iter);
  const char *name = widget->name;

  // 初始化指定名称的控件(设置属性或注册事件),请保证控件名称在窗口上唯一
  if (name != NULL && *name != '\0')
  {
    if (tk_str_eq(name, "button"))
    {
      widget_on(widget, EVT_CLICK, on_button_click, win);
    }
    else if (tk_str_eq(name, "button1"))
    {
      widget_on(widget, EVT_CLICK, on_button1_click, win);
    }
  }

  return RET_OK;
}

/**
 * 初始化窗口
 */
ret_t home_page_init(widget_t *win, void *ctx)
{
  (void)ctx;
  return_value_if_fail(win != NULL, RET_BAD_PARAMS);
  win_main = win;
  initBtn();
  widget_foreach(win, visit_init_child, win);

  return RET_OK;
}

注意:控件根据具体情况定义

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值