ios - 我想在 iOS Swift 中摇动谷歌地图标记

标签 ios swift google-maps swift3 google-maps-sdk-ios

我正在开发一个项目,我想在谷歌地图上摇动标记。 我正在使用自定义标记图标在 map 上表示。 就像一个人的头在摇晃。 我不知道该怎么做,搜索了很多,但没有找到任何解决方案。

最佳答案

您可以将 CAKeyframeAnimationCABasicAnimation 添加到您的 marker.iconView!.layer 我们添加一个 UIView,其框架大于 UIImageView然后我们需要将 UIImageView 的 anchor 调整到垂直和水平居中的底部,这将是我们动画中的枢轴点,我们的动画将是 Z 平面上从 -30 到 30 级的旋转动画实现所需的动画。这是最简单的方法,但您也可以定义自定义类并制作很多其他东西

    let marker = GMSMarker(position: CLLocationCoordinate2D(latitude: 22.404963, longitude: -79.961755))

    //we need a bigger UIView to avoid the clip problem with the UIImageView
    marker.iconView = UIView(frame: CGRect(x: 0, y: 0, width: 60, height: 40))
    let imageView = UIImageView(frame: CGRect(x: (marker.iconView?.frame.width)!/2 - 14, y: (marker.iconView?.frame.height)! - 36, width: 28, height: 36))
    imageView.contentMode = .scaleAspectFit
    imageView.image = UIImage(named: "iconomapa")
    marker.iconView?.addSubview(imageView)
    imageView.layer.anchorPoint = CGPoint(x: 0.5, y: 1.0) //we need adjust anchor point to achieve the desired behavior
    imageView.layer.frame = CGRect(x: (marker.iconView?.frame.width)!/2 - 14, y: (marker.iconView?.frame.height)! - 36, width: 28, height: 36) //we need adjust the layer frame

    let animation = CAKeyframeAnimation()
    animation.keyPath = "transform.rotation.z"
    animation.values = [ 0, -30 * .pi / 180.0, 30 * .pi / 180.0 , 0]
    animation.keyTimes = [0, 0.33 , 0.66 , 1]
    animation.duration = 1;
    animation.isAdditive = false;
    animation.isRemovedOnCompletion = true
    animation.repeatCount = .infinity

    marker.iconView!.subviews[0].layer.add(animation, forKey: "shakeAnimation")
    marker.map = self.mapView

这是它的样子

enter image description here

希望这有帮助

关于ios - 我想在 iOS Swift 中摇动谷歌地图标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45137032/

相关文章:

ios - 这是内存崩溃吗? NSZombies 没有捕获它

objective-c - 如何在Objective C中将基于XML的字符串值转换为整数

ios - 从 Firebase 将图像下载到数组时遇到问题

clang - Swift:声明 'description' 不能覆盖多个父类(super class)声明

ios - 将 ScrollView 添加到之前创建的 ViewController 中?

google-maps - 在 Access 表单上实现 Google map

ios - 在哪里放置用户登录与用户注销逻辑?

ios - 防止在 iOS 6 上旋转 ABPerson ViewController 和 ABNewPerson ViewController

android - 从 xml 加载 android 布局

google-maps - 对于某些位置,Google 时区 API 返回 ZERO_RESULTS