【问题标题】:How to access enum values in other classes in Swift?如何在 Swift 中访问其他类中的枚举值?
【发布时间】:2014-12-20 14:55:59
【问题描述】:

我正在尝试使用场景工具包快速设置粒子发射器的属性。 我觉得很愚蠢,因为我不知道如何设置设置为枚举的常量。 代码如下:

//Create emitter
let emitter = SCNParticleSystem()

//set up the emitter trying to use the enum values
emitter.birthLocation = SCNParticleBirthLocation.SCNParticleBirthLocationSurface

//I have tried a few other ways including:
emitter.birthLocation = .SCNParticleBirthLocationSurface
emitter.birthLocation = SCNParticleSystem.SCNParticleBirthLocation.SCNParticleBirthLocationSurface

这不是我在不同类中使用枚举时遇到的唯一问题,所以我认为我有一些根本性的错误。 苹果框架参考链接,以防万一:https://developer.apple.com/library/prerelease/ios/documentation/SceneKit/Reference/SCNParticleSystem_Class/index.html#//apple_ref/occ/cl/SCNParticleSystem

最后是我的整个代码,以防万一我做错了完全不同的事情

import Foundation
import SceneKit

class NextLevelScene: SCNScene{

override init(){
    super.init()
    let nextLevel: SCNNode = SCNNode()
    let text: SCNGeometry = SCNText(string: "Next Level", extrusionDepth: 0.0)
    let material = UIColor.blueColor()
    text.firstMaterial!.diffuse.contents = material
    nextLevel.geometry = text

    let exp = SCNParticleSystem()
    exp.loops = true
    exp.birthRate = 5000
    exp.emissionDuration = 0.2
    exp.spreadingAngle = 180
    exp.particleDiesOnCollision = true
    exp.particleLifeSpan = 0.2
    exp.particleLifeSpanVariation = 0.05
    exp.particleVelocity = 20
    exp.particleVelocityVariation = 3
    exp.particleSize = 0.05
    exp.stretchFactor = 0.02
    exp.particleColor = UIColor.redColor()
    exp.emitterShape = text
    //Error here
    exp.birthLocation = SCNParticleBirthLocation.SCNParticleBirthLocationSurface
    self.addParticleSystem(exp, withTransform: SCNMatrix4MakeRotation(0, 0, 0, 0))


    self.rootNode.addChildNode(nextLevel)
}

required init(coder: NSCoder) {
    super.init(coder: coder)
}
}

提前致谢!

【问题讨论】:

    标签: swift enums scenekit


    【解决方案1】:

    如果您在 Xcode 源文件中按住 Command 键单击 birthLocation 以获取 定义属性,然后按住 Command 键单击 SCNParticleBirthLocation 然后你会看到它在 Swift 中被声明为

    enum SCNParticleBirthLocation : Int {
    
        case Surface //particles are emitted on the surface of the emitter shape.
        case Volume //particles are emitted inside the volume of the emitter shape.
        case Vertex //particles are emitted on the vertices of the emitter shape.
    }
    

    所以你只需要

    emitter.birthLocation = SCNParticleBirthLocation.Surface
    

    可以缩短(由于自动类型推断)

    emitter.birthLocation = .Surface
    

    【讨论】:

    • 当我在网页的右上角选择“swift”时,只想为任何阅读常量的 swift 名称仅显示在 scenekit 的文档中的人添加。出于某种原因,选择“两者”仅显示目标 C。这发生在 OP 中链接的页面以及其他一些页面上,但不是全部。
    猜你喜欢
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多