2017-07-17 31 views
2

我正在研究一個項目,我想要在Google地圖上撼動標記。 我正在使用自定義標記圖標在地圖上表示。像一個人的頭在搖晃。 我不知道如何去做和搜索很多,但沒有找到任何解決方案。我想在iOS中撼動Google地圖標記Swift

+0

你需要動畫sh是水平的還是垂直的? –

+0

你可以使用標記的旋轉屬性, – Dhiru

+0

你也可以這樣做 https://stackoverflow.com/questions/40543095/bounce-animation-on-google-map-marker-in-ios-objective-c – Dhiru

回答

2

你可以添加一個CAKeyframeAnimationCABasicAnimation到你的marker.iconView!.layer我們添加一個比我們的UIImageView框架大的UIView,那麼我們需要調整錨點你的UIImageView的點在垂直和水平居中的底部,這將成爲我們動畫中的樞軸點,我們的動畫將是一個在-30到30級的Z平面上的旋轉動畫,以實現所需的動畫。這是最簡單的方式來做到這一點,但你也可以自定義一個類,並作出了很多其他的事情

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

希望這有助於

+0

謝謝@Reinier 但我得到無 marker.iconView!.layer.add(動畫,forKey:「shakeAnimation」) –

+0

@ParthDhorda我的答案已更新,您需要用您的替換iconomapa標記圖像名稱 –

+0

我想引腳應該保持在相同的緯度和長度,但頭部應該晃動不是整個針 謝謝 –

0

您可以使用標記屬性來旋轉你的標記,

marker.rotation = (you_angle_in_degree) * M_PI/180.0f; 
// convert degree to radian 

水平搖晃動畫後的一段時間間隔改變度旋轉,你可以使用定時器,你可以得到,

// 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 
    }