【发布时间】:2015-04-14 17:26:52
【问题描述】:
我正在使用 Swift 构建一个小应用程序,我想在其中实现以下目标: - 带有多个注释的 MapView - 单击其中一个注释会在覆盖层中显示一些详细信息,该覆盖层从底部移动并填充大约。屏幕的一半 - 显示屏的上半部分仍显示 MapView。点击地图关闭详情
现在我已经阅读了很多关于不同可能性的信息。首先,我尝试使用具有以下内容的 ContainerView:
@IBOutlet weak var detailsContainerYPosition: NSLayoutConstraint!
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
UIView.animateWithDuration(0.3, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self. detailsContainerYPosition.constant = 0
self.view.layoutIfNeeded()
}, completion: nil)
}
问题在于,viewDidLoad() 方法只触发了一次,我想在将一些数据传递给控制器类之后使用它来构建详细信息页面。
所以接下来我玩了一个自定义的 segue 并想出了这个:
class DetailsSegue: UIStoryboardSegue {
override func perform() {
// Assign the source and destination views to local variables.
var mapView = self.sourceViewController.view as UIView!
var detailsView = self.destinationViewController.view as UIView!
// Get the screen width and height.
let screenWidth = UIScreen.mainScreen().bounds.size.width
let screenHeight = UIScreen.mainScreen().bounds.size.height
// Specify the initial position of the destination view.
detailsView.frame = CGRectMake(0.0, screenHeight, screenWidth, 140)
// Access the app's key window and insert the destination view above the current (source) one.
let window = UIApplication.sharedApplication().keyWindow
window?.insertSubview(detailsView, aboveSubview: mapView)
// Animate the transition.
UIView.animateWithDuration(0.4, animations: { () -> Void in
detailsView.frame = CGRectOffset(detailsView.frame, 0.0, -140)
}, completion: nil)
}
}
这似乎是更好的解决方案,因为我可以在 prepareForSegue() 函数中向新控制器发送一些数据,并且每次启动此 segue 时都会触发 viewDidLoad() 方法,但现在我正在努力: - 每次单击注释并调用 segue 时,都会插入一个新视图。但是如果一个细节视图已经可见,我只想更新这个 - 当从 MapView 调用 didDeselectAnnotationView 时,我找不到任何方法来展开这个 segue。
但也许这里的任何人都有更好的方法来实现这样的 UI。
【问题讨论】:
标签: ios swift uiviewcontroller segue uicontainerview