如何在swift中添加位置图标?

时间:2017-05-26 13:19:52

标签: ios swift uibutton mkmapview

如何在项目中添加此内容。它们是否已整合到MKMapView

我没有在互联网上找到任何关于他们的内容。

Screenshot

2 个答案:

答案 0 :(得分:0)

地图视图顶部的子视图。我是一个iOS资源。 UIButton - >类型 - >详细信息披露

答案 1 :(得分:0)

  1. MKMapView顶部创建两个按钮添加其约束,然后在viewDidLayourSubviews函数中编写此代码以围绕所需的角并将阴影和图像添加到按钮。从身份检查器中选择浅灰色背景颜色。

    override func viewDidLayoutSubviews() {
    
    let infoButtonShape = CAShapeLayer()
    infoButtonShape.bounds = infoButton.frame
    infoButtonShape.position = infoButton.center
    
    let locationButtonShape = CAShapeLayer()
    locationButtonShape.bounds = locationButton.frame
    locationButtonShape.position = locationButton.center
    
    
    infoButton.clipsToBounds = true
    infoButton.layer.masksToBounds = false
    infoButton.layer.shadowColor = UIColor.white.cgColor
    infoButton.layer.shadowRadius = 2
    infoButton.layer.shadowOpacity = 0.5
    infoButton.layer.shadowOffset = CGSize(width: 2, height: -2)
    infoButton.layer.shadowPath = UIBezierPath(rect: infoButton.bounds).cgPath
    
    
    
    locationButton.clipsToBounds = true
    locationButton.layer.masksToBounds = false
    locationButton.layer.shadowColor = UIColor.white.cgColor
    locationButton.layer.shadowRadius = 2
    locationButton.layer.shadowOpacity = 0.5
    locationButton.layer.shadowOffset = CGSize(width: -2, height: 2)
    locationButton.layer.shadowPath = UIBezierPath(rect: locationButton.bounds).cgPath
    
    infoButton.layer.shouldRasterize = true
    locationButton.layer.shouldRasterize = true
    
    locationButtonShape.path = UIBezierPath(roundedRect: locationButton.bounds, byRoundingCorners: [UIRectCorner.bottomLeft , UIRectCorner.bottomRight], cornerRadii: CGSize(width:10.0, height:10.0)).cgPath
    
    infoButtonShape.path = UIBezierPath(roundedRect: infoButton.bounds, byRoundingCorners: [UIRectCorner.topRight , UIRectCorner.topLeft], cornerRadii: CGSize(width:10.0, height:10.0)).cgPath
    
    infoButton.layer.mask = infoButtonShape
    locationButton.layer.mask = locationButtonShape
    
    infoButton.contentMode = .scaleAspectFit
    locationButton.contentMode = .scaleAspectFit
    
    
    infoButton.setImage(#imageLiteral(resourceName: "information-icon-3"), for: .normal)
    locationButton.setImage(#imageLiteral(resourceName: "LocationArrow-512"), for: .normal)
    
    
    }
    
  2. 然后添加按钮的IBAction以更新位置

     let manager = CLLocationManager()
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    
    //Use location.last add to map then stop updating location
    manager.stopUpdatingLocation()
    
    }
    
    @IBAction func updateLocationAction(_ sender: UIButton) {
    
    manager.startUpdatingLocation()
    }
    
  3. 对于另外两个UIViews,只需为温度创建一个UILabel,为云图标创建一个UIImageView。对于UILabel和UIImageView的背景颜色及其角半径,您可以执行类似于我为按钮所做的操作,但这次选择UIImageView的右角和UILabel的左角。

  4. 以下是输出:当然,您可以为按钮使用尺寸更大的图标图像。

    enter image description here