DanijelHuis / HDAugmentedReality

Augmented Reality component for iOS, written in Swift.
MIT License
480 stars 97 forks source link

Annotation Views Glitching when moving camera around #79

Closed alexbartisro closed 6 years ago

alexbartisro commented 6 years ago

https://streamable.com/s/4zusq/dvbgnb

In the attached video you can see that the views glitch when moving the camera around. I would fix this in the framework, it's just that I don't have any idea where to start debugging.

Here's my code for when I create the ARViewController

 @IBAction private func didTapCamera() {
        arViewController.dataSource = self
        arViewController.presenter.maxDistance = 2000
        arViewController.presenter.maxVisibleAnnotations = 30
        arViewController.presenter.distanceOffsetMode = .manual
        arViewController.presenter.distanceOffsetMultiplier = 0.1   // Pixels per meter
        arViewController.presenter.distanceOffsetMinThreshold = 500
        arViewController.presenter.presenterTransform = ARPresenterStackTransform()

        arViewController.trackingManager.userDistanceFilter = 15
        arViewController.trackingManager.reloadDistanceFilter = 150
        arViewController.interfaceOrientationMask = .portrait

        arViewController.setAnnotations(presenter.arAnnotations)
        arViewController.uiOptions.closeButtonEnabled = true

        self.present(arViewController, animated: true, completion: nil)
    }

and for creating the AnnotationView


 private func loadUI() {
        titleLabel?.removeFromSuperview()
        distanceLabel?.removeFromSuperview()

        let label = UILabel()
        label.font = UIFont(name: "AvenirNext-Medium", size: 16.0) ?? UIFont.systemFont(ofSize: 14)
        label.numberOfLines = 2
        label.textColor = UIColor.white
        self.titleLabel = label

        distanceLabel = UILabel()
        distanceLabel.textColor = .cbPPGreen
        distanceLabel.font = UIFont(name: "AvenirNext-Medium", size: 14.0) ?? UIFont.systemFont(ofSize: 12)
        distanceLabel.textAlignment = .center

        self.translatesAutoresizingMaskIntoConstraints = false
        label.translatesAutoresizingMaskIntoConstraints = false
        distanceLabel.translatesAutoresizingMaskIntoConstraints = false

        self.addSubview(label)
        self.addSubview(distanceLabel)

        if self.constraints.isEmpty {
            NSLayoutConstraint.activate([
                NSLayoutConstraint(item: label, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 5),
                NSLayoutConstraint(item: label, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1, constant: -5),
                NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0),
                NSLayoutConstraint(item: label, attribute: .bottom, relatedBy: .equal, toItem: distanceLabel, attribute: .top, multiplier: 1, constant: 0),

                NSLayoutConstraint(item: distanceLabel, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 0),
                NSLayoutConstraint(item: distanceLabel, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1, constant: 0),
                NSLayoutConstraint(item: distanceLabel, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0),
                NSLayoutConstraint(item: distanceLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 15),
                ])
        }

        if let annotation = annotation as? BikeStationAR {
            titleLabel?.text = annotation.address
            distanceLabel?.text = String(format: "%.2f km", annotation.distanceFromUser / 1000)
        }
    }
DanijelHuis commented 6 years ago

Try it with annotation views from demo project, if I remember correctly the width of annotation views is important due to stack calculations etc. Try to set fixed width. If you have sample project with your POIs and everything you can send it so I'll check it out.

I don't recommend using autolayout since it impacts performance.

alexbartisro commented 6 years ago

It was actually a performance issue which I solved by dropping autolayout https://stackoverflow.com/questions/53025175/programmatically-create-uiview-with-size-based-on-subviewslabels