Hi,
ich habe folgendes Problem. Ich habe gerade meiner erstes AR feature gestaltet. So weit so gut. Die jeweilige Information wird in 2 Labeln ausgegeben (das obere Label für den Titel, das untere für die Distanz). Es sieht aus wie eine Einheit, so soll es auch sein. Wenn ich jetzt versuche, dem Frame abgerundete Ecken zuzuweisen, ist nur die rechte Seite abgerundet.
Mein 2. Problem ist, dass ich das Label, aber eigentlich den ganzen Frame dynamisch an die Länge des Textes anpassen möchte.
Vielen Dank für jeden Tipp.
Das ist die Klasse AnnotationView.
Alles anzeigen
Das ist der Teil des ViewControllers:
Alles anzeigen
Hier ist der Screenshot, wie es zur Zeit aussieht.
ich habe folgendes Problem. Ich habe gerade meiner erstes AR feature gestaltet. So weit so gut. Die jeweilige Information wird in 2 Labeln ausgegeben (das obere Label für den Titel, das untere für die Distanz). Es sieht aus wie eine Einheit, so soll es auch sein. Wenn ich jetzt versuche, dem Frame abgerundete Ecken zuzuweisen, ist nur die rechte Seite abgerundet.
Mein 2. Problem ist, dass ich das Label, aber eigentlich den ganzen Frame dynamisch an die Länge des Textes anpassen möchte.
Vielen Dank für jeden Tipp.
Das ist die Klasse AnnotationView.
Quellcode
- import UIKit
- //1
- protocol AnnotationViewDelegate {
- func didTouch(annotationView: AnnotationView)
- }
- //2
- class AnnotationView: ARAnnotationView {
- //3
- var titleLabel: UILabel?
- var distanceLabel: UILabel?
- var delegate: AnnotationViewDelegate?
- override func didMoveToSuperview() {
- super.didMoveToSuperview()
- loadUI()
- }
- //4
- func loadUI() {
- titleLabel?.removeFromSuperview()
- distanceLabel?.removeFromSuperview()
- let label = UILabel(frame: CGRect(x: 10, y: 0, width: self.frame.size.width, height: 30))
- label.font = UIFont.systemFont(ofSize: 16)
- label.numberOfLines = 0
- label.backgroundColor = UIColor(white: 0.3, alpha: 0.7)
- label.textColor = UIColor.white
- //label.layer.cornerRadius = 10
- // label.clipsToBounds = true
- self.addSubview(label)
- self.titleLabel = label
- distanceLabel = UILabel(frame: CGRect(x: 10, y: 30, width: self.frame.size.width, height: 20))
- distanceLabel?.backgroundColor = UIColor(white: 0.3, alpha: 0.7)
- distanceLabel?.textColor = UIColor.white
- distanceLabel?.font = UIFont.systemFont(ofSize: 12)
- //distanceLabel?.layer.cornerRadius = 10
- //distanceLabel?.clipsToBounds = true
- self.addSubview(distanceLabel!)
- if let annotation = annotation as? Place {
- titleLabel?.text = annotation.placeName
- distanceLabel?.text = String(format: "%.2f km", annotation.distanceFromUser / 1000)
- }
- }
- //1
- override func layoutSubviews() {
- super.layoutSubviews()
- titleLabel?.frame = CGRect(x: 10, y: 0, width: self.frame.size.width, height: 30)
- distanceLabel?.frame = CGRect(x: 10, y: 30, width: self.frame.size.width, height: 20)
- }
- //2
- override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
- delegate?.didTouch(annotationView: self)
- }
- }
Das ist der Teil des ViewControllers:
Quellcode
- extension ViewController: ARDataSource {
- func ar(_ arViewController: ARViewController, viewForAnnotation: ARAnnotation) -> ARAnnotationView {
- let annotationView = AnnotationView()
- annotationView.annotation = viewForAnnotation
- annotationView.delegate = self
- annotationView.frame = CGRect(x: 0, y: 0, width:200, height: 50)
- annotationView.layer.cornerRadius = 12
- annotationView.clipsToBounds = true
- return annotationView
- }
- }