资产目录中的景观与肖像背景图像

时间:2014-09-24 20:43:01

标签: ios iphone xcode ipad asset-catalog

我有一个用例似乎陷入资产目录和大小类之间的裂缝。我的通用应用程序需要一个横向和纵向方向不同的全屏幕背景图像,而且仅支持iPad和iPad的横向图像。 iPhone 6。

由于我无法将景观图像添加到资产目录中以获取新的图像集,因此我当前的解决方案如下所示(支持iOS 7和8):

// permit landscape mode only for iPads & iPhone 6 (would prefer to use size classes, but...?)
override func shouldAutorotate() -> Bool {
    let size = view.frame.size
    let maxv = max(size.width, size.height)
    return ((maxv > 700.0) || (maxv == 512.0)) ? true : false
}

// this will be triggered only for iOS 7, as long as viewWillTransitionToSize:withTransitionCoordinator: is also implemented!
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
    adjustToOrientation(toInterfaceOrientation)
}

// this will be triggered only for iOS 8
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

    let orientation = UIApplication.sharedApplication().statusBarOrientation

    // need to reverse the sense of the orientation here; Application still has the previous orientation
    switch orientation {
        case .Portrait, .PortraitUpsideDown:
            adjustToOrientation(.LandscapeLeft)
        case .LandscapeLeft, .LandscapeRight:
            adjustToOrientation(.Portrait)
        default:
            adjustToOrientation(.Portrait)
    }

}

func adjustToOrientation(newInterfaceOrientation: UIInterfaceOrientation) {

    let size = view.frame.size

    // rotation already permitted only for iPad & iPhone 6, but need to know which one (size classes useless here?)
    switch (max(size.width, size.height)) {
        case 1024.0:
            if UIInterfaceOrientationIsLandscape(newInterfaceOrientation) {
                backgroundImage.image = UIImage(named: "Background-Landscape@2x~ipad.png")
            } else {
                backgroundImage.image = UIImage(named: "Background@2x~ipad.png")
            }
        case 736.0:
            if UIInterfaceOrientationIsLandscape(newInterfaceOrientation) {
                backgroundImage.image = UIImage(named: "Background-Landscape@3x~iphone.png")
            } else {
                backgroundImage.image = UIImage(named: "Background@3x~iphone.png")
            }
        case 512.0:
            if UIInterfaceOrientationIsLandscape(newInterfaceOrientation) {
                backgroundImage.image = UIImage(named: "Background-Landscape~ipad.png")
            } else {
                backgroundImage.image = UIImage(named: "Background~ipad.png")
            }
        default:
            break
    }

}

这有效,但似乎很脆弱。在实际处于横向模式之前,是否有更正确的方法将设备识别为支持横向中常规尺寸类的设备?或者某种方式我错过了为图像集指定景观等效物?

2 个答案:

答案 0 :(得分:0)

您可以在资产目录中使用大小类,也可以使用两个图像集:一个用于纵向,一个用于LANDpace模式。然后,您可以按资产名称访问图像。这会减少你的代码。但我建议尽可能在任何地方使用大小等级的cosider。

答案 1 :(得分:0)

所以最终的解决方案是:

- 将图像资源上的Width属性设置为Any + Regular,并为iPhone 6+和iPad添加几个Landscape图像,UIImage自动完成其余的

- 一种更优雅的方式,只允许在iOS 7和iOS 7中使用这两种设备进行旋转。 iOS 8:

override func shouldAutorotate() -> Bool {
    let scale = UIScreen.mainScreen().scale
    let idiom = UIDevice.currentDevice().userInterfaceIdiom
    return ((idiom == .Pad) || (scale > 2.0)) ? true : false
}

所以上面所有的代码都是5行代码!