我有一个带有mapView的Xcode项目。我从这个数组中加载了我的朋友(Kevin):
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(在构建器中创建ImageView):
@IBOutlet weak var userImageView: UIImageView!
但是发生的事情是推送到顶部之后的地图,它在填充imageView之后立即下降。如果在打印行上放置一个断点,则可以看到此行为:
print("setting up user")
为什么mapView会再次向下移动?
请注意:我不关心显示图像。我知道该怎么做。我的问题是mapView再次下降。
答案 0 :(得分:1)
你需要让xib首先通过完成处理程序完成加载,然后你做动画部分。我认为从内存加载xib会将mapview重置为其原始状态
// 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)
}
}