這個任務是,在運行時在自定義地圖上繪製路徑,這些地圖在滾動視圖中使用,然後每當位置座標(lat,long)更新時,我都必須在運行時繪製路徑。這個問題在這裏試圖解決的是,我已經做了一個類'圖形'這是UIView的子類,其中我編寫了'drawrect:'方法的圖形。所以當我將圖形作爲滾動視圖的子視圖添加到圖像上時,線條會繪製,但我需要繼續繪製線條,就像它是路徑一樣。我需要在運行時繪製線條,需要不斷更新'CGContextStrokeLineSegments'方法的點(x,y)。代碼:核心圖形,如何在運行時畫線?
的ViewController:
- (void)loadView {
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
graph = [[graphics alloc] initWithFrame:fullScreenRect];
scrollView.contentSize=CGSizeMake(320,480);
UIImageView *tempImageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"fortuneCenter.png"]];
self.view=scrollView;
[scrollView addSubview:tempImageView2];
scrollView.userInteractionEnabled = YES;
scrollView.bounces = NO;
[scrollView addSubview:graph];
}
Graphics.m:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint point [2] = { CGPointMake(160, 100), CGPointMake(160,300)};
CGContextSetRGBStrokeColor(context, 255, 0, 255, 1);
CGContextStrokeLineSegments(context, point, 2);
}
因此,如何能在我運行時繪製線條。我現在只是模擬,所以即時通訊不使用實時數據(座標)。只需要使用虛擬數據(x,y的座標)進行模擬。讓我們說有一個按鈕,每當我按下它更新座標,所以路徑延伸。