我如何画一条线,使它显示在屏幕上,逐像素



我是Objective-C的新手,我正在尝试在屏幕上画一条线,使其每次显示一个像素。

使用下面的绘图代码,我可以画出这条线,但它会一次出现在屏幕上,而不是随着时间的推移而增加:

CGContextRef c = UIGraphicsGetCurrentContext();
CGFloat colour[4] = {1.0f,0.0f,0.0f,1.0f};
CGContextSetStrokeColor(c, colour);
CGContextBeginPath(c);
NSLog(@"fired...");
int line[10] = {136, 137, 138, 139,140,145,180,155}; 
CGContextMoveToPoint(c, x,0);
for (i = 0; i <10; i++) {
    x = line[i];
   // y = line[j];
    CGContextAddLineToPoint(c, x,0);
    x = x;
    //y = y;
}
CGContextStrokePath(c); 
x=x;
[self setNeedsDisplay];
//i++; 

如何实现逐像素的线条外观?

我认为你需要添加一个延迟和窗口刷新到你的for循环,如果你想看到它被绘制

在函数中逐像素扩展行,并在0.5秒后调用它(例如)。包含行点的数组必须是全局的,并且u还需要一个全局计数器:

int line[10] = {136,137,138,139,140,145,180,155};int i = 0;

- (void)extendLine {
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGFloat colour[4] = {1.0f,0.0f,0.0f,1.0f};
    CGContextSetStrokeColor(c, colour);
    CGContextBeginPath(c);
    NSLog(@"fired...");
    CGContextMoveToPoint(c, x,0);
    x = line[i];
    // y = line[j];
    CGContextAddLineToPoint(c, x,0);
    x = x;
    //y = y;
    CGContextStrokePath(c); 
    x=x;
    [self setNeedsDisplay];
    i++;
}

调用:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(extendLine) userInfo:nil repeats:YES];

我不是很有经验的图形上下文和东西,所以我不知道你是否需要调用

CGContextSetStrokeColor(c, colour);
CGContextBeginPath(c);

最新更新