2012-11-30 46 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)