Я много раз искал ответ на этот вопрос, но ни один из них, похоже, не делает именно то, что я хочу. Много учебников показывают мне, как добавлять линии и полигоны в код, но не с рисованием вручную.Нарисуйте UIImageView над MKMapView
У меня есть MKMapView с UIImageView над картой. И я могу немного нарисовать свой 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:
EDIT: Я сделал это для останавливая взаимодействия с картой:
- (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