Ios ViewController作为Swift中MapView的覆盖(如谷歌地图应用程序用户界面)
我正在使用Swift构建一个小应用程序,希望实现以下目标: -带有多个注释的MapView -单击其中一个注释显示覆盖图中的一些详细信息,覆盖图从底部移入并填充大约一半的屏幕 -显示屏的上半部分仍显示地图视图。单击地图即可关闭详细信息 现在我读了很多关于不同可能性的书。首先,我尝试将ContainerView与以下内容结合使用:Ios ViewController作为Swift中MapView的覆盖(如谷歌地图应用程序用户界面),ios,swift,uiviewcontroller,segue,uicontainerview,Ios,Swift,Uiviewcontroller,Segue,Uicontainerview,我正在使用Swift构建一个小应用程序,希望实现以下目标: -带有多个注释的MapView -单击其中一个注释显示覆盖图中的一些详细信息,覆盖图从底部移入并填充大约一半的屏幕 -显示屏的上半部分仍显示地图视图。单击地图即可关闭详细信息 现在我读了很多关于不同可能性的书。首先,我尝试将ContainerView与以下内容结合使用: @IBOutlet weak var detailsContainerYPosition: NSLayoutConstraint! func mapView(mapV
@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)
}
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()
问题是viewDidLoad()方法只触发了一次,我想在将一些数据传递给controller类后使用它来构建详细信息页面
接下来,我使用了一个定制的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)
}
}
这似乎是更好的解决方案,因为我可以在prepareforsgue()函数中向新控制器发送一些数据,并且每次启动此segue时都会触发viewDidLoad()方法,但现在我正在努力:
-每次单击注释并调用segue时,都会插入一个新视图。但如果一个细节视图已经可见,我只想更新这一个
-当从MapView调用注释视图时,我找不到任何方法来展开此序列
但也许这里的任何人都有更好的方法来实现这样的UI。我终于设法通过手动添加ViewController和这样的视图来获得我想要的:
@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)
}
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()
在忽略细节时,我移除控制器,如下所示:
@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)
}
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()
我创建了一个小示例来演示我的解决方案:
谢谢,但您的GitHub项目似乎是空的;)哦,对不起。我刚刚更新了项目,因此它现在包含实际代码。请参阅