如何使用drawRect绘制现有的视图?

我正在做一个GPS跟踪应用程序。 每次它接收到经度/纬度时,都会将其转换为(x,y)坐标,并调用drawRect在两个(x,y)对之间绘制一条直线。

然而,drawRect方法在绘制新东西之前只清除所有的旧内容。 如何让drawRect在现有的canvas上绘制新的东西? 提前致谢

这里是我的viewController.h

 #import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> @class routetrack; @interface simpleDrawViewController : UIViewController <CLLocationManagerDelegate> { CLLocationManager *locationManager; IBOutlet routetrack *routeroute; float latOld; float longOld; float latNew; float longNew; } - (IBAction) gpsValueChanged; @property (nonatomic,retain) CLLocationManager *locationManager; @property (retain,nonatomic) routetrack *routeroute; 

ViewController.m

 #import "simpleDrawViewController.h" #import "routetrack.h" @implementation simpleDrawViewController @synthesize locationManager,btn,routeroute; - (IBAction) gpsValueChanged{ [routeroute setLocationX:longNew setLocationY:latNew oldLocation:longOld oldLocationY:latOld ]; [routeroute setNeedsDisplay]; } - (void)viewDidLoad { [super viewDidLoad]; if (nil == locationManager) locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBest; [locationManager startUpdatingLocation]; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // eg self.myOutlet = nil; } - (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { if (oldLocation) { latOld=oldLocation.coordinate.latitude; longOld=oldLocation.coordinate.longitude; latNew=newLocation.coordinate.latitude; longNew=newLocation.coordinate.longitude; } else { latOld=53.3834; longOld=-6.5876; latNew=53.3835; longNew=-6.5877; } [self gpsValueChanged]; } - (void) locationManager:(CLLocationManager *)manager didFailwithError: (NSError *)error { if([error code] == kCLErrorDenied) [locationManager stopUpdatingLocation]; NSLog(@"location manger failed"); } - (void)dealloc { [locationManager release]; [super dealloc]; } @end 

我的绘画类,UIView的一个子类

 #import "routetrack.h" @implementation routetrack - (void) setLocationX:(float) loX setLocationY:(float) loY oldLocation:(float) oldX oldLocationY:(float) oldY { self->locationX = loX; self->locationY = loY; self->oldLoX = oldX; self->oldLoY = oldY; scaleX= 36365.484375; scaleY= 99988.593750; maxLat= 53.3834; maxLog= -6.5976; drawX = locationX; drawY = locationY; tempX=(maxLog - drawX) * scaleX; tempY=(maxLat - drawY) * scaleY; lastX = (int) tempX; lastY = (int) tempY; drawX = oldLoX; drawY = oldLoY; tempX=(maxLog - drawX) * scaleX; tempY=(maxLat - drawY) * scaleY; startX = (int) tempX; startY = (int) tempY; } - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { } return self; } - (void)drawRect:(CGRect)rect { int firstPointX = self->startX; int firstPointY = self->startY; int lastPointX = self->lastX; int lastPointY = self->lastY; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0); CGContextSetLineWidth(context, 2.0); CGContextMoveToPoint(context, firstPointX, firstPointY); CGContextAddLineToPoint(context, lastPointX , lastPointY); CGContextStrokePath(context); } 

UIView没有canvas模型。 如果你想保持一个canvas,你应该创build一个CGLayerCGBitmapContext并绘制到该canvas上。 然后在你的视图中绘制。

我会为CGLayerRef创build一个ivar,然后drawRect:看起来像这样(未经testing):

 - (void)drawRect:(CGRect)rect { if (self.cgLayer == NULL) { CGLayerRef layer = CGLayerCreateWithContext(UIGraphicsContextGetCurrent(), self.bounds, NULL); self.cgLayer = layer; CGLayerRelease(layer); } ... various Core Graphics calls with self.cgLayer as the context ... CGContextDrawLayerInRect(UIGraphicsContextGetCurrent(), self.bounds, self.cgLayer); } 

每当你想清除你的canvas,只需self.cgLayer = NULL

setCgLayer:会是这样的:

 - (void)setCgLayer:(CGLayerRef)aLayer { CGLayerRetain(aLayer); CGLayerRelease(cgLayer_); cgLayer_ = aLayer; } 

“旧内容”到底意味着什么? 如果你想从你的GPS数据中划出一条线,你必须在drawRect的实现中每次绘制所有的点。