0
我在SKScene中使用MKMapView,但不显示。这是我的代码。SpriteKit中的MKMapView - Swift
import SpriteKit
import MapKit
import CoreLocation
class Map : SKScene, MKMapViewDelegate {
var startY: CGFloat = 0.0
var lastY: CGFloat = 0.0
var moveableArea = SKNode()
override func didMove(to view: SKView) {
// set position & add scrolling/moveable node to screen
moveableArea.position = CGPoint(x: 0,y: 0)
self.addChild(moveableArea)
//moveableArea.addChild(Node Here)
var map : MKMapView! = MKMapView()
map.delegate = self
let location = CLLocationCoordinate2DMake(52.5031135, -6.572772100000066)
map.setRegion(MKCoordinateRegionMake(CLLocationCoordinate2DMake(52.5031135, -6.572772100000066), MKCoordinateSpanMake(0.05, 0.05)), animated: true)
var annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2DMake(location.latitude, location.longitude)
annotation.title = "This is my Town!"
annotation.subtitle = "Enniscorthy, Co. Wexford"
map.addAnnotation(annotation)
map.showAnnotations([annotation], animated: true)
map.center = CGPoint(x: self.size.width/2, y: self.size.width/2)
view.addSubview(map)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
startY = location.y
lastY = location.y
let tappednode = atPoint(location)
let tappedNodeName: String? = tappednode.name
if tappedNodeName == "exit" {
if let view = self.view {
let scene = MenuGame(size: self.size)
scene.scaleMode = .aspectFill
view.presentScene(scene)
}
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = (touch as AnyObject).location(in: self)
// set the new location of touch
var currentY = location.y
// Set Top and Bottom scroll distances, measured in screenlengths
var topLimit:CGFloat = 0.0
var bottomLimit:CGFloat = 0.6
// Set scrolling speed - Higher number is faster speed
var scrollSpeed:CGFloat = 1.0
// calculate distance moved since last touch registered and add it to current position
var newY = moveableArea.position.y + ((currentY - lastY)*scrollSpeed)
// perform checks to see if new position will be over the limits, otherwise set as new position
if newY < self.size.height*(-topLimit) {
moveableArea.position = CGPoint(x: moveableArea.position.x, y: self.size.height*(-topLimit))
}
else if newY > self.size.height*bottomLimit {
moveableArea.position = CGPoint(x: moveableArea.position.x, y: self.size.height*bottomLimit)
}
else {
moveableArea.position = CGPoint(x: moveableArea.position.x, y: newY)
}
// Set new last location for next time
lastY = currentY
}
}
}
没有关于如何在SpriteKit中做到这一点的教程,这是猜测。我错过了什么?
Update1:我应该如何将MKMapView添加到MoveableArea?
作品完美!我有一个问题,我在这个SKScene中使用了一种触摸Moved作为滚动视图。 (更新中的完整代码)我的所有SkSpriteNode()和SKLabelNodes()都添加了moveArea.addChild(---)。我应该怎么做才能添加这个? –