开发者

How do I draw a line so that it appears onscreen, pixel by pixel?

开发者 https://www.devze.com 2023-03-27 20:56 出处:网络
I\'m new to Objective-C, and I\'m trying to draw a line onscreen in such a way that it appears one pixel at a time.

I'm new to Objective-C, and I'm trying to draw a line onscreen in such a way that it appears one pixel at a time.

Using the following drawing code, I'm able to draw the line, but it all appears onscreen at once, rather than incrementally over time:

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);

fo开发者_如何转开发r (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++; 

How can I achieve this pixel-by-pixel appearance of a line?


I think you need to add a delay and a window refresh to your for-loop if you want to see it being drawn.


Extend your line pixel by pixel in a function and call it after 0.5 secs(say). Your array having line points must be global and u would also need a global counter:

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++;
}

Call:

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

Im not very experienced with graphics contexts and stuff so I dont know if you need to call

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

every time or just once.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号