【问题标题】:HowTo: iOS MapKit animated image marker / annotationHowTo:iOS MapKit 动画图像标记/注释
【发布时间】:2017-06-18 09:48:31
【问题描述】:

我有一系列想用作动画的图像,我想将此动画应用于特定的注释。

如何更改 MapKit 默认注释图像以及如何使其动画化?

【问题讨论】:

    标签: animation swift3 mapkit sequence mkannotation


    【解决方案1】:

    制作自定义图像动画图像标记注释:

    • 在资源中:添加所需的动画 X 序列图像:frame_1.png 到 frame_X.png
    • 在代码中:使用 MKMapViewDelegte 扩展您的视图控制器,如下所示:

    class MyViewController: MKMapViewDelegate

    在故事板中:

    1. 选择您的 MapKit 视图
    2. 转到实用程序框架并选择 连接检查员
    3. 将委托元素与您的 控制器。

    现在您的视图控制器可以从 MapView 获取回调。

    在代码中: 添加以下代码以覆盖标记/注释设置回调

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
        {
            if !(annotation is MKPointAnnotation) {
                return nil
            }
    
            let reuseId = "test"
    
            anView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
            if anView == nil {
                anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
                anView!.image = UIImage(named:"frame_1")
                anView!.canShowCallout = true
    
    
               Timer.scheduledTimer(timeInterval: 0.05, target: self, selector: #selector(Changeimage), userInfo: nil, repeats: true)
    
            }
            else {
                anView!.annotation = annotation
            }
    
            return anView
        }
    
        func Changeimage()
        {
            if count == 8
            {
                count=0
            }
            else
            {
                anView!.image = UIImage(named:"frame_\(count+1)")
                anView!.canShowCallout = true
                count=count+1
            }
        }
    

    如果您不需要动画,只需从代码中删除 Timer 行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-05
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 2018-06-18
      • 1970-01-01
      • 2011-01-06
      相关资源
      最近更新 更多