0

我已经为这个问题搜索了很多答案,但似乎没有一个完全符合我的要求。很多教程向我展示了如何在代码中添加线条和多边形,但不是手绘。

我在地图上有一个带有 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;
    }
}

画完之后,我想把这个画变成一个表格(区域),然后放到地图上。如果你对我有一些迹象?

谢谢你,对不起我的英语不好。

4

2 回答 2

0

如果有人和我有同样的问题,这是我的代码以及艾伦给我的链接: http: //www.apps168.net/post/1460.html

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    pointA = [touch locationInView:drawView];
    pathRef = CGPathCreateMutable();
    CGPathMoveToPoint(pathRef, NULL, pointA.x, pointA.y);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint currentLocation = [touch locationInView:drawView];
    CGPoint pastLocation = [touch previousLocationInView: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, 0.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(ctx);
    CGContextStrokePath(ctx);

    CGContextMoveToPoint(ctx, pastLocation.x, pastLocation.y);
    CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
    CGContextDrawPath(ctx, kCGPathFillStroke);
    drawView.image=UIGraphicsGetImageFromCurrentImageContext();

    CGPathAddLineToPoint(pathRef, NULL, currentLocation.x, currentLocation.y);
    UIGraphicsEndImageContext();

    //pointA = currentLocation;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    CGPathCloseSubpath(pathRef);

    NSUInteger count = [array count];
    for (NSUInteger i = 0; i < count; i=i+2) {
        NSString *lat = [array objectAtIndex: i];
        NSString *lgt = [array objectAtIndex: i+1];

        CLLocationCoordinate2D pointLoc;
        pointLoc.latitude = [lat doubleValue];
        pointLoc.longitude = [lgt doubleValue];

        locationConverToImage = [myMapView convertCoordinate:pointLoc toPointToView:drawView];
        if (CGPathContainsPoint(pathRef, NULL, locationConverToImage, NO)) {
            NSLog(@"point in path!");
            //sélection de ces points et cacher les autres.
        }
    }

    [UIImageView beginAnimations:nil context:nil];
    [UIImageView setAnimationDuration:0.5];
    [drawView setAlpha:0.0];
    [UIImageView commitAnimations];
    myMapView.zoomEnabled = YES;
    myMapView.scrollEnabled = YES;

}
于 2013-04-19T10:59:25.287 回答
0

画我的地图试试这个,这是绘制地图点项目

于 2014-11-14T09:48:11.417 回答