ios - 如何更改 Sprite 套件中 Sprite 节点的大小以适应不同的屏幕(Swift)

标签 ios swift sprite-kit

我正在创建一个游戏,我快要完成了,但是我遇到的一个烦人的问题是我的游戏不适合 iPhone 5 屏幕。我使用了大小类,将比例设置为宽高比填充,甚至尝试调整填充大小,但它不适合 iPhone 5。它适合所有其他屏幕尺寸(除了 iPhone 4s,因为我不支持它)包括iPad。除了尺寸类别之外还有其他选择吗?我必须以编程方式更改图像吗?如果是这样,我该如何在 Sprite 套件中做到这一点?

最佳答案

您可以做到这一点的一种方法是检测用户正在使用的设备,然后更改变量以满足该设备。

这是一个使用 UIDevice() 的扩展,您可以检测用户使用的是什么设备:

public extension UIDevice {

var modelName: String {
    var systemInfo = utsname()
    uname(&systemInfo)
    let machineMirror = Mirror(reflecting: systemInfo.machine)
    let identifier = machineMirror.children.reduce("") { identifier, element in
        guard let value = element.value as? Int8 where value != 0 else { return identifier }
        return identifier + String(UnicodeScalar(UInt8(value)))
    }

    switch identifier {
    case "iPod5,1":                                 return "iPod Touch 5"
    case "iPod7,1":                                 return "iPod Touch 6"
    case "iPhone3,1", "iPhone3,2", "iPhone3,3":     return "iPhone 4"
    case "iPhone4,1":                               return "iPhone 4s"
    case "iPhone5,1", "iPhone5,2":                  return "iPhone 5"
    case "iPhone5,3", "iPhone5,4":                  return "iPhone 5c"
    case "iPhone6,1", "iPhone6,2":                  return "iPhone 5s"
    case "iPhone7,2":                               return "iPhone 6"
    case "iPhone7,1":                               return "iPhone 6 Plus"
    case "iPhone8,1":                               return "iPhone 6s"
    case "iPhone8,2":                               return "iPhone 6s Plus"
    case "iPhone8,4":                               return "iPhone SE"
    case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":return "iPad 2"
    case "iPad3,1", "iPad3,2", "iPad3,3":           return "iPad 3"
    case "iPad3,4", "iPad3,5", "iPad3,6":           return "iPad 4"
    case "iPad4,1", "iPad4,2", "iPad4,3":           return "iPad Air"
    case "iPad5,3", "iPad5,4":                      return "iPad Air 2"
    case "iPad2,5", "iPad2,6", "iPad2,7":           return "iPad Mini"
    case "iPad4,4", "iPad4,5", "iPad4,6":           return "iPad Mini 2"
    case "iPad4,7", "iPad4,8", "iPad4,9":           return "iPad Mini 3"
    case "iPad5,1", "iPad5,2":                      return "iPad Mini 4"
    case "iPad6,3", "iPad6,4", "iPad6,7", "iPad6,8":return "iPad Pro"
    case "AppleTV5,3":                              return "Apple TV"
    case "i386", "x86_64":                          return "Simulator"
    default:                                        return identifier
    }
}

然后你就可以这样使用了

if UIDevice.modelName == "iPhone 4" {
    theNode.xScale = whatever
    theNode.yScale = whatever
}

关于ios - 如何更改 Sprite 套件中 Sprite 节点的大小以适应不同的屏幕(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38440479/

相关文章:

ios - 我如何通过 Foundation 获取星期几?

ios - CALayer 的实心边框

ios - 如何存储 128 位值?

swift - 将长月份名称转换为 int

swift - 实现异步线程(使用 Swift 的 Xcode)

ios - 存档期间出错 : symbols referenced by indirect symbol table entries that can't be stripped

ios - 模拟零重力风格的球员运动

swift - 绘制带边框的多段线 Mapbox, iOS

ios - 使用系统字体创建 SKLabelNode(UIFont 到字体名称)

ios - iPhone 6+ 上的 SpriteKit 性能不佳