很高兴看到快速游乐场的标签-很高兴看到这种情况继续下去。
我一直在修改Swift,想知道MapKit是否可以在操场上使用,以便您可以使用“快速查看”功能,以便可以迭代并预览我的作品。我还没有弄清楚语法,所以我想问问是否有人在游乐场环境中探索过这种语法。我在想我需要一些代码来将其建立为 View Controller 。
import UIKit
import MapKit
// Sample UIView code which works great in the playground.
//var myView:UIView = UIView();
//myView.frame = CGRectMake(0, 0, 320, 560)
//myView.backgroundColor = UIColor.blueColor()
// Non working map code - no errors but can't figure out what to call or initiate to get the view in the Quick Look side.
var mapView = MKMapView();
mapView.region.center.latitude = mapView.userLocation.coordinate.latitude;
mapView.region.center.longitude = mapView.userLocation.coordinate.longitude;
mapView.region.span.latitudeDelta = 0.00725;
mapView.region.span.longitudeDelta = 0.00725;
猜猜我只是缺少了一些简单的mapView来初始化本身等。我喜欢解释性的游乐场区域,可以随意修改,只需要弄清楚它是否可以执行mao功能,而不仅仅是在Xcode中编写.swift文件和环境并获取更正式。
最佳答案
对于OS X上的XCode7.1和Swift2.1,我打开了时间轴并执行以下操作:
import MapKit
import XCPlayground
let delta = 5.0
let frame = CGRect( x:0, y:0, width:200, height:200 )
let mapView = MKMapView( frame: frame )
var region = MKCoordinateRegion()
region.center.latitude = 31.0
region.center.longitude = -110.0
region.span.latitudeDelta = delta
region.span.longitudeDelta = delta
mapView.setRegion( region, animated: true )
XCPlaygroundPage.currentPage.liveView = mapView
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
Xcode 9.x,Swift 4.1更新:
导入PlaygroundSupport
导入MapKit
let delta = 5.0
let frame = CGRect( x:0, y:0, width:200, height:200 )
let mapView = MKMapView( frame: frame )
var region = MKCoordinateRegion()
region.center.latitude = 31.0
region.center.longitude = -110.0
region.span.latitudeDelta = delta
region.span.longitudeDelta = delta
mapView.setRegion( region, animated: true )
PlaygroundPage.current.liveView = mapView
PlaygroundPage.current.needsIndefiniteExecution = true