我正在为iOS、OSX和tvOS制作一个SpriteKit游戏。我正在尝试使用加速度计作为我的iOS目标。我在导入CMMotionManager时检查了iOS,但是在创建我的motion manager属性时,似乎无法使该检查正常工作。

#if os(iOS)
    import CMMotionManager
#endif

class MainPlayScene: TCScene, SKPhysicsContactDelegate {

    //MARK: Properties
    //Motion
    @available(iOS 9, *) // Does not work, just trying things out....
    var motionManager:CMMotionManager {
        return motionManager
    }

我怎样才能结账?
编辑:现在很晚了,我想得越多,如果我走错了方向,就纠正我。我如何才能使用加速度计只为iOS,但仍然分享我的场景代码?

最佳答案

您使用与import语句相同的语法。这也是苹果在他们的示例游戏DemoBots中所做的。

#if os(iOS)
var motionManager....
#endif

#if os(tvOS)
...
#endif

如果
 #if os(iOS) || os(tvOS)
 ....
 #elseif os(OSX)
 ...
 #endif
 ... // Code for other platforms
 #endif

How to determine device type from Swift? (OS X or iOS)
只是好奇,您计算motionManager属性的特殊原因是什么?

10-07 19:13
查看更多