我想像地球仪一样旋转 GMSMarker。我曾尝试使用旋转动画代码,但它们都需要 UIImageView 但在 GMSMarker 的情况下我没有 UIImageView 容器。另外,我不确定我们是否可以这样做
请提出建议。
最佳答案
GMSMarker
是一个NSObject
,而不是一个UIView
。所以你不能为它使用 UIView.aniamteWithDuration()
。
此外,您只能将 CATransform3D
应用于 UIView
,因此您也不能执行 marker.layer.transform = rotationTransform
。
但是,GMSMarker
有一个名为 icon
的 UIImage 属性。您可以通过更改 GMSMarker
的 icon
属性的大小来使用它作为解决方法。
示例代码:
let originalImage = UIImage(named: "markerIcon")
delay(seconds: 0.05) { () -> () in
marker.icon = self.newImageWithWidth(originalImage!, width: originalImage!.size.width/2, height: originalImage!.size.height)
delay(seconds: 0.05, { () -> () in
marker.icon = self.newImageWithWidth(originalImage!, width: 0.1, height: originalImage!.size.height)
delay(seconds: 0.05, { () -> () in
marker.icon = self.newImageWithWidth(originalImage!, width: originalImage!.size.width/2, height: originalImage!.size.height)
delay(seconds: 0.05, { () -> () in
marker.icon = self.newImageWithWidth(originalImage!, width: originalImage!.size.width, height: originalImage!.size.height)
})
})
})
}
上面的代码每 0.05 秒更改一次标记图标的宽度
,从一半变为 0.1,然后恢复为一半和全尺寸。
关于改变 UIImage 大小的示例代码:
func newImageWithWidth(image: UIImage, width: CGFloat, height: CGFloat) -> UIImage{
let imageSize = CGSize(width: width, height: height)
UIGraphicsBeginImageContext(imageSize)
let imageRect = CGRectMake(0, 0, width, height)
image.drawInRect(imageRect)
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return resizedImage
}
您可以在 this Gist 中找到完整的源代码.
关于ios - 在 iOS 中旋转 GMSMarker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31133650/