iphone - 向 MKMapView 添加 subview (不是叠加层)

标签 iphone ios mkmapview uigesturerecognizer mkmapviewdelegate

我想在 iOS map View 的点击点添加一个小 subview ,这样当我滚动和缩放 map View 时,添加的 subview 也会缩放和滚动。有什么帮助吗?我试过的代码如下:

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

View dotimage 不随 map View 移动和滚动。

最佳答案

您的方法是错误的,您无法将 View 添加为缩放 map 中的 subview ,您必须在点击时添加自定义图钉,自定义图钉应该看起来像您要添加的 View 。

你可以试试下面的代码

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

一切顺利..

关于iphone - 向 MKMapView 添加 subview (不是叠加层),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16228522/

相关文章:

ios - 将 CALayer 添加到 MKOverlayRenderer?

ios - 更改 View 后 MKMapView 重置

c - iOS 5.0 SDK GCC 对 4.2.x 设备的奇怪副作用

iphone - 在 map View 中处理错误的地址

iphone - 使用三种不同的排序选项对数组进行排序

iphone - 在 _one_ ASIHTTPRequest 中流式传输多个文件

ios - 来自 iOS "Do not add subviews directly to the visual effect view itself"的警告

ios - 有没有办法在没有 Google Sign-In for iOS 的情况下实现 firebase 邀请(短信)?

iphone - 放置带有动画的相机

css - 字体系列应用于 iphone 模拟器但未应用于真正的 iphone 设备?