小编典典

Swift-如何检测方向变化

swift

我想将两个图像添加到单个图像视图(即,用于横向显示一个图像,用于纵向显示另一个图像),但是我不知道如何使用快速语言检测方向变化。

我尝试了这个答案,但只拍了一张图片

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.currentDevice().orientation.isLandscape.boolValue {
        print("Landscape")
    } else {
        print("Portrait")
    }
}

我是iOS开发的新手,任何建议将不胜感激!


阅读 312

收藏
2020-07-07

共1个答案

小编典典

let const = "Background" //image name
let const2 = "GreyBackground" // image name
    @IBOutlet weak var imageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()

        imageView.image = UIImage(named: const)
        // Do any additional setup after loading the view.
    }

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        if UIDevice.current.orientation.isLandscape {
            print("Landscape")
            imageView.image = UIImage(named: const2)
        } else {
            print("Portrait")
            imageView.image = UIImage(named: const)
        }
    }
2020-07-07