我想为我的鹅卵石写一个模拟表面,所以我决定开始制作一个秒针动画,以确保我明白我在做什么。我做到了这一点,奇怪的是它只在0到22秒之间工作。一旦它超过23秒,它就会崩溃,直到下一分钟开始才会再次正确启动。
我有一个占据整个屏幕的图层,这是更新图层的回调函数:
void face_layer_update_callback(Layer *me, GContext* ctx) {
//set colour
graphics_context_set_stroke_color(ctx, GColorBlack);
time_t now = time(NULL);
struct tm *t = localtime(&now);
int seconds = t->tm_sec;
int hand_length = 50;
GPoint p0 = GPoint(72, 84);
int x, y, z;
z = seconds * 6;
x = hand_length * sin( (PI/180) * z);
y = hand_length * cos( (PI/180) * z);
GPoint p1 = GPoint(72 + x, 84 - y);
graphics_draw_line(ctx, p0, p1);
}
我添加了日志记录来输出计算出的值z,x和y,它们正是我所期望的。任何人都可以看到可能出现的问题吗?
(顺便说一下,这是在原始的鹅卵石上,而不是卵石时间。)
更新:根据Thomas的评论,我添加了更多日志记录,现在代码如下:
void face_layer_update_callback(Layer *me, GContext* ctx) {
APP_LOG(APP_LOG_LEVEL_DEBUG, "Start of callback routine");
//set colour
graphics_context_set_stroke_color(ctx, GColorBlack);
time_t now = time(NULL);
struct tm *t = localtime(&now);
int seconds = t->tm_sec;
APP_LOG(APP_LOG_LEVEL_DEBUG, "seconds: %d", seconds);
int hand_length = 50;
GPoint p0 = GPoint(72, 84);
int x, y;
int degrees;
degrees = seconds * 6;
float radians = (PI/180) * degrees;
APP_LOG(APP_LOG_LEVEL_DEBUG, "About to do trigonometry: degrees = %d", degrees);
x = hand_length * sin(radians);
APP_LOG(APP_LOG_LEVEL_DEBUG, "Done sin, about to do cos");
y = hand_length * cos(radians);
APP_LOG(APP_LOG_LEVEL_DEBUG, "x = %d", x);
APP_LOG(APP_LOG_LEVEL_DEBUG, "y = %d", y);
APP_LOG(APP_LOG_LEVEL_DEBUG, "End of hand: %d, %d", 72 + x, 84 - y);
GPoint p1 = GPoint(72 + x, 84 - y);
APP_LOG(APP_LOG_LEVEL_DEBUG, "About to draw hand");
graphics_draw_line(ctx, p0, p1);
日志记录显示:
[INFO ] D analogueface.c:11 Start of callback routine
[INFO ] D analogueface.c:20 seconds: 21
[INFO ] D analogueface.c:32 About to do trigonometry: degrees = 126
[INFO ] D analogueface.c:35 Done sin, about to do cos
[INFO ] D analogueface.c:38 x = 40
[INFO ] D analogueface.c:39 y = -29
[INFO ] D analogueface.c:40 End of hand: 112, 113
[INFO ] D analogueface.c:44 About to draw hand
[INFO ] D analogueface.c:11 Start of callback routine
[INFO ] D analogueface.c:20 seconds: 22
[INFO ] D analogueface.c:32 About to do trigonometry: degrees = 132
[INFO ] D analogueface.c:35 Done sin, about to do cos
[INFO ] D analogueface.c:38 x = 37
[INFO ] D analogueface.c:39 y = -33
[INFO ] D analogueface.c:40 End of hand: 109, 117
[INFO ] D analogueface.c:44 About to draw hand
[INFO ] D analogueface.c:11 Start of callback routine
[INFO ] D analogueface.c:20 seconds: 23
[INFO ] D analogueface.c:32 About to do trigonometry: degrees = 138
我尝试记录弧度值,但不会在日志中对其进行格式化。
(对于试图将评论内容放在不应该出现的评论中的道歉)。