objective-c - MKMapView Overlay(图像动画)

标签 objective-c ios animation mkmapview overlay

我正在尝试在 MKMapView 上制作雷达图像的动画。我有不同时间的单独图像,并且我已成功将图像设置为 MKMapView 顶部的叠加层。我的问题是当我尝试按顺序依次显示这些不同的图像时。我尝试了一种添加和删除叠加层的方法,但是系统根本处理不好,叠加层闪烁,并且有些叠加层没有被删除,总的来说,它不值得。

有人可以帮我找到一种在 MapView 上以流畅、快速的方式显示多个图像(例如动画 gif)的方法吗?

这是我覆盖图像的代码:

- (id)initWithImageData:(NSData*)imageData withLowerLeftCoordinate:(CLLocationCoordinate2D)lowerLeftCoordinate withUpperRightCoordinate:(CLLocationCoordinate2D)upperRightCoordinate {

self.radarData = imageData;

MKMapPoint lowerLeft = MKMapPointForCoordinate(lowerLeftCoordinate);
MKMapPoint upperRight = MKMapPointForCoordinate(upperRightCoordinate);

mapRect = MKMapRectMake(lowerLeft.x, upperRight.y, upperRight.x - lowerLeft.x, lowerLeft.y - upperRight.y);

return self;

}

- (CLLocationCoordinate2D)coordinate
{
    return MKCoordinateForMapPoint(MKMapPointMake(MKMapRectGetMidX(mapRect), MKMapRectGetMidY(mapRect)));
}

- (MKMapRect)boundingMapRect
{
    return mapRect;
}

这是我在 MapView 上设置它的方法:

- (void)drawMapRect:(MKMapRect)mapRect
          zoomScale:(MKZoomScale)zoomScale
          inContext:(CGContextRef)ctx
{
    MapOverlay *mapOverlay = (MapOverlay *)self.overlay;
    image = [[UIImage alloc] initWithData:mapOverlay.radarData];

    MKMapRect theMapRect = [self.overlay boundingMapRect];
    CGRect theRect = [self rectForMapRect:theMapRect];


    @try {
        UIGraphicsBeginImageContext(image.size);
        UIGraphicsPushContext(ctx);
        [image drawInRect:theRect blendMode:kCGBlendModeNormal alpha:0.5];
        UIGraphicsPopContext();
        UIGraphicsEndImageContext();
    }
    @catch (NSException *exception) {
        NSLog(@"Caught an exception while drawing radar on map - %@",[exception description]);
    }
    @finally {
    }

}

这是我添加叠加层的位置:

- (void)mapRadar {        

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

self.mapOverlay = [[MapOverlay alloc] initWithImageData:appDelegate.image withLowerLeftCoordinate:CLLocationCoordinate2DMake(appDelegate.south, appDelegate.west) withUpperRightCoordinate:CLLocationCoordinate2DMake(appDelegate.north, appDelegate.east)];

[self.mapView addOverlay:self.mapOverlay];
[self.mapView setNeedsDisplay];

self.mapView.showsUserLocation = YES;
MKMapPoint lowerLeft2 = MKMapPointForCoordinate(CLLocationCoordinate2DMake(appDelegate.south2, appDelegate.west2) );
MKMapPoint upperRight2 = MKMapPointForCoordinate(CLLocationCoordinate2DMake(appDelegate.north2, appDelegate.east2));

MKMapRect localMapRect = MKMapRectMake(lowerLeft2.x, upperRight2.y, upperRight2.x - lowerLeft2.x, lowerLeft2.y - upperRight2.y);


[self.mapView setVisibleMapRect:localMapRect animated:YES];
}

#pragma Mark - MKOverlayDelgateMethods

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay{
    if([overlay isKindOfClass:[MapOverlay class]]) {
        MapOverlay *mapOverlay = overlay;
        self.mapOverlayView = [[MapOverlayView alloc] initWithOverlay:mapOverlay];
    }
    return self.mapOverlayView;
}

有谁有一个想法或解决方案,可以让我在 MKMapView 上顺利显示一系列图像?此时,我迫切需要解决方案,并且在其他地方找不到它,所以任何东西都会帮助我,谢谢!

最佳答案

使用Lefteris的建议,您可以将UIImageView与animationImages一起使用。如果您的叠加图像尺寸适合屏幕,动画应该很流畅。

要将触摸传递到下面的 map View ,您可以创建一个新的 UIView 子类,其中包含对 map View 的(弱)引用。覆盖 touch event方法(touchesBegan:withEvent:、touchesMoved:withEvent: 等)转发到 map View ,如下所示:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.mapView touchesBegan:touches withEvent:event];
}

这样用户仍然可以与下面的 View 交互。

当用户尝试缩放时,您可能会遇到问题,因为实时缩放所有动画帧即使不是不可能,也会很困难。您应该考虑使用 CATiledLayer。

关于objective-c - MKMapView Overlay(图像动画),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14075083/

相关文章:

ios - 在后台运行代码

objective-c - EncodeInt64 和 DecodeInt64 适用于无符号整数吗?

CSS动画向右走

ios - Poptorootviewcontroller 延迟

ios - 如何将另一个 View Controller 的数组保存到应用程序委托(delegate)中的文件中?

ios - 扫描 iOS 上的任何蓝牙设备

ios - 关闭软键盘后 Flex 4.6 IOS 屏幕不展开

ios - WiFi 关闭时 NSURLSessionTask 永远不会回调

jquery - 在 Ember 中的路线之间制作动画

javascript - 如何使用 popmotion pure 从关键帧旋转、平移和缩放矩形?