自定义大头针涉及到两个class:MKPointAnnotation
和MKAnnotationView
。MKAnnotationView
有个属性annotation
就是MKPointAnnotation
。
可以把MKPointAnnotation
看做MVC的M,把MKAnnotationView
看做MVC的V。大头针是会复用的,你可以把它想象成cell。
因为MKPointAnnotation
提供的数据有限、MKAnnotationView
自带的控件有限,所以自定义大头针一般不是直接使用它们而是继承它们并按需扩展属性或方法。
大头针的大小可以通过setFrame调整,还可以直接在大头针上add subView:
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
self.frame = CGRect.init(x: 0, y: 0, width: 56, height: 69)
addSubview(logoImageView)
logoImageView.snp.makeConstraints { (make) in
make.centerX.equalTo(self)
make.size.equalTo(CGSize.init(width: 44, height: 44))
make.top.equalTo(6)
}
}
在setAnnotation
方法里进行各种赋值操作:
override var annotation: MKAnnotation? {
didSet {
guard let anno = self.annotation as? StoreAnnotation else { return }
if let isShouldHighlight = anno.raffle?.isShouldHighlight, isShouldHighlight == true {
self.image = UIImage.init(named: "map_mark_sel")
} else {
self.image = UIImage.init(named: "map_mark_def")
}
logoImageView.ds.load(url: anno.raffle?.brand.imageUrl)
}
}
网友评论