ios - 旋转 MKAnnotationView 的显示图像

标签 ios ios6 mkmapview mkannotationview cgaffinetransform

我目前正在使用 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/

相关文章:

ios6 - 在 iOS 6.0 和 iOS 6.0.1 上调用 [FBSession openActiveSessionWithPermissions...] 时从 ACAccountStore 抛出 NSInvalidArgumentException

uiimageview - 显示图像的简单 UICollectionView 行为奇怪 : Some Images are displayed correct, 有些位置错误,有些则完全丢失

ios - 如何重新定位 MKMapView 的指南针?

mkmapview - IOS 上的 Mapkit 是否支持叠加层的出列? (就像注释一样)

iphone - 发布新版本的应用程序会删除旧版本的文件吗?

objective-c - 没有文本字段的 MFMessageComposeViewController

objective-c - 用 Objective-C 打电话不工作 IOS6

iOS map 套件 : how to prevent MKMapView setRegion from changing region?

ios - Swift3 麦克风音频输入 - 无录音播放

ios - 如何在没有 segue 的情况下使用通知中心将数据从第一个 TableView 发送到第二个 TableView