gpt4 book ai didi

ios - 在 viewWillTransition 上重新定位 View

转载 作者:行者123 更新时间:2023-11-30 10:53:02 31 4
gpt4 key购买 nike

我有一个 iOS 应用程序,有 3 个容器 View (A、B 和 C)。顶部容器 View (A) 是静态的,下面有一个容器 View (B),带有一个按钮,可将其“移动”到一侧以显示第三个容器 View (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
}
}
}

现在,当我按下按钮从一个 View “移动”到另一个 View 时,“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()”命令

最佳答案

您可以通过以下方式检测设备旋转并重新定位您的项目。

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

关于ios - 在 viewWillTransition 上重新定位 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54264367/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com