ViewController 作为 Swift 中 MapView 的叠加层(类似于 Google 地图应用程序用户界面)
ViewController as Overlay on MapView in Swift (like Google Maps Application User Interface)
我正在用 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() 方法只触发了一次,我想在将一些数据传递给控制器后使用它来构建详细信息页面 class。
所以接下来我尝试了一个自定义的 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。
我终于通过手动添加 ViewController 和这样的视图获得了我想要的东西:
let detailsWidth: CGFloat = view.bounds.width
let detailsHeight: CGFloat = 150
let detailsViewFrame: CGRect = CGRectMake(0, view.bounds.height, detailsWidth, detailsHeight)
detailsController = storyboard?.instantiateViewControllerWithIdentifier("details") as! DetailsViewController
detailsController.descriptionText = "I'm a text that was passed from the MainViewController"
self.addChildViewController(detailsController)
detailsController.view.frame = detailsViewFrame
view.addSubview(detailsController.view)
detailsController.didMoveToParentViewController(self)
并且在取消详细信息时,我像这样删除控制器:
self.detailsController.removeFromParentViewController()
我创建了一个小示例来演示我的解决方案:
https://github.com/flavordaaave/iOS-Container-View-Overlay
我正在用 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() 方法只触发了一次,我想在将一些数据传递给控制器后使用它来构建详细信息页面 class。
所以接下来我尝试了一个自定义的 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。
我终于通过手动添加 ViewController 和这样的视图获得了我想要的东西:
let detailsWidth: CGFloat = view.bounds.width
let detailsHeight: CGFloat = 150
let detailsViewFrame: CGRect = CGRectMake(0, view.bounds.height, detailsWidth, detailsHeight)
detailsController = storyboard?.instantiateViewControllerWithIdentifier("details") as! DetailsViewController
detailsController.descriptionText = "I'm a text that was passed from the MainViewController"
self.addChildViewController(detailsController)
detailsController.view.frame = detailsViewFrame
view.addSubview(detailsController.view)
detailsController.didMoveToParentViewController(self)
并且在取消详细信息时,我像这样删除控制器:
self.detailsController.removeFromParentViewController()
我创建了一个小示例来演示我的解决方案: https://github.com/flavordaaave/iOS-Container-View-Overlay