2012-11-30 2 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; 

업데이트 나 추가하려고 동작하지 않지만 제스처 인식기입니다. imageTapped 메서드는지도에서 MKAnnotationView 내의 UIImageView를 누를 때 호출되지 않습니다. 이 방법에 있습니다 : - (MKAnnotationView *)지도보기 (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

제게 쓴 방법을 알려주세요. 제스처 인식기를 이미지보기에 추가하고 이미지보기 사용자 상호 작용을 예로 설정 한 것을 기억해야합니다. –

+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를 회전시킨 다음 MKAnnotationView이라는 속성을 설정하기 전에 UIImage를 회전하는 데 사용했습니다.

도우미 클래스는 여기에 있습니다 : 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

나는이 이제 실감 아주 오래된 질문이지만 최근에이 문제가 발생했고 아주 간단한 해결책이있었습니다. 이미지 뷰를 사용할 때 어노테이션 뷰가 unclickable이되는 유일한 이유는 이미지가 설정되지 않은 경우 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

다음과 같은 빠른 확장 방법을 사용할 수 있습니다. 가장 많이 사용되는 대답은 링크 된 도우미 라이브러리를 사용하여 흐릿한 이미지를 생성합니다.

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)