如何在Swift中的AR注释视图中生成图标?

时间:2018-01-26 16:48:26

标签: ios swift annotations

我正在尝试在注释AR视图中显示不同的图标。所以,我需要一些帮助来生成我的资产文件夹中的图标。让我们说如果我的注释气泡视图显示一个餐厅的地方,那么我想在注释视图上显示我的餐厅图标。任何提示/建议,以帮助我解决这个问题。谢谢!

import UIKit
import AVFoundation

protocol AnnotationViewDelegate {
  func didTouch(annotationView: AnnotationView)
}


class AnnotationView: ARAnnotationView {


  var titleLabel: UILabel?
  var distanceLabel: UILabel?
  var delegate: AnnotationViewDelegate?

  var backgroundView: UIView?

  var pinImage: UIImageView?

    let pinoImage = ["bakeries", "banks", "barber", "bars", "beaches", "breweries", "cardealer", "carrepair", "church", "cinema",
                    "coffee", "college", "dentist", "dining", "doctors", "drycleaning", "fastfood", "firetruck", "fitness", "gas",
                    "grocery", "hospital", "hotel", "library", "lounges", "motorcycledealers", "musicvenues", "park", "petstore",
                    "pharmacy", "police", "postoffice", "train", "transportation", "zoo"]



  override func didMoveToSuperview() {
    super.didMoveToSuperview()

    loadUI()
  }



  func getRandomColor() -> UIColor{

    let red:CGFloat = CGFloat(drand48())
    let green:CGFloat = CGFloat(drand48())
    let blue:CGFloat = CGFloat(drand48())

    return UIColor(red:red, green: green, blue: blue, alpha: 1.0)
  }

  func loadUI() {
    titleLabel?.removeFromSuperview()
    distanceLabel?.removeFromSuperview()
    backgroundView?.removeFromSuperview()
    pinImage?.removeFromSuperview()

    backgroundView = UIView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: 70))
    backgroundView?.backgroundColor = getRandomColor()
      //UIColor(white: 1, alpha: 0.80)
    backgroundView?.layer.cornerRadius = 10.0
    self.addSubview(backgroundView!)

    pinImage = UIImageView(frame: CGRect(x: 16, y: 8, width: 37.76, height: 54))


    pinImage?.contentMode = UIViewContentMode.scaleAspectFit
    self.backgroundView?.addSubview(pinImage!)

    let label = UILabel(frame: CGRect(x: 40, y: 8, width: self.frame.size.width, height: 22.0))
    label.font = UIFont(name: "Montserrat-Regular", size: 8)
    label.numberOfLines = 1
    label.textColor = UIColor.black
    self.backgroundView?.addSubview(label)
    self.titleLabel = label

    distanceLabel = UILabel(frame: CGRect(x: 66, y: 47, width: self.frame.size.width, height: 15.0))
    distanceLabel?.textColor = UIColor.black
    distanceLabel?.font = UIFont(name: "Montserrat-Regular", size: 12)
    self.backgroundView?.addSubview(distanceLabel!)

      if let annotation = annotation as? Place {


      titleLabel?.text = annotation.placeName
      distanceLabel?.text = String(format: "%.2f mi", annotation.distanceFromUser * 0.000621371)

       pinImage?.image = UIImage(named: "dining2")



    }

  }

  override func layoutSubviews() {
    super.layoutSubviews()




    backgroundView?.frame = CGRect(x: 0, y: 0, width: 170, height: 80)

  titleLabel?.frame = CGRect(x: 50, y: 8, width: self.frame.size.width - 66, height: 22.0)

    distanceLabel?.frame = CGRect(x: 50, y: 30, width: self.frame.size.width, height: 20)
    pinImage = UIImageView(frame: CGRect(x: 16, y: 8, width: 37.76, height: 54))



  }


  override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    delegate?.didTouch(annotationView: self)

  }




}
  

放置(这是我的对象)

import CoreLocation
import UIKit

class Place: ARAnnotation {
  var reference: String
  var placeName: String
  var address: String?
  var iconUrl: String?
  var iconImage: UIImage?
  var phoneNumber: String?
  var website: String?

  var pinoImage: String?

    var place: Place!

   var coordinate :CLLocationCoordinate2D!


  var photoReferences: [String]?



 var infoText: String {
    get {
      var info = "Address: \(address)"

      if phoneNumber != nil {
        info += "\nPhone: \(phoneNumber!)"
      }

      if website != nil {
        info += "\nweb: \(website!)"
      }
      return info
    }
  }



  init(location: CLLocation, reference: String, name: String, address: String, phoneNumber: String? = nil,
       website: String? = nil, pinImage: String? = nil) {
    self.placeName = name
    self.reference = reference
    self.address = address
    self.phoneNumber = phoneNumber
    self.website = website
    self.pinoImage = pinImage 


    super.init()

    self.location = location
  }



  override var description: String {
    return placeName
  }
}

0 个答案:

没有答案