我有这张 gif 图片:
我可以使用以下代码添加自定义静态图像作为标记:
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 图像,这是它在其中的显示方式:
最佳答案
我知道了。所以这是解决方案:
- 在项目文件夹而不是 Assets 中添加 gif 图像。
- 创建一个新的 swift 文件并复制/粘贴此链接中的代码:https://github.com/kiritmodi2702/GIF-Swift/blob/master/GIF-Swift/iOSDevCenters%2BGIF.swift
在您想要 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 } }
关于ios - GIF 图像作为 Google Map for iOS 中的标记 (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54680456/