在MKMapView上绘制UIImageView [英] Draw on UIImageView over MKMapView

查看:103
本文介绍了在MKMapView上绘制UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了很多关于这个问题的答案,但它们似乎都没有完全符合我的要求。很多教程向我展示了如何在代码中添加线条和多边形,但没有使用徒手绘图。

I have searched a lot an answer for this question, but none of them seem to do exactly what I want. A lot of tutorials show me how to add lines and polygons in code, but not with freehand drawing.

我在地图上有一个带有UIImageView的MKMapView。我可以在我的UIImageView上画一点,但在那之后,MKMapView会被拖延,画面也不会继续。

I got a MKMapView with a UIImageView over the map. And I can draw a little on my UIImageView but right after that, the MKMapView is draged and the draw doesn't continue.

我想知道我做了什么错误?

I would like to know what have I done wrong ?

以下是我的触摸事件代码:

Here is my code for the touches events :

- (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的按钮代码:

And the code of the button who show the UIImageView :

编辑:我这是为了停止与地图的互动:

EDIT : I made this for stoping the interactions with the map :

- (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;
    }
}

完成抽奖后,我想要将此绘制转换为表格(区域),将其放在地图上。如果你对我有一些指示?

After doing my draw, I would like to transform this draw into a form (area), that will be put on the map. If you have some indications for me ?

谢谢,抱歉我的英语不好。

Thank you, and sorry for my bad english.

推荐答案

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

Draw my map try this , this is draw map point project

这篇关于在MKMapView上绘制UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆