我想繪製一條線(在iphone上),這樣我就可以查看每個正在繪製的像素。如何使用對象繪製一個像素逐像素C
我目前使用定時器調用drawRect方法。
以下是我在哪裏使用繪製從數組的值的波的代碼:
static int i=0;
int x = 10;
int y = 0;
int x2;
int y2;
- (void)drawRect:(CGRect)rect
{
// Drawing code.
CGContextRef c = UIGraphicsGetCurrentContext();
CGFloat black[4] = {0.0f,0.0f,0.0f,1.0f};
CGContextSetStrokeColor(c, black);
CGContextBeginPath(c);
NSLog(@"fired...");
int ecg[358] = {36, 37, 37, 36, 34, 33, 33, .... ,36}; //Assume the data is there
CGContextMoveToPoint(c, x, y);
x2 = i;
y2 = ecg[i];
CGContextAddLineToPoint(c, x2, y2);
x = x2;
y = y2;
CGContextStrokePath(c);
[self setNeedsDisplay];
i++;
}
- (void) awakeFromNib
{
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(drawRect:) userInfo:nil repeats:YES];
}
我只能看到在一次一行被拉伸時的drawRect被調用。但我沒有看到整個圖像。我想不出一種不同的方法來看到屏幕上正在繪製的波浪。
此外,initWithFrame方法永遠不會被調用,所以不得不與awakeFromNib一起去聲明計時器,如果我錯了,請糾正我。
任何指向動畫線條圖的指針都會有所幫助。
謝謝
每次調用drawRect時,都會清除上下文。所以你必須重新繪製一切到你想要的點。 – DarkDust 2011-04-01 14:03:31
@DarkDust:是的,我明白了,你能爲此提出一種不同的方法嗎? – R3D3vil 2011-04-01 14:22:51