How do I animate MKAnnotationView drop?

前端 未结 7 1193
情书的邮戳
情书的邮戳 2020-11-29 01:48

I have a custom MKAnnotationView where I set my image myself in viewForAnnotation. How do I animate it\'s drop like I can with MKPinAnnotationView?

My code is

7条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 02:39

    @mrAlek answer in Swift:

    func mapView(mapView: MKMapView!, didAddAnnotationViews views: [AnyObject]!) {
        println("didAddAnnotationViews()")
    
        var i = -1;
        for view in views {
            i++;
            let mkView = view as! MKAnnotationView
            if view.annotation is MKUserLocation {
                continue;
            }
    
            // Check if current annotation is inside visible map rect, else go to next one
            let point:MKMapPoint  =  MKMapPointForCoordinate(mkView.annotation.coordinate);
            if (!MKMapRectContainsPoint(self.mapView.visibleMapRect, point)) {
                continue;
            }
    
            let endFrame:CGRect = mkView.frame;
    
            // Move annotation out of view
            mkView.frame = CGRectMake(mkView.frame.origin.x, mkView.frame.origin.y - self.view.frame.size.height, mkView.frame.size.width, mkView.frame.size.height);
    
            // Animate drop
            let delay = 0.03 * Double(i)
            UIView.animateWithDuration(0.5, delay: delay, options: UIViewAnimationOptions.CurveEaseIn, animations:{() in
                mkView.frame = endFrame
                // Animate squash
                }, completion:{(Bool) in
                            UIView.animateWithDuration(0.05, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations:{() in
                                mkView.transform = CGAffineTransformMakeScale(1.0, 0.6)
    
                            }, completion: {(Bool) in
                                    UIView.animateWithDuration(0.3, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations:{() in
                                        mkView.transform = CGAffineTransformIdentity
                                        }, completion: nil)
                            })
    
                        })
        }
    }
    

提交回复
热议问题