ViewController 作为 Swift 中 MapView 的叠加层(如 Google 地图应用程序用户界面) [英] ViewController as Overlay on MapView in Swift (like Google Maps Application User Interface)

查看:23
本文介绍了ViewController 作为 Swift 中 MapView 的叠加层(如 Google 地图应用程序用户界面)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Swift 构建一个小应用程序,我想在其中实现以下目标:- 带有多个注释的 MapView- 单击其中一个注释会在从底部移入的叠加层中显示一些细节并填充大约.屏幕的一半- 显示屏的上半部分仍显示 MapView.点击地图关闭详细信息

I'm building a small app with Swift where I want to achieve following: - MapView with several Annotations - Click on one of the annotations shows some Details in an overlay that is moving in from the bottom and fill approx. half of the screen - The upper half of the display still shows the MapView. A click on the map closes the details

现在我已经阅读了很多关于不同可能性的信息.首先,我尝试使用具有以下内容的 ContainerView:

Now I've read a lot about different possibilities. First I've tried using a ContainerView with something like this:

@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() 方法只被触发一次,我想在将一些数据传递给控制器​​类后使用它来构建详细信息页面.

The problem with that was, that the viewDidLoad() method was only fired once and I wanted to use that to build up the Details page after passing some data to the controller class.

所以接下来我尝试了自定义转场并想出了这个:

So next I've played around with a custom segue and cam up with this:

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.

This seems to be the better solution because I could send some data to the new controller within the prepareForSegue() function AND the viewDidLoad() method is fired every time this segue is initiated but now I'm struggling: - Everytime the annotation is clicked and the segue is called, a new View is inserted. But if ONE detail view is already visible, I would like to just update this one - I can't find any way to unwind this segue when didDeselectAnnotationView is called from the MapView.

但也许这里的任何人都有更好的方法来实现这样的用户界面.

But perhaps anybody here has even a better way to achieve an UI like this.

推荐答案

我终于通过手动添加一个 ViewController 和这样的视图来获得我想要的:

I've finally managed to get what I wanted by manually adding a ViewController and a view like this:

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)

当关闭细节时,我像这样删除控制器:

And when dismissing the Details, I remove the Controller like this:

self.detailsController.removeFromParentViewController()

我创建了一个小示例来演示我的解决方案:https://github.com/flavordaaave/iOS-Container-View-Overlay

I created a small sample to demonstrate my solution: https://github.com/flavordaaave/iOS-Container-View-Overlay

这篇关于ViewController 作为 Swift 中 MapView 的叠加层(如 Google 地图应用程序用户界面)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆