2016-04-06 9 views
1

Ich möchte eine benutzerdefinierte Pin Annotation auf einer Karte. Ich habe den Animationscode, aber ich kann es nicht als eine Anmerkung auf einer Karte implementieren.Benutzerdefinierte Pin (Annotation) Animation auf der Karte

Meine Animation Code für den Kreis ist:

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") 

} 

Hier einige der Code für die Karte ist:

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 
} 

Ich habe zur Zeit ein eigenes Bild watch für den Stift Anmerkung genannt, aber Was ich will, ist diese Animation. Irgendwelche Ideen?

Antwort

0

Hier einige Anregungen:

https://github.com/TransitApp/SVPulsingAnnotationView

ähnlich, aber anders. Aber viele Techniken sind die gleichen, denke ich.

+0

Hey das war sehr nützlich und es hat mich weiter gebracht. Das einzige Problem ist, dass, wenn ich um meine Karte scrolle oder die App verlasse und zurückkomme, meine Animation einfriert (stoppt). Ich verstehe den Code aus dem obigen Link nicht genug, um einige seiner Ideen auf meine App anzuwenden, da es mir gut geht, aber nicht gut mit Objective-C. Irgendwelche Gedanken darüber, wie man das löst? – JEL

Verwandte Themen