gpt4 book ai didi

ios - 旋转 MKAnnotationView 的显示图像

转载 作者:行者123 更新时间:2023-11-28 22:44:23 25 4
gpt4 key购买 nike

我目前正在使用 CGAffineTransformMakeRotation 旋转 MKAnnotationView 的图像属性。但是,在这种情况下,整个 View 以及标注气泡都会旋转。所以我听从了 MKAnnotationView Only Rotate the Image property 中的建议,现在正在创建一个 UIImageView 并将其作为 subview 添加到 MKAnnotationView。图像现在可以毫无问题地显示和旋转。但是,现在 MKAnnotationView 不响应触摸事件。我需要它像以前一样运行 - 点击/触摸它应该显示标注气泡。有什么想法我需要做什么吗?

我尝试的初始方式(旋转标注气泡):

MKAnnotationView *aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];
aView.image = [UIImage imageNamed:@"mapPin.png"];
float newRad = degreesToRadians(degreesToRotate);
[aView setTransform:CGAffineTransformRotate(CGAffineTransformIdentity, newRad)];

使用 UIImageView 并添加为 subview :

MKAnnotationView *aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];
UIImageView *iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mapPin.png"]];
iv.userInteractionEnabled = YES;
[aView addSubview:iv];
aView.canShowCallout = YES;

更新我试图添加一个手势识别器,但它不起作用。当我点击 map 上 MKAnnotationView 中的 UIImageView 时,不会调用 imageTapped 方法。这是在方法内:- (MKAnnotationView *)mapView:(MKMapView *)mView viewForAnnotation:(id )annotation

MKAnnotationView *aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];

UIImageView *iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mapPin.png"]];
iv.userInteractionEnabled = YES;
iv.exclusiveTouch = NO;

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
[iv addGestureRecognizer:tapGesture];

[aView addSubview:iv];

最佳答案

我尝试了这里的所有答案,但没有一个完全有效,但我找到了一个不同的解决方案!

我相信最简单的方法是在设置 MKAnnotationViewimage 属性之前旋转 UIImage 本身。如果您有成百上千的注释,这可能会表现不佳,但如果您只需要旋转一些东西(就像我的情况),它就可以正常工作。

我找到了一个帮助类来旋转 UIImage,然后在设置 MKAnnotationViewimage 属性之前简单地使用它来旋转 UIImage。

辅助类在这里:http://www.catamount.com/forums/viewtopic.php?f=21&t=967

现在,要在不牺牲标注气泡的情况下在 map 上旋转 MKAnnotationView 的 UIImage 属性,请执行以下操作:

- (MKAnnotationView *)mapView:(MKMapView *)pMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MyAnnotationView *annotationView = [[MyAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"WayPoint"];

annotationView.image = [annotationView.image imageRotatedByDegrees:45.0];

annotationView.canShowCallout = YES;

return annotationView;
}

关于ios - 旋转 MKAnnotationView 的显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13652772/

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