如何在swift中添加位置图标?
答
-
上创建
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() }
对于两个其它UIViews只是创建一个UILabel用于温度以及用于云图标一个的UIImageView。对于UILabel和UIImageView的背景颜色及其圆角半径,您可以做类似于我为按钮所做的操作,但是这次为UIImageView和UILabel的左角选择了正确的角点。
这里是输出:当然,您可以使用更好的大小的图标图像为您的按钮。
+0
很棒,谢谢。明天我会试试它! –
答
子视图在地图视图的顶部。我是一个iOS资源。的UIButton - >型 - >详细披露
再见地图 – Ved
好创建的UIView,但我怎么能安装该系统的部位图标使用外部图像白化? (对不起,我的英语) –
有没有像屏幕右上角的“我”圈 –