代码之家  ›  专栏  ›  技术社区  ›  Jacobo

Swift 4,设备定位

  •  0
  • Jacobo  · 技术社区  · 6 年前

    我正在做一个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()
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Josh Homann    6 年前

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