通过mapkit覆盖的UIImage动画

时间:2013-09-28 03:35:12

标签: ios overlay mapkit

我正在尝试在美国地图上制作一系列雷达图像。我有从NOAA网站下载的雷达GIF图像列表。

如何在mapkit Overlay上实现一系列图像的动画? 我在这里看过这些帖子: Animated MKOverlayViewAnimating an MKOverlayView

但无法找到解决方案。

3 个答案:

答案 0 :(得分:1)

这是iOS7的最佳解决方案。 MKOverlayRenderer使动画添加到MKMapkit非常困难。请按照此project

中的示例进行操作

答案 1 :(得分:0)

我通过在视图控制器中创建一个计时器来解决问题。每次定时器触发时,它都会为我的自定义MKOverlayRenderer调用setNeedsDisplay方法。在渲染器子类drawMapRect方法中,我有以下代码来更新叠加层上的图像:

-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context{
    if (!weatherDataStore) //This is the store where I have my radar images
        weatherDataStore = [WeatherDataStore sharedInstance];

    UIImage *image = [weatherDataStore currentRadarImage];
    if (!image || ![image isKindOfClass:[UIImage class]]) {
        return;
    }

    CGImageRef imageReference = image.CGImage;
    CGContextSetAlpha(context, 0.8);
    MKMapRect theMapRect = [self.overlay boundingMapRect];
    CGRect theRect = [self rectForMapRect:theMapRect];

    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextTranslateCTM(context, 0.0, -theRect.size.height);

    CGContextDrawImage(context, theRect, imageReference);
}

答案 2 :(得分:0)

<强> SWIFT

通过继承MKOverlay和MKOverlarRenderer

将图像添加为叠加层

<强> MapOverlay.swift

import UIKit
import MapKit

    class MapOverlay: NSObject, MKOverlay {

        var coordinate: CLLocationCoordinate2D
        var boundingMapRect: MKMapRect

        init(coord: CLLocationCoordinate2D, rect: MKMapRect) {
            self.coordinate = coord
            self.boundingMapRect = rect
        }
    }

<强> MapOverlayView.swift

import UIKit
import MapKit

class MapOverlayView: MKOverlayRenderer {

    var overlayImage: UIImage

    init(overlay: MKOverlay, overlayImage:UIImage) {
        self.overlayImage = overlayImage
        super.init(overlay: overlay)
    }

    override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {
        let mapImage = overlayImage.cgImage
        let mapRect = rect(for: overlay.boundingMapRect)
        context.scaleBy(x: 1.0, y: -1.0)
        context.translateBy(x: 0.0, y: -mapRect.size.height)
        context.draw(mapImage!, in: mapRect)
    }
}

<强> ViewController.swift

import UIKit
import MapKit

class ViewController: UIViewController {

    @IBOutlet weak var mapview: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        mapview.delegate = self

        let location = CLLocationCoordinate2D(latitude: 47.6062, longitude: -122.3320)
        let span = MKCoordinateSpanMake(2.0, 2.0)
        let region = MKCoordinateRegion(center: location, span: span)
        mapview.setRegion(region, animated: true)

        let rec = should be same as your image size 
        let overlay = MapOverlay(coord: location, rect: rec)
        mapview.add(overlay)
    }

}



extension ViewController: MKMapViewDelegate {

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {

        if overlay is MapOverlay {
            let logo = UIImage(named: "swift")
            let overlayView = MapOverlayView(overlay: overlay, overlayImage: logo)
            return overlayView
        } else {
            return MKPolylineRenderer()
        }
    }
}

此后调用一个函数来切换MKOverlayRenderer的alpha值。

enter image description here

该项目在github中提供:link