我想在iOSSwift中摇动google地图标记
I want to shake the google map marker in iOS Swift
我正在做一个项目,我想在 google 地图上摇动标记。
我正在使用自定义标记图标在地图上表示。
就像一个人的头在摇晃。
不知道怎么办,找了很多都没有找到解决方法。
您可以使用标记 属性 来旋转您的标记,
marker.rotation = (you_angle_in_degree) * M_PI / 180.0f;
// convert degree to radian
Horizontally Shake动画你可以在一段时间后改变旋转的度数,你可以使用定时器,
// create a timer
timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
func timerAction() {
// Place you aniamtion logic here ,
// every 0.5 second change the rotation of your maker
}
你可以添加一个CAKeyframeAnimation
或CABasicAnimation
到你的marker.iconView!.layer
我们添加一个框架比我们里面的UIImageView更大的UIView然后我们需要调整你的UIImageView的锚点在垂直和水平居中的底部这将是我们动画中作为轴心的点,我们的动画将是 Z 平面中从 -30 到 30 级的旋转动画来实现动画 desired.This 是最简单的方法要做到这一点,但你也可以定义一个自定义 class 并制作很多其他东西
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
这是它的样子
希望对您有所帮助
我正在做一个项目,我想在 google 地图上摇动标记。 我正在使用自定义标记图标在地图上表示。 就像一个人的头在摇晃。 不知道怎么办,找了很多都没有找到解决方法。
您可以使用标记 属性 来旋转您的标记,
marker.rotation = (you_angle_in_degree) * M_PI / 180.0f;
// convert degree to radian
Horizontally Shake动画你可以在一段时间后改变旋转的度数,你可以使用定时器,
// create a timer
timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
func timerAction() {
// Place you aniamtion logic here ,
// every 0.5 second change the rotation of your maker
}
你可以添加一个CAKeyframeAnimation
或CABasicAnimation
到你的marker.iconView!.layer
我们添加一个框架比我们里面的UIImageView更大的UIView然后我们需要调整你的UIImageView的锚点在垂直和水平居中的底部这将是我们动画中作为轴心的点,我们的动画将是 Z 平面中从 -30 到 30 级的旋转动画来实现动画 desired.This 是最简单的方法要做到这一点,但你也可以定义一个自定义 class 并制作很多其他东西
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
这是它的样子
希望对您有所帮助