仅在iOS上声明属性

时间:2016-06-01 04:38:21

标签: ios swift macos sprite-kit cmmotionmanager

我正在为iOS,OSX和tvOS制作SpriteKit游戏。我正在尝试将加速度计用于我的iOS目标。我在导入CMMotionManager时检查了iOS,但在创建运动管理器属性时似乎无法使检查工作。

#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上使用加速度计并仍然共享我的场景代码?

2 个答案:

答案 0 :(得分:2)

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

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

#if os(tvOS)
...
#endif

您也可以进行多项检查,如果

,则使用else / else
 #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属性吗?

答案 1 :(得分:0)

您可以使用类似

的内容
#if TARGET_OS_IPHONE
    import CMMotionManager