gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:00:16 26 4
gpt4 key购买 nike

我想在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com