在viewWillTransition上重新定位视图

时间:2019-01-19 05:35:58

标签: ios swift uicontainerview

我有一个iOS应用,该应用具有3个容器视图(A,B和C)。顶部容器视图(A)是静态的,一个容器视图(B)在下方(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
}

我在覆盖viewWillTransition(...)上调用“ resetPositions()”命令

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式检测设备旋转并重新放置物品。

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
    }
}