Objective c 如何使用drawRect在现有视图中绘制?
我正在做一个GPS跟踪应用程序。每次接收到纬度/经度时,它都会将其转换为(x,y)坐标,并调用Objective c 如何使用drawRect在现有视图中绘制?,objective-c,ios,drawing,draw,drawrect,Objective C,Ios,Drawing,Draw,Drawrect,我正在做一个GPS跟踪应用程序。每次接收到纬度/经度时,它都会将其转换为(x,y)坐标,并调用drawRect在两个(x,y)对之间绘制一条线 然而,drawRect方法只是在绘制新内容之前清除所有旧内容。如何使drawRect在现有画布上绘制新事物?提前谢谢 这是我的viewController.h 我的绘图类,UIView的子类 你说的“旧内容”到底是什么意思?如果要从GPS数据绘制直线,则必须在每次实施drawRect时绘制所有点。UIView没有画布模型。如果要保留画布,应创建CGLay
drawRect
在两个(x,y)对之间绘制一条线
然而,drawRect方法只是在绘制新内容之前清除所有旧内容。如何使drawRect在现有画布上绘制新事物?提前谢谢
这是我的viewController.h
我的绘图类,UIView的子类
你说的“旧内容”到底是什么意思?如果要从GPS数据绘制直线,则必须在每次实施drawRect时绘制所有点。UIView没有画布模型。如果要保留画布,应创建
CGLayer
或CGBitmapContext
并在其上绘制。然后在你的视野中画出来
我会为CGLayerRef
创建一个ivar,然后drawRect:
看起来像这样(未测试):
只要想清除画布,只需self.cgLayer=NULL
setCgLayer:
应该是这样的:
- (void)setCgLayer:(CGLayerRef)aLayer {
CGLayerRetain(aLayer);
CGLayerRelease(cgLayer_);
cgLayer_ = aLayer;
}
第一次GPS获得lat/long,该功能使用该数据绘制一条线并显示它。但下一次GPS获得新数据时,旧的线就会消失,并画出一条新的线。那不是我想要的。我希望旧的线存在,并画出一条新的线。你每次都得画所有的线。例如,您需要将所有坐标存储在NSMutableArray中,并在im drawRect中绘制整个数组的内容。@Kin,请确保将注释作为注释而不是答案发布。主持人已删除您的非答案答案。CGLayerRef将是视图上的实例变量(ivar)。它应该有一个类似RouteTrackView的名称,而不是“routetrack”。永远不要使用箭头符号
self->locationX
,除非你真的知道自己在做什么(这几乎是不正确的)。这应该是self.locationX
,您应该包括@synthetic locationX
创建访问器。
#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.
// e.g. 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
#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);
}
- (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);
}
- (void)setCgLayer:(CGLayerRef)aLayer {
CGLayerRetain(aLayer);
CGLayerRelease(cgLayer_);
cgLayer_ = aLayer;
}