美文网首页
iOS小笔记 | 自定义大头针

iOS小笔记 | 自定义大头针

作者: Lol刀妹 | 来源:发表于2019-10-26 10:41 被阅读0次

    自定义大头针涉及到两个class:MKPointAnnotationMKAnnotationViewMKAnnotationView有个属性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)
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS小笔记 | 自定义大头针

          本文链接:https://www.haomeiwen.com/subject/risyvctx.html