2012-11-30 47 views
0

我正在使用CGAffineTransformMakeRotation旋转MKAnnotationView的图像属性。然而,在这种情况下,整个视图都会旋转,以及标注泡泡。所以我按照MKAnnotationView Only Rotate the Image property之后的建议,现在创建一个UIImageView并将其作为子视图添加到MKAnnotationView中。图像现在显示并旋转,没有问题。但是,现在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并添加作为一个子视图:

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; 

更新 我试图添加一个手势识别器,但它不起作用。当我点击地图上的MKAnnotationView内的UIImageView时,imageTapped方法不会被调用。这是在方法中: - (MKAnnotationView *)的MapView:(的MKMapView *)MView的viewForAnnotation:(ID)注释

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

回答

0

尝试做这样的事情的图像视图,看看你是否可以通过它的触摸事件注释。

//Do this before you add the imageview as a subview 
self.imageView.userInteractionEnabled = YES; 
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)]; 
[self.imageView addGestureRecognizer:tapGesture]; 



- (void) imageTapped:(UITapGestureRecognizer *)tapGesture { 
    MKAnnotationView * annotationView = (MKAnnotationView *)tapGesture.view.superview; 
    if (annotationView.selected) { 
     [annotationView setSelected:NO animated:YES]; 
    } else { 
     [annotationView setSelected:YES animated:YES]; 
    } 
} 
+0

我没有运气尝试这个前几天,我将在本周末再次尝试一下,看看如果我有任何运气。 – mservidio

+0

给出我写它的方式,应该记住将手势识别器添加到图像视图并将图像视图用户交互设置为yes。 –

+0

触摸手势事件不会触发......我会发布我在问题中尝试的内容。 – mservidio

-2

//只需设置图像属性添加子视图之前和它的作品般的魅力

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

aView.image = [UIImage imageNamed:@"mapPin.png"]; // just this line 

[aView addSubview:iv]; 
aView.canShowCallout = YES; 
2

我在这里尝试了所有的答案,他们没有完全工作,但我发现了一个不同的解决方案,没有!

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

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

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

现在,在不牺牲标注泡沫旋转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; 
} 
+0

您的帮助程序类链接已死亡。你能更新它吗? – franiis

1

我意识到这是现在非常古老的问题,但我最近遇到了这个问题,它有一个非常简单的解决方案。当使用图像视图时,注释视图变得不可点击的唯一原因是当未设置图像时,MKAnnotationView的大小为(0,0)。图像视图是可见的,因为注解视图不会剪裁到边界。我所要做的就是在注解视图上设置框架,使其再次可点击。

MKAnnotationView *aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"]; 
UIImageView *iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mapPin.png"]]; 
[aView addSubview:iv]; 
annotationView.frame = (CGRect) { 
      .origin = CGPointZero, 
      .size = iv.frame.size 
     }; 
aView.canShowCallout = YES; 
0

您可以使用下面的Swift扩展方法来提供帮助。最流行的答案会导致使用链接帮助程序库的模糊图像。

extension UIImage { 

    func rotated(degrees degrees : Int) -> UIImage { 
     // calculate the size of the rotated view's containing box for our drawing space 
     let rotatedViewBox = UIView(frame: CGRectMake(0,0,self.size.width, self.size.height)) 
     let rotation = CGFloat(degrees) * CGFloat(M_PI)/180 
     let t = CGAffineTransformMakeRotation(rotation); 
     rotatedViewBox.transform = t; 
     let rotatedSize = rotatedViewBox.frame.size; 
     UIGraphicsBeginImageContextWithOptions(rotatedSize, false, 0.0); 
     let context = UIGraphicsGetCurrentContext(); 
     CGContextTranslateCTM(context, rotatedSize.width/2, rotatedSize.height/2); 
     CGContextRotateCTM(context, rotation); 
     CGContextScaleCTM(context, 1.0, -1.0); 
     CGContextDrawImage(context, CGRectMake(-self.size.width/2, -self.size.height/2, self.size.width, self.size.height), self.CGImage); 
     let rotatedImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     return rotatedImage; 
    } 
} 

用法:

annotationView.image = UIImage(named: "my_image")!.rotated(degrees: 45)