【问题标题】:Is it possible to call animatesDrop in a MKAnnotationView rather than MKPinAnnotationView?是否可以在 MKAnnotationView 而不是 MKPinAnnotationView 中调用 animatesDrop?
【发布时间】:2010-01-26 12:07:58
【问题描述】:

你知道 MKPinAnnotationView 有一个方法“animatesDrop”,可以用阴影在地图上从顶部到点对 pin 注释进行动画处理?好的,是否可以使用自定义图像执行此操作??

【问题讨论】:

    标签: iphone view mkannotation


    【解决方案1】:

    您始终可以在 MKMapViewDelegate 方法中制作自定义动画。

    - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
    

    可能是这样的(你不会得到花哨的阴影动画,如果你想要它,你需要自己做):

    - (void) mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
        CGRect visibleRect = [mapView annotationVisibleRect]; 
        for (MKAnnotationView *view in views) {
           CGRect endFrame = view.frame;
    
           CGRect startFrame = endFrame; startFrame.origin.y = visibleRect.origin.y - startFrame.size.height;
           view.frame = startFrame;
    
           [UIView beginAnimations:@"drop" context:NULL]; 
           [UIView setAnimationDuration:1];
    
           view.frame = endFrame;
    
           [UIView commitAnimations];
        }
    }
    

    Swift 版本

    func mapView(mapView: MKMapView, didAddAnnotationViews views: [MKAnnotationView]) {
        let visibleRect = mapView.annotationVisibleRect
    
        for view:MKAnnotationView in views{
            let endFrame:CGRect = view.frame
            var startFrame:CGRect = endFrame
            startFrame.origin.y = visibleRect.origin.y - startFrame.size.height
            view.frame = startFrame;
    
            UIView.beginAnimations("drop", context: nil)
            UIView.setAnimationDuration(1)
    
            view.frame = endFrame;
    
            UIView.commitAnimations()
        }
    }
    

    【讨论】:

    • @gcamp:我也尝试过这种方式但不起作用(图像没有改变):MKPinAnnotationView *annView=[mapView dequeueReusableAnnotationViewWithIdentifier:ident]; annView=[[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ident]autorelease]; annView.image =[UIImage imageNamed:@"annotImg.png"]; annView.animatesDrop=TRUE;
    • 干得好!也找到了这个链接,它非常相似……所以现在我们需要的只是最后的挤压效果。可能是另一个在尾部瞬间挤压图像高度的动画。 stackoverflow.com/questions/1857160/…
    【解决方案2】:

    @gcamp 相同的答案仅适用于感兴趣的人在 Swift 中

    func mapView(mapView: MKMapView, didAddAnnotationViews views: [MKAnnotationView]) {
        let visibleRect = mapView.annotationVisibleRect
    
        for view:MKAnnotationView in views{
            let endFrame:CGRect = view.frame
            var startFrame:CGRect = endFrame
            startFrame.origin.y = visibleRect.origin.y - startFrame.size.height
            view.frame = startFrame;
    
            UIView.beginAnimations("drop", context: nil)
            UIView.setAnimationDuration(1)
    
            view.frame = endFrame;
    
            UIView.commitAnimations()
        }
    }
    

    【讨论】:

      【解决方案3】:

      感谢@gcamp 的回答,它工作正常,但我对其进行了一些修改,以便准确地确定视图在 mapView 上的放置位置,请检查以下代码:

      - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
      {
      CGRect visibleRect = [mapView annotationVisibleRect];
      for (MKAnnotationView *view in views)
       {
          CGRect endFrame = view.frame;
          endFrame.origin.y -= 15.0f;
          endFrame.origin.x += 8.0f;
          CGRect startFrame = endFrame;
          startFrame.origin.y = visibleRect.origin.y - startFrame.size.height;
          view.frame = startFrame;
      
          [UIView beginAnimations:@"drop" context:NULL];
          [UIView setAnimationDuration:0.2];
      
          view.frame = endFrame;
      
          [UIView commitAnimations];
       }
      }
      

      【讨论】:

        【解决方案4】:

        为 iOS 14 更新

        func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
            let visibleRect = mapView.annotationVisibleRect
            for view: MKAnnotationView in views {
                let endFrame:CGRect = view.frame
                var startFrame:CGRect = endFrame
                startFrame.origin.y = visibleRect.origin.y - startFrame.size.height
                view.frame = startFrame
                UIView.animate(withDuration: 1.5, animations: {
                    view.frame = endFrame
                }, completion: nil)
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2012-05-16
          • 1970-01-01
          • 2018-09-05
          • 1970-01-01
          • 1970-01-01
          • 2015-08-08
          • 2010-11-14
          • 2015-03-03
          • 2023-04-01
          相关资源
          最近更新 更多