如何使用弹出窗口行为全屏显示 ViewController? Swift
How to present ViewController in full screen with behavior of popover? Swift
我需要全屏显示 VC,但可以在它弹出时向下滑动它,类似于它在 Apple Music 中的歌曲详细信息。
我已经尝试了 vc.modalPresentationStyle 中的所有选项,但要么全屏无法向下滑动关闭,要么有此功能但不能全屏
vc.modalPresentationStyle = .overFullScreen
vc.modalTransitionStyle = .coverVertical
你可以试试这个:
viewController.modalPresentationStyle = .fullScreen
- 设置
modalPresentationStyle
:
viewController.modalPresentationStyle = .formSheet
- 设置
preferredContentSize
:
viewController.preferredContentSize = CGSize(width: kScreenWidth, height: kScreenHeight)
所以,我只是全屏显示它并编写了函数,它允许平移关闭 vc,同时降低 alpha
func panToCloseGesture() {
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:)))
view.addGestureRecognizer(panGesture)
}
@objc func handlePanGesture(_ sender: UIPanGestureRecognizer) {
let percentThreshold:CGFloat = 0.3
let translation = sender.translation(in: view)
let newY = self.ensureRange(value: view.frame.minY + translation.y, minimum: 0, maximum: view.frame.maxY)
let progress = self.progressAlongAxis(newY, view.bounds.height)
view.frame.origin.y = newY
if newY > 0 {
self.rootView.alpha = 1 - (newY / 400)
}
if sender.state == .ended {
let velocity = sender.velocity(in: view)
if velocity.y >= 1000 || progress > percentThreshold {
self.dismiss(animated: true, completion: nil)
} else {
UIView.animate(withDuration: 0.2, animations: {
self.rootView.alpha = 1
self.view.frame.origin.y = 0
})
}
}
sender.setTranslation(.zero, in: view)
}
我需要全屏显示 VC,但可以在它弹出时向下滑动它,类似于它在 Apple Music 中的歌曲详细信息。
我已经尝试了 vc.modalPresentationStyle 中的所有选项,但要么全屏无法向下滑动关闭,要么有此功能但不能全屏
vc.modalPresentationStyle = .overFullScreen
vc.modalTransitionStyle = .coverVertical
你可以试试这个:
viewController.modalPresentationStyle = .fullScreen
- 设置
modalPresentationStyle
:
viewController.modalPresentationStyle = .formSheet
- 设置
preferredContentSize
:
viewController.preferredContentSize = CGSize(width: kScreenWidth, height: kScreenHeight)
所以,我只是全屏显示它并编写了函数,它允许平移关闭 vc,同时降低 alpha
func panToCloseGesture() {
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:)))
view.addGestureRecognizer(panGesture)
}
@objc func handlePanGesture(_ sender: UIPanGestureRecognizer) {
let percentThreshold:CGFloat = 0.3
let translation = sender.translation(in: view)
let newY = self.ensureRange(value: view.frame.minY + translation.y, minimum: 0, maximum: view.frame.maxY)
let progress = self.progressAlongAxis(newY, view.bounds.height)
view.frame.origin.y = newY
if newY > 0 {
self.rootView.alpha = 1 - (newY / 400)
}
if sender.state == .ended {
let velocity = sender.velocity(in: view)
if velocity.y >= 1000 || progress > percentThreshold {
self.dismiss(animated: true, completion: nil)
} else {
UIView.animate(withDuration: 0.2, animations: {
self.rootView.alpha = 1
self.view.frame.origin.y = 0
})
}
}
sender.setTranslation(.zero, in: view)
}