- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试像照片应用程序那样做一个自定义 pinView:
到目前为止,我可以在 SO 或 here 上找到的大多数资源和 here显示自定义图钉图像或标注 View 的方法,但在标记本身的自定义 View 上没有。
最终,我想要实现的类似于照片应用程序,其中显示在标记本身上的每个图像都是用户的个人资料图像,而是一个圆形 View 。到目前为止,我的代码非常基本:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "pin") as? MKMarkerAnnotationView
if annotationView == nil {
annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "pin")
annotationView?.canShowCallout = true
annotationView?.leftCalloutAccessoryView = UIImageView(image: UIImage(named: "profileImage"))
} else {
annotationView?.annotation = annotation
}
return annotationView
}
最佳答案
您可以实现任何您想要的自定义设计,您只需要提供一个名为 viewFor 的委托(delegate)方法。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: "pin", for: annotation)
pinView = MapPinView(annotation: annotation, reuseIdentifier: "pin")
return pinView
}
在 viewDidload 方法中,将 mapview 委托(delegate)设置为 self,当然还可以像下面这样注册您的自定义 View 。
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
mapView.register(MapPinView.self, forAnnotationViewWithReuseIdentifier: "pin")
}
自定义 View 可能看起来像这样。
import MapKit
class MapPinView: MKAnnotationView {
private lazy var containerView: UIView = {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
view.backgroundColor = .white
view.layer.cornerRadius = 16.0
return view
}()
private lazy var imageView: UIImageView = {
let imageview = UIImageView()
imageview.translatesAutoresizingMaskIntoConstraints = false
imageview.image = UIImage(named: "bg")
imageview.layer.cornerRadius = 8.0
imageview.clipsToBounds = true
return imageview
}()
private lazy var bottomCornerView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .white
view.layer.cornerRadius = 4.0
return view
}()
// MARK: Initialization
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
setupView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupView() {
containerView.addSubview(bottomCornerView)
bottomCornerView.topAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -15.0).isActive = true
bottomCornerView.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).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: 8.0).isActive = true
imageView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 8.0).isActive = true
imageView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -8.0).isActive = true
imageView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -8.0).isActive = true
}
}
我的看起来像这样。
关于swift - 自定义 MKMarkerAnnotationView Like Photos App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48487846/
我一直在尝试新的 iOS 11 MKMarkerAnnotationView,最后集群工作起来就像一个魅力。但问题是,如图所示,簇的颜色错误。我怎样才能控制这个? 最佳答案 没关系,我设法弄清楚了。
我正在尝试缩放大图像以适合 MKMarkerAnnotationView 子类的字形图像: class MyAnnotationView: MKMarkerAnnotationView { v
设置带有附近咖啡馆注释的 map View 。但是当我尝试使用 MKMarkerAnnotationView 自定义注释时, map View 上没有任何显示。当我记录标记时,框架是 (0 0; 0
我目前正在为通过 MapKit 添加到我的 map View 中的注释创建标注。注释效果很好,但目前没有显示标注,即使我使用正确的代码来启用它们(我相信)。 这是我的viewFor annotatio
我想从 MKMarkerAnnotationView 获取 UIImage,但使用以下代码图像被截断(在顶部和底部,请参见屏幕截图) let newAnnotation = SimpleAnnotat
为什么 MKMarkerAnnotationView 标题会消失。有时我们旋转 map 时会出现标题。如何使 MKMarkerAnnotationView 标题始终可见。 最佳答案 MKMarkerA
我有一个 MKMapView,我想在长按手势上添加注释。添加注释后,我想选择注释 View 。我认为这是一个非常简单的请求。 问题是我正在使用带有自定义 rightCalloutAccessoryVi
我尝试在带有标题的图像 (MKSnapshot) 上绘制标记,但标题未显示。 有谁知道为什么会出现这种情况吗? let annotation = MKPointAnnotation() ann
我正在为 iOS 构建一个应用程序,允许用户基于位置存储图像。现在我想为此使用 iOS 中最新的 AnnotationView (MKMarkerAnnotationView),因为它提供了自动集群。
我正在尝试使用添加到 ios11 中的 map View 的新功能。 我正在将我所有的 MKAnnotationView 与圆形碰撞进行聚类,但我必须在注释变为聚类时实时检查。 我不知道该怎么做。 编
我正在尝试像照片应用程序那样做一个自定义 pinView: 到目前为止,我可以在 SO 或 here 上找到的大多数资源和 here显示自定义图钉图像或标注 View 的方法,但在标记本身的自定义 V
我有一个表格 View ,我想在 UITableViewCell 中显示自定义 MKMarkerAnnotationView。该 TableView 显示在专用的 ViewController 中,而
在较旧的手机(iPhone 7、8)上,为 MKMapSnapshotter 创建的 MKMarkerAnnotationView 可以在 map 上正确显示。仅在 iPhone X 及更高版本上它们
我已经通过引用苹果提供的以下示例对点进行了聚类。 https://developer.apple.com/documentation/mapkit/mkannotationview/declutter
我有一个实现 MKAnnotation 协议(protocol)的 MapItem 类。我正在使用 MKMarkerAnnotationView 在 map 上显示注释。 根据文档,MKMarkerA
MKPinAnnotationView 在 iOS 15 中重命名为 MKMarkerAnnotationView。 在 iOS 14 之前,我将 MKPinAnnotationView 子类化为 c
我是一名优秀的程序员,十分优秀!