gpt4 book ai didi

swift - 如何使 map 图钉动画掉落? swift 2.2 iOS 9

转载 作者:搜寻专家 更新时间:2023-11-01 05:50:19 26 4
gpt4 key购买 nike

我在此处对 map 进行了基本设置,我正在尝试让图钉以动画形式掉落...就像您在 map 应用程序中按住不放,图钉会掉落到该位置一样。所以当我进入 View 时,所有的图钉都会动画化到它们的位置,我真的不知道该怎么做。如果有人可以指导我找到我需要添加的正确代码,那就太好了!

这是我的当前代码:

   class FirstViewController: UIViewController,MKMapViewDelegate, CLLocationManagerDelegate{



@IBOutlet weak var mapView: MKMapView!





let locationManager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()

self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true


let LitzmanLocation = CLLocationCoordinate2DMake(32.100668,34.775192)
// Drop a pin
let Litzman = MKPointAnnotation()
Litzman.coordinate = LitzmanLocation
Litzman.title = "Litzman Bar"
Litzman.subtitle = "Nemal Tel Aviv St'"
mapView.addAnnotation(Litzman)}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
//Dispose of resources that can be re created.

}

//Mark:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])

{
let location = locations.last

let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))

self.mapView.setRegion(region, animated: true)

self.locationManager.stopUpdatingLocation()
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Errors: " + error.localizedDescription)
}

最佳答案

首先,我没有维护任何标准,我只有一个解决你问题的方法,我正在通过解决其他问题来学习 swiftma​​pKit

这里我给出了一些示例代码,并通过对象库添加了 ma​​pViewUITapGestureRecognizer

用于添加注释,例如从顶部下拉

pinView!.animatesDrop = true

viewForAnnotation委托(delegate)方法中。当您在 map 上点击时,addAnnotation方法将调用添加注释。

为所有注释设置动画

我已经实现了 regionDidChangeAnimated 委托(delegate)方法,只需删除并重新添加所有注释。

import UIKit
import MapKit
import CoreLocation

class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var mapView: MKMapView!

let locationManager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()
print("viewDidLoad")
self.mapView.delegate = self
self.locationManager.delegate = self
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true

let locationData = [
//Walker Art Gallery
["name": "Walker Art Gallery",
"latitude": 12,
"longitude": 79],
//Liver Buildings
["name": "Liver Buildings",
"latitude": 13,
"longitude": 77],
//St George's Hall
["name": "St George's Hall",
"latitude": 14,
"longitude": 77]
]
var annotations = [MKPointAnnotation]()
for each in locationData {
let latitude = CLLocationDegrees(each["latitude"] as! Double)
let longitude = CLLocationDegrees(each["longitude"] as! Double)
let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let name = each["name"] as! String
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = "\(name)"
annotations.append(annotation)
}
mapView.addAnnotations(annotations)
}

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
print("viewForAnnotation \(annotation.title)")
if annotation is MKUserLocation {
return nil
}
let reuseID = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView
if(pinView == nil) {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
}
return pinView
}

@IBAction func addAnnotation(sender: UITapGestureRecognizer) {
if sender.state == .Ended {
let location = sender.locationInView(mapView)
let coordinate = mapView.convertPoint(location,toCoordinateFromView: mapView)

let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
mapView.addAnnotation(annotation)
}
}

func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
print("regionDidChangeAnimated")
let annotations = self.mapView.annotations
self.mapView.removeAnnotations(annotations)
self.mapView.addAnnotations(annotations)
}
}

编辑

对于您在 viewDidLoad 方法中的情况,添加此

self.mapView.delegate = self

要注释 MKAnnotation,您必须实现以下委托(delegate)方法

 //Annotation view
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let reuseID = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView
if(pinView == nil) {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
}
return pinView
}

在注释完所有注释之后,您必须在委托(delegate)方法下实现。

 func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
let annotations = self.mapView.annotations
self.mapView.removeAnnotations(annotations)
self.mapView.addAnnotations(annotations)
}

我假设您知道如何在 map 上点击添加注释...

EDIT2

我的 friend ,这是你需要的。

在 viewDidLoad 中添加一个 notification observer 如下。

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(didBecomeActive), name: UIApplicationDidBecomeActiveNotification, object: nil)

最后将 regionDidChangeAnimated 更改为 didBecomeActive,如下所示

func didBecomeActive() {
let annotations = self.mapView.annotations
self.mapView.removeAnnotations(annotations)
self.mapView.addAnnotations(annotations)
}

关于swift - 如何使 map 图钉动画掉落? swift 2.2 iOS 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38599534/

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