我正在开发一项功能,该功能将从UIView
屏幕底部向上动画,以告诉用户他们的数据已保存。但是,它在 iPhone 11 Pro Max 上的显示位置与在 iPhone X 上不同。
iPhone 11 Pro Max(这就是我想要的样子):
但在 iPhone X 上:
我可能应该注意到我在故事板上创建了视图并@IBOutlet
连接到它。在故事板上,它位于您在顶部屏幕截图中看到的位置。我没有对其施加任何限制。其余的在代码中完成。
动画代码非常简单:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
savedView.center.y += view.bounds.height // start with it off the screen
savedView.center.x = (view.bounds.width / 2)
}
override func viewDidAppear(_ animated: Bool) {
super.viewWillAppear(true)
// animate it onto the screen
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.0, options: [], animations: {
self.savedView.center.y -= self.view.bounds.height <-- the problem must be here
}, completion: nil)
}
我认为这可能是由于不同的设备尺寸类别,但所有 iPhone 都是“常规”高度,所以我不认为是这样。
我怎样才能让这个视图在所有设备上都处于相同的位置(相对于屏幕底部)?
我能够弄清楚。
问题是视图的初始center.y
位置基于它在故事板中的位置从 814 开始。所以它在我作为 iPhone 11 Pro 观看的情节提要上看起来不错,但是,当在具有不同尺寸的其他设备模拟器上运行时,数学不再起作用。
解决方案是center.y
根据view.bounds.height
在任何设备上为其提供一致的起始位置来设置位置:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
savedView.center.y = (view.bounds.height - 60) // position the view 60 pixels bottom
savedView.center.x = (view.bounds.width / 2) // center it horizontally
savedView.center.y += view.bounds.height // move it outside the visible bounds
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句