【发布时间】:2020-07-06 20:18:29
【问题描述】:
我想知道是否有人可以帮助我使用 Xcode iOS Swift 应用程序。我对编码很陌生,当用户从菜单中单击某个按钮时,我试图删除带有标题/副标题/图像的自定义图钉/注释,但是当它出现时我无法显示该图钉在按钮的 IBAction 函数中。但是,当我从 IBAction 中删除 func mapView 并将其放置在 UIViewController 类中时,pin drop 确实有效(当我在另一个坐标处测试它时)但问题是它在主屏幕加载时自动出现。当我在 IBAction 按钮中包含 mapView 函数时,什么也没有发生,自定义注释也不会出现。
它还会使当前位置的蓝色用户点由于某种原因而消失。我想知道是否有人可以帮助仅删除自定义引脚的初始自动加载,并且仅在单击正确按钮时才执行相关功能。现在我只关注 UI,所以坐标指向 SF 中的恶魔岛。非常感谢!!
我在下面附上了相关代码。
查看控制器代码:
var locationManager = CLLocationManager()
var tulipPin:customPin!
@IBOutlet weak var mapView: MKMapView!
@IBAction func tulipButton(_ sender: Any) {
tulipPin = customPin(Title: "username", Subtitle: "caption", coordinate:CLLocationCoordinate2D(latitude: 37.8270, longitude: -122.4230))
tulipPin.image = UIImage(named: "tulip")
self.mapView.addAnnotation(tulipPin)
}
func mapView(_ mapview: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let annotationView = MKAnnotationView(annotation: tulipPin, reuseIdentifier: "tulip")
annotationView.image = UIImage(named: "tulip")
annotationView.canShowCallout = true
annotationView.calloutOffset = CGPoint(x: -5, y: 5)
let transform = CGAffineTransform(scaleX: 0.07, y: 0.07)
annotationView.transform = transform
return annotationView
}
加上其他可能有助于破译的相关位置代码:
func determineCurrentLocation()
{
mapView.delegate = self
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
mapView.delegate = self
let center = CLLocationCoordinate2D(latitude: 37.8270, longitude: -122.4230)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
mapView.setRegion(region, animated: true)
这是主视图控制器上的 viewDidLoad 函数:
override func viewDidLoad() {
super.viewDidLoad()
regionSet()
determineCurrentLocation()
}
还有我的 customPin 类:
import Foundation
import MapKit
class customPin: NSObject, MKAnnotation {
@IBOutlet weak var mapView: MKMapView!
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
var image: UIImage? = nil
init(Title: String, Subtitle: String, coordinate:CLLocationCoordinate2D) {
self.title = Title
self.subtitle = Subtitle
self.coordinate = coordinate
//self.image
super.init()
}
}
【问题讨论】:
标签: ios swift button mkmapview mkannotation