ios - 无法一致地获取在自定义 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)
委托功能。
任何帮助,将不胜感激。
谢谢!
解决方案
更糟糕的是:点击被 UIView 后面的地图识别并导致意外操作。
对我有用的解决方法是使用 enabled UIControl
s 而不是UIView
s。
这可以是UIButton
您直接用作按钮的 s,也可以是背景中启用的不可见大按钮和UIView
顶部的 s。
背景中的大隐形UIButton
人的任务是防止点击进入地图。
如果需要,可以在顶部使用较小的可见按钮进行正常的用户交互。
推荐阅读
- javascript - 使用 componentDidUpdate 自动滚动到锚标记元素
- python - 有没有更有效的方法从给定数量的字典值中提取部分和?
- git - 为什么 Git 凭证管理器需要单独实现每个服务?
- java - 显示一项活动的项目列表
- javascript - CodeMirror 文本区域中的 onresize 事件
- mysql - 如何修复 dBeaver 的身份验证插件错误
- angular - 从内部文件导入成员会抛出“Module not found: Error: Can't resolve”
- html - jQuery - 将点击的 HTML 元素的属性放入图像的 src
- python - ImportError: DLL load failed while importing _pywrap_tensorflow_internal: 动态链接库 (DLL) 初始化例程失败
- java - 正则表达式替换整个单词,而不是字符