我正在做一个ArKit+CoreLocation应用程序,当我触摸一个节点时,我会在屏幕上的视图上显示一些信息。问题是,当我旋转设备时,我重新绘制了DataView,但没有正确绘制。
我注意到的第一件事是,当我启动应用程序时,手机处于纵向模式,我必须进行评估
if(!UIDevice.current.orientation.isPortrait )
为了得到合适的尺寸。
为什么在发射过程中方向不是纵向的?
而且,只有在应用程序启动时,维度才能得到很好的管理。如果我在横向模式下运行应用程序,视图将正确渲染,纵向也是如此。我发现的另一个问题是,当我“重画”视图时,如果我想更新视图中的文本,那就不可能了。
我制作了一个视频来向你们展示正在发生的事情,也为了知道当一个节点被选中时,它周围有一个紫色的圆圈。你会看到,在重新定向之后,无论我触摸哪个节点,我都无法更新文本。
https://youtu.be/ls_Y6dnW8E4
我的源代码是
/*
* Initialize the TextView to display the information about the monument
*/
func initDataView() {
let screenWidth = self.view.frame.width
let screenHeight = self.view.frame.height
var leftPadding : CGFloat = 0.0
var bottomPadding : CGFloat = 0.0
var frameHeight : CGFloat = 0.0
/*
* Configure paddings if the device is a phone
*/
if (UIDevice.current.userInterfaceIdiom == .phone) {
if ( UIDevice.current.orientation.isPortrait ) {
(leftPadding, bottomPadding, frameHeight) = getDataViewPaddings(phone: true, portrait: true, screenWidth: screenWidth, screenHeight: screenHeight)
} else {
(leftPadding, bottomPadding, frameHeight) = getDataViewPaddings(phone: true, portrait: false, screenWidth: screenWidth, screenHeight: screenHeight)
}
}
dataView = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth-(leftPadding*2), height: screenHeight/frameHeight))
dataView.frame.origin.x = leftPadding
dataView.frame.origin.y = screenHeight - dataView.frame.height - bottomPadding
//dataView.backgroundColor = UIColor.white.withAlphaComponent(0.7)
//Adjust border and border color
dataView.layer.borderWidth = 4.0
dataView.layer.borderColor = UIColor(hexString: "#951789ff")?.cgColor
initTextView(leftPadding: leftPadding)
dataView.addSubview(textView)
dataView.addSubview(descriptionView)
dataView.addSubview(iconView)
}
public override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
initDataView()
}