Swift 4,设备方向

时间:2018-12-05 01:03:50

标签: ios swift core-location arkit

我正在做一个ArKit + CoreLocation应用程序,当我触摸节点时,我会在屏幕上的View上显示一些信息。问题是,当我旋转设备时,我重新绘制了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()
}

1 个答案:

答案 0 :(得分:0)

将UIKit UIViews与ARkit ARSCNView一起使用与其他任何视图都没有不同。如果要设置自定义框架,则必须在viewDidLayoutSubviews中完成代码。在这种情况下,尽管看起来您只想将视图固定在距前端和前端一定距离的位置,并保持恒定的宽度和高度。如果这是您想要的,则只需使用自动布局,然后让布局引擎计算位置即可。这适用于代码或界面生成器。您也可以在界面生成器中手动将UIView的类设置为ARSCNView。