GIF图片作为iOS版Google Map(快速)中的标记

时间:2019-02-13 22:31:02

标签: ios swift google-maps google-maps-markers cllocationmanager

我有这个gif图片:
enter image description here
我可以使用以下代码添加自定义静态图片作为标记:

if let location = locationManager.location {
            currentLocationMarker = GMSMarker(position: location.coordinate)
            currentLocationMarker?.icon = UIImage(named: "user")
            currentLocationMarker?.map = mapView
            currentLocationMarker?.rotation = locationManager.location?.course ?? 0
        }

我想将此gif图像用作标记。反正有吗虽然我知道鼓励使用静态图像,但是有可能吗?

我当时正在考虑添加一个UIView说一个100x100视图并在其中添加动画图标,虽然还没有尝试过,但是我尝试了该线程中提到的解决方案:How to load GIF image in Swift?

let jeremyGif = UIImage.gifImageWithName("puppy")
let imageView = UIImageView(image: jeremyGif)
imageView.frame = CGRect(x: 0, y: 0, width: jeremyGif?.size.width ?? 100.0, height: jeremyGif?.size.height ?? 100.0)
//view.addSubview(imageView)
//mapView.addSubview(imageView)

if let location = locationManager.location {
    currentLocationMarker = GMSMarker(position: location.coordinate)
    currentLocationMarker?.icon = imageView.image //UIImage(named: "user")
    currentLocationMarker?.map = mapView
    currentLocationMarker?.rotation = locationManager.location?.course ?? 0
}

,但是这样做也不起作用。我已将此gif图片添加到资产中,这就是它在其中的显示方式:

enter image description here

2 个答案:

答案 0 :(得分:0)

我知道了。所以这是解决方案:

  1. 在项目文件夹中而不是资产中添加gif图像。 enter image description here
  2. 制作一个新的快速文件,并从以下链接中复制/粘贴代码:https://github.com/kiritmodi2702/GIF-Swift/blob/master/GIF-Swift/iOSDevCenters%2BGIF.swift
  3. 将此代码添加到要具有gif图像的位置。就我而言,我将其放置在用户位置:

    func addCurrentLocationMarker() {
    currentLocationMarker?.map = nil; currentLocationMarker = nil
    let puppyGif = UIImage.gifImageWithName("Puppy")
    let imageView = UIImageView(image: puppyGif)
    imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
    
    if let location = locationManager.location {
        currentLocationMarker = GMSMarker(position: location.coordinate)
        currentLocationMarker?.iconView = imageView
        currentLocationMarker?.map = mapView
        currentLocationMarker?.rotation = locationManager.location?.course ?? 0
    } }
    

,它的工作原理是: enter image description here

答案 1 :(得分:0)

感谢您的提问和回答。这里的主要思想是通过使用显示Gif图像的UIImageView覆盖GMSMarker.iconView

我将您的想法用于实现,但可能比您的答案要复杂一些,但未使用第3个库

首先,我通过此工具https://onlinepngtools.com/convert-gif-to-png将gif图像导出为png图像序列

然后,我创建可对这些PNG进行动画处理的UIImageView

UIImageView * gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
gifImageView.animationImages = @[ [UIImage imageNamed:@"gifToPngFrame01"]
                                  ,[UIImage imageNamed:@"gifToPngFrame02"]
                                  ,[UIImage imageNamed:@"gifToPngFrame03"]
                                  ,[UIImage imageNamed:@"gifToPngFrame04"]
                                  ,[UIImage imageNamed:@"gifToPngFrame05"]
                                  ,[UIImage imageNamed:@"gifToPngFrame06"]
                                 ,[UIImage imageNamed:@"gifToPngFrame07"]
                                 ,[UIImage imageNamed:@"gifToPngFrame08"]
                                 ,[UIImage imageNamed:@"gifToPngFrame09"]
                                 ,[UIImage imageNamed:@"gifToPngFrame10"]
                                 ,[UIImage imageNamed:@"gifToPngFrame11"]
                                 ,[UIImage imageNamed:@"gifToPngFrame12"]
                                 ,[UIImage imageNamed:@"gifToPngFrame13"]];

gifImageView.animationRepeatCount = 0;
gifImageView.animationDuration = 1;


CLLocationCoordinate2D position2 = CLLocationCoordinate2DMake(21.033333, 105.87);

GMSMarker *gifMarker = [GMSMarker markerWithPosition:position2];
gifMarker.iconView = gifImageView;
gifMarker.map = mapView;
gifMarker.groundAnchor = CGPointMake(0.5, 0.5);

[mapView animateToLocation:position2];

[gifImageView startAnimating];

此代码使用目标c。但我认为这可能会使其他人对该主题有更多好处