Gen*_*use 3 xcode ios swift xcode7
我有一个代码在地图视图上显示一些注释.但是,我正在尝试使用某些功能,当用户点击UISwitch按钮时可以启用或禁用它.关于我如何实现这一目标的任何建议?提前致谢!
let theHouse = MKPointAnnotation()
theHouse.coordinate = CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365)
theHouse.title = "The House, DC"
theHouse.subtitle = "New Jersey, DC"
myMapView.addAnnotation(theHouse)
myAnnotations.append(theHouse.title!)
//Switch that toggles the annotation...
@IBAction func turnOffAnnotations(_ sender: UISwitch) {
//Code that enables and disables the annotation?
if toggle.isOn{
let visible = myMapView.visibleMapRect
let inRect = myMapView.annotations(in: visible)
for annotation: MKAnnotation in myMapView.annotations{
if (inRect.contains(anno as! AnyHashable)){
myMapView.removeAnnotation(annotation)
}
}
} else {
let visible = myMapView.visibleMapRect
let inRect = myMapView.annotations(in: visible)
for annotation: MKAnnotation in myMapView.annotations{
if (inRect.contains(anno as! AnyHashable)){
myMapView.addAnnotation(annotation)
}
}
}
//How to add back the removed annotations here?...
}
Run Code Online (Sandbox Code Playgroud)
使用viewFor(annotation : MKAnnotation)方法:
斯威夫特3
@IBAction func changeAnnotationsVisibility(_ sender: UISwitch) {
let annotationVisible = sender.isOn
for annotation in myMapView.annotations {
myMapView.view(for: annotation)?.isHidden = !annotationVisible
}
}
Run Code Online (Sandbox Code Playgroud)