怎么实现点击slider之后滑动条不发生变法,当拖拉slider的时候才发生变化

我想实现当点击slider的时候只聚焦这个控件,而slider的进度不会发生变化,只有当拖拉slider的时候进度才会改变。
看了挺多资料都是通过lv_obj_add_flag(slider, LV_OBJ_FLAG_ADV_HITTEST)去实现仅旋钮控制模式,此模式下slider宽度较小触控就比较困难,希望大佬能帮忙解答一下怎么实现点击不改变进度,谢谢。

static void slider_cb(lv_event_t *e) // 滑动回调
{
static bool touched = false;
static lv_point_t click_point1, click_point2;
int movex, movey;

lv_obj_t *bak = (lv_obj_t *)e->target;
lv_obj_t *slider = lv_obj_get_child(bak, 0);
lv_obj_t *num_lable = lv_obj_get_child(bak, 1);

lv_obj_refr_size(bak);

if (e->code == LV_EVENT_SIZE_CHANGED && lv_obj_is_valid(slider))
{

	int value = atoi(lv_label_get_text(num_lable));

	if (lv_obj_has_flag(bak, direction_x))
	{
		lv_obj_set_x(slider, -lv_disp_get_hor_res(lv_disp_get_default()) + (int)((lv_obj_get_width(bak) * (((float)value) / 100))));
	}

	if (lv_obj_has_flag(bak, direction_y))
	{
		lv_obj_set_y(slider, (int)(lv_obj_get_height(bak) * (((float)(100 - value)) / 100)));
	}
}

if (e->code == LV_EVENT_PRESSING)
{

	if (touched == false)
	{
		lv_indev_get_point(lv_indev_get_act(), &click_point1);
		touched = true;
		return;
	}
	else
	{
		lv_indev_get_point(lv_indev_get_act(), &click_point2);
	}

	movex = click_point2.x - click_point1.x; // 记录x滑动了多少
	movey = click_point2.y - click_point1.y; // 记录y滑动了多少
	click_point1.x = click_point2.x;
	click_point1.y = click_point2.y;

	if (lv_obj_has_flag(bak, direction_x) && movex != 0)
	{
		if (movex + lv_obj_get_x(slider) < -lv_disp_get_hor_res(lv_disp_get_default()) ||
			movex + lv_obj_get_x(slider) > -lv_disp_get_hor_res(lv_disp_get_default()) + lv_obj_get_width(bak))
			return;

		lv_obj_set_x(slider, movex + lv_obj_get_x(slider));
		lv_obj_refr_pos(slider);
		lv_label_set_text_fmt(num_lable, "%d%%", (int)((lv_disp_get_hor_res(lv_disp_get_default()) + lv_obj_get_x(slider)) / ((float)(lv_obj_get_width(bak)) / 100)));
		lv_event_send(bak, LV_EVENT_VALUE_CHANGED, 0);
	}

	if (lv_obj_has_flag(bak, direction_y) && movey != 0)
	{
		if (movey + lv_obj_get_y(slider) > lv_obj_get_height(bak) ||
			movey + lv_obj_get_y(slider) < 0)
			return;

		lv_obj_set_y(lv_obj_get_child(bak, 0), movey + lv_obj_get_y(slider));
		lv_obj_refr_pos(slider);
		lv_label_set_text_fmt(num_lable, "%d%%", (int)((lv_obj_get_height(bak) - lv_obj_get_y(slider)) / ((float)(lv_obj_get_height(bak)) / 100)));
		lv_event_send(bak, LV_EVENT_VALUE_CHANGED, 0);
	}
}

if (e->code == LV_EVENT_RELEASED)
{
	touched = false;
	return;
}

}

谢谢你的回复,不过使用你的函数后点击slider的时候,进度还是会跳到点击的位置

自定义一个,创建两个tileview,再用这个事件,因为我就是这么干的

关于这个问题已经有视频教程了:https://www.bilibili.com/video/BV1H1421i79r

1 个赞

早上刚看完,嘿嘿嘿 :+1:

1 个赞