ios - 如何快速添加 subview Controller ?

标签 ios iphone xcode swift

我试图将图像添加到 View Controller 上的特定坐标,我已经导入了 SpriteKit 以查看是否有帮助。我只需要添加图像并能够在 UIBezierPath 上移动它,但它不会让我添加 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

之间的空格

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

的参数

self.addChild(blueDot) 语法是正确的,但是 blueDot 的类型是:SKNode 并且 SKNode 没有继承自 UIViewController 所以你不能通过那个论点。

我建议您改为这样做:

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

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

关于ios - 如何快速添加 subview Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28396659/

相关文章:

objective-c - 是否可以对多个应用程序使用相同的应用程序ID?

ios - CloudKit 私有(private)数据库返回前 100 个 CKRecords

iphone - CALayer drawInContext 与 addSublayer

iphone - UITabBarController 触摸响应速度慢

iOS - 如何将 bundle 放入 Bundle.main.resourcePath?

ios - UNUserNotificationCenter.current().getDeliveredNotifications 只返回一个空数组

ios - 如何在单元格中显示进度?

iphone - NSXMLParser iPhone 问题

ios - 在 VS for Mac 中无法查看个人团队配置文件

iphone - 如何使用自定义 inputView 设计 iOS 程序