我对 iOS/Swift 开发很陌生,我正在尝试实现一个水平滑入式菜单(用于用户设置)。
我设法让它工作,我面临的问题是,菜单从右到左滑动,但它应该反过来,即从左到右。我尝试过使用正值和负值,但我仍然对这些 CG 属性如何工作(轴 x、y 等)感到有些困惑。
这是我的代码,负责显示菜单:
private func showSettingsMenu(){
if let window = UIApplication.shared.keyWindow {
blackView.backgroundColor = UIColor(white: 0, alpha: 0.7)
blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss)))
window.addSubview(blackView)
window.addSubview(collectionView)
blackView.frame = window.frame
blackView.alpha = 0
let collectionWidth: CGFloat = window.frame.width * 0.75
let x = window.frame.width - collectionWidth
collectionView.frame = CGRect(x: window.frame.width, y: 0, width: collectionWidth, height: window.frame.height)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.blackView.alpha = 1
self.collectionView.frame = CGRect(x: x, y: 0, width: self.collectionView.frame.width, height: self.collectionView.frame.height)
}, completion: nil)
}
}