我已经为这个问题搜索了很多答案,但似乎没有一个完全符合我的要求。很多教程向我展示了如何在代码中添加线条和多边形,但不是手绘。
我在地图上有一个带有 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;
}
}
画完之后,我想把这个画变成一个表格(区域),然后放到地图上。如果你对我有一些迹象?
谢谢你,对不起我的英语不好。