在我的主视图 Controller 中,我有一个 MKMapView 和 TableView。 tableview 使用 AlamoFire 调用用户信息 API 来填充其单元格。
我希望发生什么: 当您选择一个单元格时,带有更详细的用户信息的第二个容器 View 将进入屏幕中表格 View 所在的部分。同时, map View 将标注该用户的位置。
问题: 当选择单元格时,第二个容器 View 滑到屏幕上(好), map 注释(好),但随后信息容器 View 再次消失(坏)。第二次点击最初选择的单元格时,信息容器 View 会滑回屏幕上并停留。我需要摆脱这种双击。
map 注释似乎由于某种原因否定了这个过程。当我注释掉 map 注释时,VC 转换工作正常......只需轻轻一按。
任何想法都会有帮助。
这是有问题的代码:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.localTableView.deselectRowAtIndexPath(indexPath, animated: true)
dispatch_async(dispatch_get_main_queue(),{
self.populateInfoContainerView(indexPath.row)
self.mapView.selectAnnotation(self.users[indexPath.row], animated: true)
if(self.userInfoContainerView.frame.origin.x == UIScreen.mainScreen().bounds.size.width){
self.showInfoView(true)
} else {
self.hideInfoView(true)
}
})
}
func showInfoView(animated: Bool){
UIView.animateWithDuration(0.33, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 1.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: ({
self.userInfoContainerView.frame.origin.x = 0
self.localTableView.frame.origin.x = -UIScreen.mainScreen().bounds.size.width
}), completion: { finished in
//animation complete
})
self.infoVisible = true
}
func hideInfoView(animated: Bool){
let xPos = UIScreen.mainScreen().bounds.size.width
if(animated == true){
UIView.animateWithDuration(0.25, animations: ({
self.userInfoContainerView.frame.origin.x = xPos
self.localTableView.frame.origin.x = 0
}), completion: { finished in
//animation complete
})
} else {
self.userInfoContainerView.frame.origin.x = xPos
}
self.infoVisible = false
}
谢谢。
最佳答案
我相信我已经找到答案了。我不确定为什么这个有效,而上面的版本不起作用,但我会向您展示我做了什么不同的事情。
我现在正在为 TableView 和 InfoView 的约束设置动画。
首先,我按住 ctrl+单击并将约束从 main.storyboard 拖到我的代码中,并创建一个 IBOutlet 来制作动画。我还添加了 let screenWidth
作为移动它们的常量。
@IBOutlet weak var infoViewLeadingEdge: NSLayoutConstraint!
@IBOutlet weak var tableViewXCenter: NSLayoutConstraint!
let screenWidth = UIScreen.mainScreen().bounds.size.width
然后我在 showInfo()
中替换了上面的代码:
self.userInfoContainerView.frame.origin.x = 0
self.localTableView.frame.origin.x = -UIScreen.mainScreen().bounds.size.width
使用这个新代码:
self.infoViewLeadingEdge.constant -= self.screenWidth
self.tableViewXCenter.constant += self.screenWidth
self.view.layoutIfNeeded()
在上面的 hideInfo()
中,我替换了以下内容:
self.userInfoContainerView.frame.origin.x = xPos
self.localTableView.frame.origin.x = 0
使用这个新代码:
self.infoViewLeadingEdge.constant += self.screenWidth
self.tableViewXCenter.constant -= self.screenWidth
self.view.layoutIfNeeded()
不要忘记self.view.layoutIfNeeded()
。如果您不使用该代码,动画将不起作用。另外,tableViewXCenter 上的 += 和 -= 似乎与我希望表格执行的操作相反,但是,这是我需要放置它们的方式,以便让表格按照我想要的方向移动。但这看起来确实违反直觉。
就是这样。我希望这对某人有帮助。
关于ios - 尝试注释 MapView 并同时加载第二个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31601893/