简体   繁体   中英

How to add child to view controller swift?

Im trying to add an image to a specific coordinate on the view controller, I already imported SpriteKit to see if that could help. I just need to add the image and be able to move it on a UIBezierPath but it wont let me add Child.

    var blueDot = SKSpriteNode()
    var blackDot = SKSpriteNode()
    var levelLabelInt = levelLabel.text!.toInt()

    var blueDotTexture = SKTexture(imageNamed: "Sprites.atlas/BlueDot.png")
    blueDot = SKSpriteNode(texture: blueDotTexture)
    blueDot.position = CGPoint(x: (50), y: levelLabelInt! + 100)
    self .addChildViewController: ViewController (blueDot)

By looking at the code above, one of your issues is a space between self and .addChildViewController

In Swift to send a message to self you have to use .dotSyntax. and the addChild() function requires an argument of type UIViewController

self.addChild(blueDot) syntax is correct but blueDot is of type: SKNode and SKNode doesn't inherit from UIViewController so you can't pass that argument.

I suggest you do this instead:

    let image = UIImage(named: "Sprites.atlas/BlueDot.png")
    let imageView = UIImageView(image: image)
    self.view.addSubview(imageView)

Now you have an image inside your ViewController's views hierarchy or as you prefer to refer to it: a child object inside your parent object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM