1
我想在地圖上定製引腳註釋。我有動畫代碼,但似乎無法將其作爲地圖上的註釋來實現。地圖上的自定義引腳(註釋)動畫
我的圈子動畫代碼是:
func animation() {
// The circle in its smallest size.
let circlePath1 = UIBezierPath(arcCenter: CGPoint(x: 200,y: 150), radius: CGFloat(3), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)
// The circle in its largest size.
let circlePath2 = UIBezierPath(arcCenter: CGPoint(x: 200,y: 150), radius: CGFloat(60), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)
// Configure the layer.
let shapeLayer = CAShapeLayer()
shapeLayer.strokeColor = green.CGColor
shapeLayer.fillColor = green.CGColor
// This is the path that's visible when there'd be no animation.
shapeLayer.path = circlePath1.CGPath
self.layer.addSublayer(shapeLayer)
// Animate the path.
let pathAnimation = CABasicAnimation(keyPath: "path")
pathAnimation.fromValue = circlePath1.CGPath
pathAnimation.toValue = circlePath2.CGPath
// Animate the alpha value.
let alphaAnimation = CABasicAnimation(keyPath: "opacity")
alphaAnimation.fromValue = 1
alphaAnimation.toValue = 0
// We want both animations to run together perfectly, so we
// put them into an animation group.
let group = CAAnimationGroup()
group.animations = [pathAnimation, alphaAnimation]
group.duration = 2.4
group.repeatCount = FLT_MAX
// Add the animation to the layer.
shapeLayer.addAnimation(group, forKey:"sonar")
}
下面是一些在地圖代碼:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "MyPin"
if annotation.isKindOfClass(MKUserLocation) {
return nil
}
// Reuse the annotation if possible
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
// I have an image here, but instead I want my custom animation
annotationView!.image = UIImage(named: "watch")
} else {
annotationView!.annotation = annotation
}
return annotationView
}
我現在有一個自定義圖像稱爲watch
的引腳註釋,但我想要的是這部動畫。有任何想法嗎?
嘿,這是非常有用的,它讓我進一步。唯一的問題是,當我滾動我的地圖或離開應用程序並回來時,我的動畫凍結(停止)。我不明白以上鍊接中的代碼足以嘗試將他的一些想法應用到我的應用程序,因爲我沒事,但Objective-C不太好。有關如何解決這個問題的任何想法? – JEL