向Swift的MKAnnotationView添加脉冲动画

时间:2019-06-23 21:01:04

标签: swift animation mkmapview calayer mkannotationview

我正在尝试将脉冲动画添加到地图的AnnotationView中。我希望动画在将注解放置在地图上时开始播放,并一直进行到用户离开地理围栏区域为止。

我尝试将动画放置在创建注释的位置以及viewFor注释委托方法中。我还尝试将动画添加到annotationView层和mapView层。 我尝试将动画居中到notesView.center,但结果有点随机。以下是我所获得的最接近的方法,如果用户距离地理围栏区域不到600英尺,则在didUpdatelocations委托中调用该方法。 我还逐步检查了代码,以确保所有的纬度和经度信息正确无误,而不会返回0。

// MARK: - SET UP ANNOTATIONS FOR MAP
func addHapsToMapView(mapView: MKMapView, manager: CLLocationManager) {
    for haps in Haps.shared.hapArray {
        guard let managerCoordinates: CLLocationCoordinate2D = manager.location?.coordinate else { return }
        let userLocation: CLLocation = CLLocation(latitude: managerCoordinates.latitude, longitude: managerCoordinates.longitude)

        let hapLocation = CLLocation(latitude: haps.latitude ?? 0, longitude: haps.longitude ?? 0)

        let annotation = MKPointAnnotation()
        annotation.coordinate = CLLocationCoordinate2D(latitude: haps.latitude ?? 0, longitude: haps.longitude ?? 0)
        annotation.title = haps.title

        if userLocation.distance(from: hapLocation) < 600 && checkAnnotations(annotation: annotation, mapView: mapView) {
            let point = mapView.convert(annotation.coordinate, toPointTo: mapView)
            let pulse = PulseAnimation(numberOfPulse: 15, radius: 50.0, position: point)
            pulse.animationDuration = 1.0
            pulse.backgroundColor = UIColor(rgb: 0xff550a).cgColor
            mapView.addAnnotation(annotation)
            mapView.layer.insertSublayer(pulse, above: mapView.layer)
        }
    }
}

预期的结果是使动画集中在注释上并循环播放,直到用户退出地理围栏为止。那时我希望注释和脉冲动画一起消失。

我遇到的问题是动画有时会随机放置在某个地方,或者一旦用户进入地理围栏或他们离开地理围栏并移除注释后便结束,动画仍然在那里进行动画处理消失。

0 个答案:

没有答案