ios - 在标记顶部显示自定义视图
问题描述
点击标记后如何在CustomCalloutView
标记顶部显示?
处理这种方法:
func presentCallout(from rect: CGRect, in view: UIView, constrainedTo constrainedRect: CGRect, animated: Bool) {
if isCalloutTappable() {
let gesture = UITapGestureRecognizer(target: self, action: #selector(calloutTapped))
self.isUserInteractionEnabled = true
self.addGestureRecognizer(gesture)
} else {
self.isUserInteractionEnabled = false
}
//Always, Slightly above center
self.center = view.center.applying(CGAffineTransform(translationX: 0, y: -self.frame.height))
view.addSubview(self)
}
如何将标记框传递给它?
解决方案
解决方案:
在CustomCalloutView
var annotationPoint: CGPoint
required init(annotation: CustomAnnotation, annotationPoint: CGPoint) {
self.representedObject = annotation
self.annotationPoint = annotationPoint
}
func presentCallout(from rect: CGRect, in view: UIView, constrainedTo constrainedRect: CGRect, animated: Bool) {
self.center = annotationPoint.applying(CGAffineTransform(translationX: 0, y: -self.frame.height - 40.0))
view.addSubview(self)
}
在MapVC
:
func mapView(_ mapView: MGLMapView, calloutViewFor annotation: MGLAnnotation) -> MGLCalloutView? {
let customAnnotation = CustomAnnotation(coordinate: annotation.coordinate, title: title, subtitle: subtitle, image: img!)
let annotationPoint = mapView.convert(annotation.coordinate, toPointTo: nil)
return CustomCalloutView(annotation: customAnnotation, annotationPoint: annotationPoint)
}
推荐阅读
- c++ - 在 C++ 中使用 shared_ptr 从公共静态成员函数访问私有构造函数
- ios - UISplitViewController:使用 DisplayMode.primaryOverlay 时 displayModeButtonItem 被隐藏
- git - git for-each-ref 与
什么都不返回 - ssh - 共享主机 SSH(启用 rsync)到 Google 云存储
- kotlin - Simplify testing of a null variable in an IF statement
- python - 如何扩展/扩大 numpy 数组?
- amazon-cloudfront - HtmlUnit 不加载整个页面
- python - 将字典存储到 Jinja 模板中
- java - 为什么我会收到这个 InvalidPathException
- angular - 如何解决这个问题( /home/user/MagnaFurn/node_modules/@angular/cli/node_modules/@angular-devkit/architect/ )?