gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:26:04 24 4
gpt4 key购买 nike

如何将这些东西添加到我的项目中。它们是否集成在 MKMapView 中?

我在互联网上没有找到任何有关他们的信息。

Screenshot

最佳答案

  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. 对于另外两个 UIView,只需创建一个用于温度的 UILabel 和一个用于云图标的 UIImageView。对于 UILabel 和 UIImageView 的背景颜色及其角半径,您可以执行与我为按钮所做的类似的操作,但这次为 UIImageView 选择右角,为 UILabel 选择左角。

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

enter image description here

关于ios - 如何在 swift 中添加位置图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44202690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com