如何在 swift 中添加位置图标?
How can i add location icon in swift?
如何在我的项目中添加这些东西。它们是否集成在 MKMapView
中?
我在互联网上没有找到关于它们的任何信息。
地图视图顶部的子视图。 i 是一个 iOS 资源。 UIButton -> 类型 -> 详细信息披露
在 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)
}
然后添加更新位置的按钮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()
}
对于另外两个 UIView,只需为温度创建一个 UILabel,为云图标创建一个 UIImageView。对于 UILabel 和 UIImageView 的背景颜色及其圆角半径,您可以做一些与我为按钮所做的类似的事情,但这次选择 UIImageView 的右角和 UILabel 的左角。
输出如下:当然,您可以为按钮使用尺寸更大的图标图像。
如何在我的项目中添加这些东西。它们是否集成在 MKMapView
中?
我在互联网上没有找到关于它们的任何信息。
地图视图顶部的子视图。 i 是一个 iOS 资源。 UIButton -> 类型 -> 详细信息披露
在
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) }
然后添加更新位置的按钮
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() }
对于另外两个 UIView,只需为温度创建一个 UILabel,为云图标创建一个 UIImageView。对于 UILabel 和 UIImageView 的背景颜色及其圆角半径,您可以做一些与我为按钮所做的类似的事情,但这次选择 UIImageView 的右角和 UILabel 的左角。
输出如下:当然,您可以为按钮使用尺寸更大的图标图像。