首页 > 解决方案 > 无法一致地获取在自定义 MKAnnotationView 上调用的 mapViewdidSelectView 函数

问题描述

创建自定义MKAnnotationView时,称为GroupUserAnnotationView,它只注册注释底部的点击,而不是顶部。我尝试将 a 添加UITapGestureRecognizer到注释视图中,但它不起作用。

创建注释视图:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard !annotation.isKind(of: MKUserLocation.self) else {
            return nil
        }
        
        var annotationView: MKAnnotationView!
        var annotationIdentifier: String!
        
        if annotation.isKind(of: GroupUserAnnotation.self) {
            annotationIdentifier = "groupUser"
            annotationView = GroupUserAnnotationView(annotation: annotation as! GroupUserAnnotation, reuseIdentifier: annotationIdentifier)
            annotationView.frame = (annotationView as! GroupUserAnnotationView).containerView.frame
    
        } else {
            annotationIdentifier = "marker"
            annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        }
        
        
        return annotationView
    }

GroupUserAnnotationView: _


class GroupUserAnnotation: MKPointAnnotation {
    var email: String!
    var image: UIImage!
}

class GroupUserAnnotationView: MKAnnotationView {
    public lazy var containerView: UIView = {
        let view = UIView(frame: CGRect(x: -40, y: -70, width: 70, height: 70))
        view.backgroundColor = .accentColor
        view.layer.cornerRadius = view.frame.width / 2
        view.isUserInteractionEnabled = true
        
        return view
    }()
    
    public lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.image = (annotation as! GroupUserAnnotation).image!
        imageView.clipsToBounds = true
        imageView.isUserInteractionEnabled = true
        return imageView
    }()
    
    public lazy var bottomCornerView: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .accentColor
        view.layer.cornerRadius = 4.0
        view.isUserInteractionEnabled = true
        return view
    }()
    
    // MARK: Initialization
    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation as! GroupUserAnnotation, reuseIdentifier: reuseIdentifier)
        setupView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func setupView() {
        subviews.forEach({ $0.removeFromSuperview() })
        
        containerView.addSubview(bottomCornerView)
        bottomCornerView.topAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -20.0).isActive = true
        bottomCornerView.centerXAnchor.constraint(equalTo: containerView.centerXAnchor, constant: 2).isActive = true
        bottomCornerView.widthAnchor.constraint(equalToConstant: 24).isActive = true
        bottomCornerView.heightAnchor.constraint(equalToConstant: 24).isActive = true
        
        let angle = (39.0 * CGFloat.pi) / 180
        let transform = CGAffineTransform(rotationAngle: angle)
        bottomCornerView.transform = transform
        
        addSubview(containerView)
        containerView.addSubview(imageView)
        imageView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 2.0).isActive = true
        imageView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 2.0).isActive = true
        imageView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -2.0).isActive = true
        imageView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -2.0).isActive = true
        
        imageView.layer.cornerRadius = (containerView.frame.size.width - 1) / 2
        
    }

再一次,只有注释的底部实际上是注册的(这意味着我已经正确地实现了mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)委托功能。

任何帮助,将不胜感激。

谢谢!

标签: iosswiftmapkituigesturerecognizermkannotation

解决方案


更糟糕的是:点击被 UIView 后面的地图识别并导致意外操作。

对我有用的解决方法是使用 enabled UIControls 而不是UIViews。

这可以是UIButton您直接用作按钮的 s,也可以是背景中启用的不可见大按钮和UIView顶部的 s。

背景中的大隐形UIButton人的任务是防止点击进入地图。

如果需要,可以在顶部使用较小的可见按钮进行正常的用户交互。


推荐阅读