繁体   English   中英

选择时不要为 MKMarkerAnnotationView 设置动画

[英]Don't animate MKMarkerAnnotationView when selected

我想捕获MKMarkerAnnotationView上的点击,而不是在发生这种情况时为视图设置动画。

通过我的MKMapView的委托,我可以捕获大致相当于点击MKMarkerAnnotationView的选择和取消选择(点击不属于MKMarkerAnnotationView的标签时也会发生选择)

我正在尝试删除默认动画。 我没有找到一个简单的解决方案。

我试过了:

1/在选择过程中将视图设置为不选择。 这确实会取消动画,但不会捕获更多的点击。

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    view.setSelected(false, animated: false)
    // or
    view.isSelected = false
    handleTap(view)
}

2/ 在视图上添加另一个点击手势识别器并防止其他手势识别器接收触摸。 这很好用,除非我点击标签而不是注释视图。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let view = dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) as? MKMarkerAnnotationView ??
        MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
    view.annotation = annotation
    view.gestureRecognizers?.forEach { view.removeGestureRecognizer($0) }
    let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didTap(gesture:)))
    gestureRecognizer.delegate = self
    view.addGestureRecognizer(gestureRecognizer)
    return view
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return false
}

您可以尝试覆盖类:

final class CustomMarkerAnnotationView: MKMarkerAnnotationView {
  var onSelect: (() -> Void)?

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(false, animated: false)
    if selected {
      onSelect?() // or catch this in delegate
    }        
  }
}

覆盖对我不起作用。 这是最终对我有用的(Objc):

- (void)mapView:(MKMapView *)mapView
didSelectAnnotationView:(MKAnnotationView *)view
{
    //stops animation
    for( CALayer *sublayer in view.layer.sublayers )
    {
        [sublayer removeAllAnimations];
    }
    
    //stops selection
    [view setSelected:NO animated:NO];
}

出于我自己的目的,我使用了下面的方法,因为它允许在选择后立即再次点击引脚时正确取消选择引脚。

- (void)mapView:(MKMapView *)mapView
didSelectAnnotationView:(MKAnnotationView *)view
{
    //stops animation
    for( CALayer *sublayer in view.layer.sublayers )
    {
        [sublayer removeAllAnimations];
    }
    
    //stops selection
    [mapView deselectAnnotation:view.annotation animated:NO];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM