Custom pin animation - MKMapView

前端 未结 5 528
生来不讨喜
生来不讨喜 2020-12-08 08:54

I have used pin images in application instead of standard pin, now i want to give animation (dropping effect as it was with standard pins) to custom pins. How can i provide

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 09:27

    This worked well for me. Cant remember where i found it on here though

    - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
    MKAnnotationView *aV; 
    
    for (aV in views) {
    
        // Don't pin drop if annotation is user location
        if ([aV.annotation isKindOfClass:[MKUserLocation class]]) {
            continue;
        }
    
        // Check if current annotation is inside visible map rect, else go to next one
        MKMapPoint point =  MKMapPointForCoordinate(aV.annotation.coordinate);
        if (!MKMapRectContainsPoint(self.mapView.visibleMapRect, point)) {
            continue;
        }
    
        CGRect endFrame = aV.frame;
    
        // Move annotation out of view
        aV.frame = CGRectMake(aV.frame.origin.x, aV.frame.origin.y - self.view.frame.size.height, aV.frame.size.width, aV.frame.size.height);
    
        // Animate drop
        [UIView animateWithDuration:0.3 delay:0.03*[views indexOfObject:aV] options:UIViewAnimationCurveLinear animations:^{
    
            aV.frame = endFrame;
    
            // Animate squash
        }completion:^(BOOL finished){
            if (finished) {
                [UIView animateWithDuration:0.05 animations:^{
                    aV.transform = CGAffineTransformMakeScale(1.0, 0.8);
    
                }completion:^(BOOL finished){
                    if (finished) {
                        [UIView animateWithDuration:0.5 animations:^{
                            aV.transform = CGAffineTransformIdentity;
                        }];
                    }
                }];
            }
        }];
        }
    }
    

提交回复
热议问题