将代码从UIViewController转到UIView

时间:2016-02-27 05:28:44

标签: ios swift uiview uiviewcontroller mapbox

我正在寻找在UIView中创建这个地图。这可能吗?如果是这样,你能告诉我如何将这段代码从UIViewController转到UIView。我正在尝试将MapBox地图放入UIView中,并且只有如何将其导入UIViewController的说明

这段代码如何看作UIView类:

import Mapbox

class mapboxMap: UIViewController, MGLMapViewDelegate {

var mapView: MGLMapView!

override func viewDidLoad() {
    super.viewDidLoad()

    mapView = MGLMapView(frame: view.bounds)
    mapView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

    // set the map's center coordinate
    mapView.setCenterCoordinate(CLLocationCoordinate2D(latitude: 40.7326808,
        longitude: -73.9843407),
        zoomLevel: 10, animated: false)
    view.addSubview(mapView)

    // Set the delegate property of our map view to self after instantiating it.
    mapView.delegate = self

    // Declare the marker `hello` and set its coordinates, title, and subtitle
    let hello = MGLPointAnnotation()
    hello.coordinate = CLLocationCoordinate2D(latitude: 40.7326808, longitude: -73.9843407)
    hello.title = "Hello world!"
    hello.subtitle = "Welcome to my marker"
    mapView.addAnnotation(hello)

    let hello2 = MGLPointAnnotation()
    hello2.coordinate = CLLocationCoordinate2D(latitude: 40.7526808, longitude: -73.9843407)
    hello2.title = "Hello world!"
    hello2.subtitle = "Welcome to my marker"
    mapView.addAnnotation(hello2)


}

// Use the default marker; see our custom marker example for more information
func mapView(mapView: MGLMapView, imageForAnnotation annotation: MGLAnnotation) -> MGLAnnotationImage? {
    return nil
}

func mapView(mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
    return true
}

override func viewDidAppear(animated: Bool) {
    // Wait a bit before setting a new camera.

    // Create a camera that rotates around the same center point, back to 0°.
    // `fromDistance:` is meters above mean sea level that an eye would have to be in order to see what the map view is showing.
    let camera = MGLMapCamera(lookingAtCenterCoordinate: mapView.centerCoordinate, fromDistance: 9000, pitch: 45, heading: 0)

    // Animate the camera movement over 5 seconds.
    mapView.setCamera(camera, withDuration: 2.5, animationTimingFunction: CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut))
}

}

1 个答案:

答案 0 :(得分:2)

我认为你要做的是“在子视图中添加UIViewController”。尝试使用Container view Controller。以下主题可能对您有所帮助。

add UIViewController in subview