Swift - 向きの変化を検出する方法 質問する

Swift - 向きの変化を検出する方法 質問する

単一の画像ビューに 2 つの画像を追加したいのですが (つまり、横向き用に 1 つの画像、縦向き用に別の画像)、Swift 言語を使用して方向の変更を検出する方法がわかりません。

この回答を試してみましたが、画像は1枚しか必要ありません

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

私は iOS 開発の初心者なので、アドバイスをいただければ幸いです。

ベストアンサー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)
        }
    }

おすすめ記事