gpt4 book ai didi

在 Pebble 表盘应用程序上创建多个文本层

转载 作者:太空宇宙 更新时间:2023-11-04 04:34:09 31 4
gpt4 key购买 nike

我正在尝试使用自定义字体和位图图像创建一个简单的表盘。当我刚刚显示时间时,它工作正常。我只使用了 1 个 TextLayer。

static TextLayer *s_time_layer;

然后我决定在显示中添加日期和星期。所以我又创建了 2 个 TextLayers。

static TextLayer *s_day_layer,*s_date_layer;

创建这两层后,我成功地能够显示星期和日期。但是 watch 停止滴答。它只显示当前时间一次然后卡住。它在我再次启动后显示当前时间。

这是我更新时间的函数。

        static void update_time(struct tm *tick_time)
{
time_t temp = time(NULL);
tick_time = localtime(&temp);

static char buffer[6];
static char daybuffer[9];
static char datebuffer[] = "00-00-00";

strftime(buffer, sizeof(buffer), "%I:%M", tick_time); //using 12 hr format
text_layer_set_text(s_time_layer,buffer);

strftime(daybuffer, sizeof(daybuffer), "%A", tick_time);
text_layer_set_text(s_day_layer, daybuffer); //display current day

strftime(datebuffer, sizeof(datebuffer), "%d/%m/%y", tick_time);
text_layer_set_text(s_date_layer, datebuffer); //display current date

}

为什么会这样?我应该如何设置我的时钟?

最佳答案

您定义了 daybuffer[9],但最坏的情况是星期三,即 9 个字符长 + null 终止符。

这意味着 text_layer_set_text 将调用 UB什么时候打印你的字符串。

将缓冲区扩大到 10 个字符。

看看strftime手册页

在哪里可以找到

RETURN VALUE

Provided that the result string, including the terminating null byte, does not exceed max bytes, strftime() returns the number of bytes (excluding the terminating null byte) placed in the array s. If the length of the result string (including the terminating null byte) would exceed max bytes, then strftime() returns 0, and the contents of the array are undefined. Note that the return value 0 does not necessarily indicate an error. For example, in many locales %p yields an empty string. An empty format string will likewise yield an empty string.

关于在 Pebble 表盘应用程序上创建多个文本层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33015802/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com