swift - SpriteKit 中的 MKMapView - Swift

标签 swift sprite-kit mkmapview skscene

我在 SKScene 中使用 MKMapView,但它没有显示。这是我的代码。

    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 中执行此操作的教程,因此其中大部分内容都是猜测。我错过了什么?

更新1:我应该如何将MKMapView添加到MoveableArea?

最佳答案

        let map : MKMapView! = MKMapView()
        map.delegate = self
   // below line is missing  
   map.frame=CGRect(x: 0,y: 0,width: self.size.width,height: self.size.width);
        let location = CLLocationCoordinate2DMake(52.5031135, -6.572772100000066)
        map.setRegion(MKCoordinateRegionMake(CLLocationCoordinate2DMake(52.5031135, -6.572772100000066), MKCoordinateSpanMake(0.05, 0.05)), animated: true)

        let 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)

you missed frame of your map view 

关于swift - SpriteKit 中的 MKMapView - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41387548/

相关文章:

macos - 改变 NSWindow 投影的重量?

ios - Swift Playground - 对类使用未实现的初始化程序 'init(size:)'

ios - SpriteKit游戏: Moving Gameplay View

swift - Sprite 套件中弹丸的平滑过渡

swift - 快速的大中央调度(GCD)

json - 使用 alamofire + swift 获取动态加载的 html

swift - 每天更改重复提醒的内容

ios - 带有自定义标注和按钮的 MKAnnotationView 子类

iphone - iOS iPhone 在空间中显示用户方向和方向,如 MKMapView 上的指南针应用程序

iphone - CLLocationCoordinate2D 不知道数组中有多少?