2013-04-18 73 views
0

我已經搜索了很多關於這個問題的答案,但是他們沒有一個看起來像我想要的那樣。很多教程向我展示瞭如何在代碼中添加線條和多邊形,但不適用於徒手畫。通過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; 
    } 
} 

做我的畫,後,我想這場平局變成一種形式(地區),這將被放在地圖上。如果你對我有一些跡象?

謝謝你,對不起我的英語不好。

+0

保存此圖作爲一幅圖像並將此圖像用作地圖視圖上的註釋。如果是工作:) – Mangesh

回答

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; 

} 
相關問題