ios - GIF 图像作为 Google Map for iOS 中的标记 (Swift)

标签 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 View 并在其中为这个图标设置动画,还没有尝试过,但我尝试了这个线程中提到的解决方案: 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
}

但是这样做也行不通。我在 Assets 中添加了这个 gif 图像,这是它在其中的显示方式:

enter image description here

最佳答案

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

  1. 在项目文件夹而不是 Assets 中添加 gif 图像。 enter image description here
  2. 创建一个新的 swift 文件并复制/粘贴此链接中的代码: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

关于ios - GIF 图像作为 Google Map for iOS 中的标记 (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54680456/

相关文章:

ios - UIActivityViewController 不在 gmail 中显示正文

ios - 如何为 Stripe 配置后端以在 Swift 应用程序中实现?

iphone - arc 的核心图内存消耗问题

ios - 如何防止 Xcode 7 Playgrounds 自动运行?

ios - 如何在 Swift 中将对象数组发送到服务器?

android - getSupportFragmentManager().findFragmentById(R.id.map) 开始返回 null

ios - 与 if/else 子句一起使用时,分段控件 tintColor 设置不正确

ios - 我如何从谷歌地图获取设备的当前位置?

android - 我可以以某种方式在 getMapAsync 调用期间发生错误吗?

ios - AVSpeechSynthesizer 音量太低