Перестановка просмотров на viewWillTransition - PullRequest
0 голосов
/ 19 января 2019

У меня есть приложение для iOS, которое имеет 3 вида контейнера (A, B и C).Верхний контейнерный вид (A), который является статичным, и один контейнерный вид снизу (B) с кнопкой, которая «перемещает» его в сторону, чтобы открыть третий вид контейнера (C).Проблема, с которой я сталкиваюсь, заключается в том, что, когда я перехожу с B на C, а затем переворачиваю телефон, боковые стороны B & C перекрываются, они не сохраняют свои позиции, и позиции внезапно перекрываются, если я нажимаю кнопку перемещения, так как они обаперекрытые они оба выходят за пределы экрана.Я добавил 3 скриншота, показывающих поведение:
https://imgur.com/a/wluCkpT

Пока это моя установка для viewDidLoad. Я вызываю resetPositions ():

class HomeVC: UIViewController{
 var isMainContainer: Bool = true

func resetPositions() {
        if isMainContainer {
            if UIApplication.shared.statusBarOrientation.isLandscape {
                containerViewBot.center.y = containedView.center.y
                containerViewBotBack.center.y = containedView.center.y - offset
            } else {
                containerViewBot.center.x = self.view.center.x
                containerViewBotBack.center.x = self.view.center.x - offset
            }
        } else {
            if UIApplication.shared.statusBarOrientation.isLandscape {
                containerViewBotBack.center.y = containedView.center.y
                containerViewBot.center.y = containedView.center.y + offset
            } else {
                containerViewBotBack.center.x = self.view.center.x
                containerViewBot.center.x = self.view.center.x + offset
            }
        }
    }

Теперь«isMainContainer» изменяется, когда я нажимаю кнопку, чтобы «переместиться» из одного представления в другое:

func getMore(from: String) {
        //from is the sender
        if from == "homeResults" {
            containerViewBotBack.isHidden = false
            goToCommute()
        } else {
            //containerViewBotBack.isHidden = true
            containerViewBot.isHidden = false
            goToResults()
        }
    }

Наконец методы goToCommute и goToResults:

func goToCommute() {
    let duration = 0.5
    if UIApplication.shared.statusBarOrientation.isLandscape {
        UIView.animate(withDuration: duration, animations: {
            self.moveUp(view: self.containerViewBot)
            self.moveUp(view: self.containerViewBotBack)
        }) { (_) in
            self.containerViewBotBack.isHidden = false
            self.containerViewBot.isHidden = true
        }
    } else {
        UIView.animate(withDuration: duration, animations: {
            self.moveRight(view: self.containerViewBot)
            self.moveRight(view: self.containerViewBotBack)
        }) { (_) in
            self.containerViewBotBack.isHidden = false
            self.containerViewBot.isHidden = true
        }
    }
    isMainContainer = false
}

func goToResults() {
    let duration = 0.5
    if UIApplication.shared.statusBarOrientation.isLandscape {
        UIView.animate(withDuration: duration, animations: {
            self.moveDown(view: self.containerViewBot)
            self.moveDown(view: self.containerViewBotBack)
        }) { (_) in
            self.containerViewBot.isHidden = false
            self.containerViewBotBack.isHidden = true
        }
    } else {
        UIView.animate(withDuration: duration, animations: {
            self.moveLeft(view: self.containerViewBot)
            self.moveLeft(view: self.containerViewBotBack)
        }) { (_) in
            self.containerViewBot.isHidden = false
            self.containerViewBotBack.isHidden = true
        }
    }
    isMainContainer = true
}

Я вызываю «resetPositions»() "команда переопределения viewWillTransition (...)

1 Ответ

0 голосов
/ 19 января 2019

Вы можете обнаружить вращение устройства указанным ниже способом и изменить положение ваших предметов.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    NotificationCenter.default.addObserver(self, selector: #selector(deviceRotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}

override func viewWillDisappear(_ animated: Bool) {
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}

func deviceRotated(){
    if UIDeviceOrientationIsLandscape(UIDevice.current.orientation) {
        print("Landscape")
        // Resize other things
    }
    if UIDeviceOrientationIsPortrait(UIDevice.current.orientation) {
        print("Portrait")
        // Resize other things
    }
}
...