Swift 填充 Xib 图像元素时 MapView 不保留最新班次
Swift MapView Doesn't Retain Latest Shift when Populating Xib Image Element
我有一个带有 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 再次下降。
您需要先让 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)
}
}
我有一个带有 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 再次下降。
您需要先让 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)
}
}