如何添加子视图查看器迅速? [英] How to add child to view controller swift?

查看:37
本文介绍了如何添加子视图查看器迅速?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像添加到视图控制器上的特定坐标,我已经导入了SpriteKit来查看是否有帮助.我只需要添加图像并能够在UIBezierPath上移动它,但它不会让我添加Child.

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)

推荐答案

通过查看上面的 code ,您的问题之一是 self .addChildViewController

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

在Swift中,要向 self 发送消息,您必须使用.dotSyntax.并且 addChild()函数需要一个 UIViewController

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)语法正确,但是blueDot类型: SKNode SKNode 不继承自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.

我建议您改为这样做:

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

现在,您在ViewController的视图层次结构中有一个图像,或者您更喜欢引用它:父对象中的一个子对象.

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.

这篇关于如何添加子视图查看器迅速?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆