我已經搜索了很多關於這個問題的答案,但是他們沒有一個看起來像我想要的那樣。很多教程向我展示瞭如何在代碼中添加線條和多邊形,但不適用於徒手畫。通過MKMap在UIImageView上繪圖查看
我在地圖上得到了一個帶有UIImageView的MKMapView。而且我可以在我的UIImageView上畫一點,但之後,MKMapView被拖拽並且繪圖不會繼續。
我想知道我做錯了什麼?
這裏是我的觸摸事件的代碼:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
pointA = [touch locationInView:drawView];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentLocation = [touch locationInView:drawView];
UIGraphicsBeginImageContext(drawView.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[drawView.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)];
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, 5.0);
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, pointA.x, pointA.y);
CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
CGContextStrokePath(ctx);
drawView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
pointA = currentLocation;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentLocation = [touch locationInView:drawView];
UIGraphicsBeginImageContext(drawView.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[drawView.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)];
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, 5.0);
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, pointA.x, pointA.y);
CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
CGContextStrokePath(ctx);
drawView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
pointA = currentLocation;
}
而且按鈕的代碼誰顯示的UIImageView:
編輯:我做了這個用於回採與地圖的互動:
- (IBAction)modeDraw:(id)sender {
if (drawView.alpha == 0.0) {
drawView.image=nil; //empty for a new draw
drawView.backgroundColor = [UIColor whiteColor];
[UIImageView beginAnimations:nil context:nil];
[UIImageView setAnimationDuration:0.5];
[drawView setAlpha:0.4];
[UIImageView commitAnimations];
myMapView.zoomEnabled = NO;
myMapView.scrollEnabled = NO;
} else {
[UIImageView beginAnimations:nil context:nil];
[UIImageView setAnimationDuration:0.5];
[drawView setAlpha:0.0];
[UIImageView commitAnimations];
myMapView.zoomEnabled = YES;
myMapView.scrollEnabled = YES;
}
}
做我的畫,後,我想這場平局變成一種形式(地區),這將被放在地圖上。如果你對我有一些跡象?
謝謝你,對不起我的英語不好。
保存此圖作爲一幅圖像並將此圖像用作地圖視圖上的註釋。如果是工作:) – Mangesh