我想在iOS地图视图的分接点添加一个小子视图,这样当我滚动和缩放地图视图时,添加的子视图也会缩放和滚动。有帮助吗?我试过的代码如下:
- (void)viewDidLoad
{
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(foundTap:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1;
[self.myMapView addGestureRecognizer:tapRecognizer];
}
- (IBAction)foundTap:(UITapGestureRecognizer *)recognizer
{
CGPoint point = [recognizer locationInView:self.myMapView];
dotimage = [[UIView alloc]initWithFrame:CGRectMake(point.x,point.y , 10, 10)];
dotimage.backgroundColor = [UIColor redColor];
[self.myMapView addSubview:dotimage];
}
视图dotimage
未移动并使用地图视图滚动。
答案 0 :(得分:2)
您的方法是错误的,您无法在地图缩放时将视图添加为子视图,您必须自动添加自定义图钉,自定义图钉应该看起来像您要添加的视图..
您可以尝试以下代码
- (void)viewDidLoad
{
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addCustomView:)];
[recognizer setNumberOfTapsRequired:1];
[map addGestureRecognizer:recognizer];
[recognizer release];
}
- (void)addCustomView:(UITapGestureRecognizer*)recognizer
{
CGPoint tappedPoint = [recognizer locationInView:map];
//Get the coordinate of the map where you tapped
CLLocationCoordinate2D coord= [map convertPoint:tappedPoint toCoordinateFromView:map];
//Add Annotation
/* Create a custom annotation class which takes coordinate */
CustomAnnotation *ann=[[CustomAnnotation alloc] initWithCoord:coord];
[map addAnnotation:ann];
}
然后在你map delegate
函数
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
if([annotation isKindOfClass:[CustomAnnotation class]])
{
//Do your annotation initializations
// Then return a custom image that looks like your view like below
annotationView.image=[UIImage imageNamed:@"customview.png"];
}
}
All the Best ..