无法始终如一地获取在自定义 MKAnnotationView 上调用的 mapViewdidSelectView 函数
Unable to Consistently get mapViewdidSelectView function called on a custom MKAnnotationView
当创建一个名为 GroupUserAnnotationView
的自定义 MKAnnotationView
时,它只注册注释底部的点击,而不是顶部。我尝试在注释视图中添加 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({ [=11=].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 后面的地图识别并导致意外操作。
对我有用的解决方法是使用启用的 UIControl
s 而不是 UIView
s。
这可以是您直接用作按钮的 UIButton
按钮,也可以是后台启用的不可见大按钮和顶部的 UIView
按钮。
背景中的大隐形 UIButton
的任务是防止点击进入地图。
如果需要,在顶部使用较小的可见按钮进行正常的用户交互。
当创建一个名为 GroupUserAnnotationView
的自定义 MKAnnotationView
时,它只注册注释底部的点击,而不是顶部。我尝试在注释视图中添加 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({ [=11=].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 后面的地图识别并导致意外操作。
对我有用的解决方法是使用启用的 UIControl
s 而不是 UIView
s。
这可以是您直接用作按钮的 UIButton
按钮,也可以是后台启用的不可见大按钮和顶部的 UIView
按钮。
背景中的大隐形 UIButton
的任务是防止点击进入地图。
如果需要,在顶部使用较小的可见按钮进行正常的用户交互。