swift - 在 Swift 中初始化类属性

标签 swift class swift3 in-class-initialization

所以我正在尝试创建一个用作“演示文稿”的 Playground ,这意味着我有多个幻灯片并且它是交互式的。我是 Swift 的新手,所以真的需要一些帮助。

这是我的主要代码

import UIKit
import SpriteKit
import PlaygroundSupport

let frame = CGRect(x: 0, y: 0, width: 640, height: 480)

let view = SKView(frame: frame)
view.showsDrawCount = true
view.showsNodeCount = true
view.showsFPS = true

let scene = MainScene(size: CGSize(width: 640, height: 480))
view.presentScene(scene) //error here, explained below
PlaygroundPage.current.liveView = view

这是我的 MainScene.swift 代码 -

import UIKit
import SpriteKit
import PlaygroundSupport

public class Slide: SKView {

    public var title: SKLabelNode!
    public var info: UITextView!

    public func setter() {

        self.title = SKLabelNode(fontNamed: "Chalkduster")
        self.title.verticalAlignmentMode = .center
        self.title.color = SKColor.white
        self.title.position = CGPoint(x: frame.midX, y: 440)

        let framer = CGRect(x: 40, y: 60, width: 560, height: 360)

        self.info = UITextView(frame: framer)
        self.info.font = UIFont(name: "Chalkduster", size: 20)
        self.info.textAlignment = .center
        self.info.textColor = UIColor.white
        self.info.backgroundColor = UIColor.black
    }


}

public class MainScene: SKScene {

    public var button1 = SKSpriteNode(imageNamed: "Next")
    public var button2 = SKSpriteNode(imageNamed: "Previous")

    public var scene1: Slide?

    public override func didMove(to view: SKView) {

        backgroundColor = SKColor.black
        scene1?.setter()
        setScene1()
    }

    public func setScene1() {

        scene1?.title = SKLabelNode(fontNamed: "Chalkduster")
        scene1?.title.text = "Title."
        scene1?.title.position = CGPoint(x: frame.midX, y: frame.midY)
        scene1?.info.text = "Text."

        addChild(scene1?.title)
        addButtons()
    }

    public func addButtons() {

        self.button1.position = CGPoint(x: 600, y: 40)
        self.button2.position = CGPoint(x: 40, y: 40)
        addChild(self.button1)
        addChild(self.button2)
    }

}

基本上,我试图通过会更改上一张幻灯片的标题/信息的函数来设置多张幻灯片。但是,我认为为我的幻灯片定义一个 SKView 类以便使用 SKTransition 会好得多。

但是,在我当前的代码中,我遇到了执行中断错误。此外,当我改变一些东西时,错误类型跟着我进入了 Expected Declaration 或 no Class Initializers for MainScene.swift。

真的,我只是想修复类 Slide 的声明,然后使用该类制作多张幻灯片。

最佳答案

这是一个没有 SpriteKit 的例子。您真的应该复习一些基本的 Swift 和 UIKit 类(class),以获得良好的语言基础

import UIKit
import PlaygroundSupport

public class Slide: UIView {

    public var title: UILabel
    public var info: UITextView

    required public override init(frame:CGRect) {
        self.title = UILabel()
        self.title.font = UIFont(name: "Chalkduster", size: 20)
        self.title.textColor = UIColor.white
        self.title.backgroundColor = UIColor.red
        self.title.center = CGPoint(x: frame.midX, y: 440)

        self.info = UITextView()
        self.info.font = UIFont(name: "Chalkduster", size: 20)
        self.info.textAlignment = .center
        self.info.textColor = UIColor.white
        self.info.backgroundColor = UIColor.blue

        super.init(frame: frame)
        addSubview(title)
        addSubview(info)

    }
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

public class MainScene: UIView {
    public var scene1: Slide

    required public override init(frame: CGRect) {
        scene1 = Slide()
        super.init(frame: frame)
        scene1.title.text = "Title."
        scene1.title.sizeToFit()
        scene1.title.center = CGPoint(x: frame.midX, y: frame.midY - 20)
        scene1.info.text = "Text."
        scene1.info.sizeToFit()
        scene1.info.center = CGPoint(x: frame.midX, y: frame.midY + scene1.title.frame.height + 20)
        addSubview(scene1)
    }
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

let frame = CGRect(x: 0, y: 0, width: 640, height: 480)

let view = UIView(frame: frame)

let scene = MainScene(frame: frame)
view.addSubview(scene)
PlaygroundPage.current.liveView = view

关于swift - 在 Swift 中初始化类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43143802/

相关文章:

java - 需要帮助理解 NullPointerException

ios - 按下 UIButtons 作为文本字段的输入

ios - 如何将 Objective-C 静态扩展库链接/嵌入到 Xcode 13 项目中?

swift - 纵向和横向模式下可缩放的全屏图像

c++ - 类 : size of object of derived class

iOS - 使用 AVAudioSessionCategoryAmbient 和 duckOthers 和 mixWithOthers 开启静音

swift - 根据互联网连接刷新watchos应用程序

ios - 如何使用 alamofire 实现 put http 请求

swift - 覆盖在 Swift 的父类(super class)扩展中声明的类变量

java - 来自另一个类的 RadioButton setText