【发布时间】:2012-03-31 19:40:15
【问题描述】:
我对 iOS 开发完全陌生,我心中有一个想法,但我不知道如何正确实现它。
我需要的是我的程序来绘制一条可由用户通过点击按钮控制的线。这有点像“蛇”游戏。我尝试了核心图形,但我猜这不是正确的方法。我做了以下:
- (void)viewDidLoad{
[NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(updateGame:) userInfo:nil repeats:YES];
}
-(void)updateGame:(id)sender{
double timeInterval= [self.lastDate timeIntervalSinceNow]*-1;
for (int n=0; n<playerNum; n++) {
CGPoint lastPoint=[[locationArray objectAtIndex:n]CGPointValue];
CGPoint updatedLoc= CGPointMake(lastPoint.x+100*timeInterval*sin([[directionArray objectAtIndex:n]doubleValue]), lastPoint.y+60*timeInterval*cos([[directionArray objectAtIndex:n]doubleValue]));
[locationArray replaceObjectAtIndex:n withObject:[NSValue valueWithCGPoint:updatedLoc]];
[self.drawingView drawToBufferFrom:lastPoint to:updatedLoc withColor:[colorArray objectAtIndex:n]];
}
self.lastDate=[NSDate date];
}
在 DrawingView.m 中
-(void)drawToBufferFrom:(CGPoint)lastLoc to:(CGPoint)currentLoc withColor:(UIColor *)color{
//[color setStroke];
CGContextSetStrokeColorWithColor(offScreenBuffer, color.CGColor);
//CGContextSetRGBStrokeColor(offScreenBuffer, 1, 0, 1, 1);
CGContextBeginPath(offScreenBuffer);
CGContextSetLineWidth(offScreenBuffer, 10);
CGContextSetLineCap(offScreenBuffer, kCGLineCapRound);
CGContextMoveToPoint(offScreenBuffer, lastLoc.x, lastLoc.y);
CGContextAddLineToPoint(offScreenBuffer, currentLoc.x, currentLoc.y);
CGContextDrawPath(offScreenBuffer, kCGPathStroke);
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
CGImageRef cgImage= CGBitmapContextCreateImage(offScreenBuffer);
UIImage* screenImage= [[UIImage alloc]initWithCGImage:cgImage];
CGImageRelease(cgImage);
[screenImage drawInRect:self.bounds];
}
用户点击屏幕两侧会改变方向。 但是我面临的是:设备本身的主要滞后,所以我认为需要一种更简单的方法来绘制这些线条而没有任何滞后。
【问题讨论】:
标签: ios cocoa-touch cocoa core-graphics