2016-09-15 3 views
1

MapKit을 사용하여지도에서 사용자의 방향을 보여주는 방법을 찾으려고합니다. 기본 MapKit 방법은 항상 전체지도를 회전시킵니다. 사용자 위치도 MKAnnotationView이므로 특정 클래스를 만들어서 재정의하고 특정 이미지 (화살표 사용)를 사용하기로 결정했습니다. 신속하게 MapKit을 사용하여 사용자 정의 userLocationAnnotationView 이미지를 회전하는 방법은 무엇입니까?

class UserLocationAnnotationView: MKAnnotationView { 

override init(frame: CGRect) { 
    super.init(frame: frame) 
} 

override init(annotation: MKAnnotation!, reuseIdentifier: String!) { 

    super.init(annotation: annotation, reuseIdentifier: reuseIdentifier) 

    var frame = self.frame 
    frame.size = CGSizeMake(130, 130) 
    self.frame = frame 
    self.backgroundColor = UIColor.clearColor() 
    self.centerOffset = CGPointMake(-30, -50) 

} 

required init(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder)! 
} 

/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
*/ 
override func drawRect(rect: CGRect) { 
    // Drawing code 
    UIImage(named: "userLocation.png")?.drawInRect(CGRectMake(65, 65, 65, 65)) 


} 

는 지금은을 locationManager의 didUpdateHeading의 FUNC에 그 MKAnnotationView 이미지를 회전 할 수있는 방법을 찾기 위해 노력하고있어.

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { 

var userLocationView :MKAnnotationView? 

func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) { 
    print(newHeading.magneticHeading) 
} 

newHeading.magneticHeading의 인쇄물이 꽤 정확합니다. 이제 어떻게 사용자 정의 UserLocationAnnotationView를 회전시킬 수 있습니까?

도움 주셔서 감사합니다.

답변

2

이 시점에서 완전한 코드 예제를 제공 할 수는 없지만 앞으로 몇 가지 지침을 제공 할 수 있기를 바랍니다.

먼저, MKAnnotationView의 하위 클래스를 반드시 가져야한다고 생각하지 않습니다. 단순히 UIImageimage 속성에 할당 할 수 있습니다. 나는 그것이 당신이 커스터마이징을 요구하지 않는 한 일을 더 쉽게 만들 것이라고 생각한다.

이제 맵에 주석을 추가하고 그에 대한 참조가 있다고 가정합니다.

제목 표시를 회전하려면, 나는 세 가지 옵션을 참조하십시오

  1. 회전 MKAnnotationViewimage 재산 :
    • 방법 : 제목 변경의 UIImage의 회전 된 사본을 만들 때와 그것을 image 속성에 할당하십시오. Example (테스트하지 않음).
    • 단점 : 회전을 쉽게 애니메이트 할 수 없습니다.
      • 방법 :
    • MKAnnotationView 자체 회전 제목 변경, MKAnnotationView '의 S/ UIViewtransform 속성을 사용합니다. 적절한 CGAffineTransform을 할당하십시오.
    • 전문가 : 가장 쉽습니다.
    • 단점 : 상세 정보/설명 선보기도 회전합니다. 필요한 경우이 옵션을 사용할 수 없습니다.
      • 방법 :
    • UIImage MKAnnotationView- add that one as a subview UIImageView에 등을 넣어 2. 유사를하지만 MKAnnotationView 자체에 직접하지 UIImageViewtransform 속성을 사용합니다. 이렇게하면 설명 선보기는 회전되지 않습니다.
    • 프로 : 잘 작동해야합니다.
    • 단점 : 좀 더 많은 작업이 필요합니다.

당신은 또한 필요한 것 :

  • 함수는 라디안으로 변환 할 수 있습니다. 아핀 변환에는 라디안이 필요합니다.
  • transform 속성의 고정animate 방법의 변경 내용을 적용하려면 회전 (1 제외)의 애니메이션을 적용하십시오.
+0

고맙습니다. @ 아서! 비록 당신이 어떤 코드를 제공하지 않았더라도 정확하게 대답했고 다른 방법으로 나는 그것을 작동하게하는 길을 찾았습니다. userAnnotationView에 대한 콜 아웃 뷰가 필요하지 않으므로 두 번째 방법을 사용하기로 결정했습니다. 또한 UIView의 애니메이션 메서드 사용에 대한 마지막 조언은 큰 도움이되었습니다. 다시 한 번 고마워. – matthioo

관련 문제