2016-11-27 7 views
1

mapView가있는 Xcode 프로젝트가 있습니다. 이 배열에서 내 친구 (Kevin)를로드합니다.Xib 이미지 요소를 채울 때 Swift MapView가 최신 시프트를 유지하지 않습니다.

myProfiles.append(UserClass 
    (locationLatitude:44.067, locationLongitude:-88.296, 
     id:1,username: "Kevin" 
    ) 
) 

지도에 내 친구를 표시하는 customAnnotation이 있습니다. 내 친구 주석은지도의 맨 아래에 있습니다. 따라서 주석을 클릭하면 mapView가 203 칸만큼 위로 밀고 거기에 머물러 있어야합니다.

// triggered when the user selects an annotation. We are clicking on Kevin 
func mapView(_ mapView: MKMapView, didSelect annotationView: MKAnnotationView) 
{ 
    print("before setting map") 
    let adjustedBy: CGFloat = 203.0 
    self.mapView.frame.origin.y = self.mapView.frame.origin.y - adjustedBy 
    self.populateSelectedProfile() 
} 

func populateSelectedProfile() { 
    DispatchQueue.main.async { 
     // the tag 1000 is for the first user in array (username: Kevin) 
     if let userXib = self.view.viewWithTag(1000) as? User { 
      print("setting up user") 
      userXib.isHidden = false 
      userXib.userImageView.image = UIImage(named: "user") 
     } 
    } 
} 

populateSelectedProfile은 (이미지 뷰는 빌더에서 만든) 친구의 이미지로 XIB를 채우기 시작합니다 :

@IBOutlet weak var userImageView: UIImageView! 

을하지만 발생하면 상단에 밀어 후지도, 그것은가는 것입니다 이미지 뷰를 채우고 바로 아래로. 인쇄 줄에 중단 점을 배치하면이 동작을 볼 수 있습니다.

print("setting up user") 

왜 mapView가 다시 아래로 이동합니까?

참고 : 이미지를 표시하는 데 신경 쓰지 않습니다. 나는 그것을하는 방법을 안다. 내 문제는 mapView 다시 내려 오는 것입니다.

답변

1

완성 처리기를 통해 xib 마무리를 먼저로드 한 다음 애니메이션 부분을 수행해야합니다. 메모리에서 xib를로드하면지도 뷰가 원래 상태로 재설정됩니다.

// triggered when the user selects an annotation. We are clicking on Kevin 
func mapView(_ mapView: MKMapView, didSelect annotationView: MKAnnotationView) 
{ 
    print("before setting map") 
    let adjustedBy: CGFloat = 203.0 
    self.mapView.frame.origin.y = self.mapView.frame.origin.y - adjustedBy 
    self.populateSelectedProfile { finished in 
     DispatchQueue.main.async { 
      let adjustedBy: CGFloat = 203.0 
      self.mapView.frame.origin.y = self.mapView.frame.origin.y - adjustedBy 
     } 
    } 
} 

func addUserXib(completion: @escaping ((_ finished: Bool) -> Void)) { 
    if let userXib = Bundle.main.loadNibNamed("User", owner: self, options: nil)?[0] as? User 
    { 
     userXib.tag = 1000 
     userXib.frame = CGRect(x: width, y: 0, width: width, height: height * (3/8)) 
     userXib.isHidden = true 
     self.view.addSubview(userXib) 
    } 
}