我有一个自定义 View Controller ,可以通过一个按钮全屏显示。它通常是 View 的 subview (嵌入)。我从嵌入进入全屏是这样的:
private func enterFullScreenFromEmbed() {
self.proxyView = UIView(frame: self.view.frame)
self.proxyView?.isHidden = true
self.proxyView?.autoresizingMask = self.view.autoresizingMask
self.view.superview?.addSubview(self.proxyView!)
// Now set the frame to the screen frame
let frame = self.view.window?.convert(self.view.frame, from: self.proxyView?.superview)
self.view.window?.addSubview(self.view)
self.view.frame = frame!
self.isFullscreen = true
UIView.animate(withDuration: 0.25) {
self.view.frame = self.view.window!.bounds
self.view.layoutIfNeeded()
self.setNeedsStatusBarAppearanceUpdate()
}
}
并从全屏退出:
private func exitFullScreenToEmbed() {
let frame = self.view.window?.convert(self.view.frame, to: self.proxyView?.superview)
self.proxyView?.superview?.addSubview(self.view)
self.view.frame = frame!
self.isFullscreen = false
UIView.animate(withDuration: 0.25, animations: {
self.view.frame = self.proxyView!.frame
self.view.layoutIfNeeded()
self.setNeedsStatusBarAppearanceUpdate()
}) { (_) in
self.proxyView?.removeFromSuperview()
self.proxyView = nil
}
}
这很好,除了我在进入全屏动画中隐藏状态栏,并在退出全屏动画中显示它。这会导致我的顶 View 跳回原位,而没有动画。
注意,isFullscreen
变量隐藏了状态栏。
override var prefersStatusBarHidden: Bool {
return isFullscreen
}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}
这里是动画的 GIF:
请注意风景的顶部和底部(在风景上更容易看到)。在顶部,玩家框架会立即设置为旧位置,这会将所有内容向下移动 20 像素(或状态栏的高度)
这与隐藏状态栏有关。有人有解决办法吗?
我想通了。这是一种 hacky 并且没有经过全面测试,但这是我想出的解决方案。
首先,获取一个变量来保存状态栏高度(不是0)。
var statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.height != 0 ? UIApplication.shared.statusBarFrame.height : 20.0
然后,设置状态栏框架更改的通知。
NotificationCenter.default.addObserver(self, selector: #selector(SKPlayerViewController.updateLocalStatusBarFrameHeight), name: .UIApplicationDidChangeStatusBarFrame, object: nil)
现在,在方法中,仅当高度不为 0 时才更新变量。我们保存高度是因为状态栏可能不是 20(如果正在调用等)。
@objc private func updateLocalStatusBarFrameHeight() {
let height = UIApplication.shared.statusBarFrame.height
if height > 0 {
self.statusBarHeight = height
}
}
现在,我更新了我的 exitFullScreenToEmbed()
,仅当不是横向时才通过 statusBarHeight
减去计算的帧原点(因为状态栏隐藏在横向中)。
private func exitFullScreenToEmbed() {
var frame = self.view.window?.convert(self.view.frame, to: self.proxyView?.superview)
self.proxyView?.superview?.addSubview(self.view)
if !(UIApplication.shared.statusBarOrientation == .landscapeRight || UIApplication.shared.statusBarOrientation == .landscapeLeft) {
frame?.origin.y -= self.statusBarHeight
}
self.view.frame = frame!
self.isFullscreen = false
UIView.animate(withDuration: 0.25, animations: {
self.view.frame = self.proxyView!.frame
self.view.layoutIfNeeded()
self.setNeedsStatusBarAppearanceUpdate()
}) { (_) in
self.proxyView?.removeFromSuperview()
self.proxyView = nil
}
}
如果这对任何人有帮助,不客气。我只是在这里分享一些知识
关于ios - 断断续续的全屏退出过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46086670/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) | Powered by Discuz! X3.4 |