efremidze / Cluster

Easy Map Annotation Clustering 📍
MIT License
1.28k stars 121 forks source link

Strange expand/ shrinking animation of an image cluster pin. #67

Closed IVANKRUPSKI closed 6 years ago

IVANKRUPSKI commented 6 years ago

iOS 11, Cluster v. 2.1.3

We are using png image for cluster pin. This png image is scaled depending on the size of the clusters. So the more places clustered in the cluster, the bigger the scaling factor is. In ios 10 it works well, but in iOS 11 there is a strange expand/ shrinking animation.

Any idea where this animation is coming from and how can we get rid of it?

Below is the code that we are using to scale the image and the gif.

untitled-3

class RetailerClusterAnnotationView: ClusterAnnotationView {

    var pinTextColor: UIColor = .white

    required init(annotation: MKAnnotation?, reuseIdentifier: String?, style: ClusterAnnotationStyle, pinTextColor: UIColor) {
        self.pinTextColor = pinTextColor

        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier, style: style)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func configure(with type: ClusterAnnotationStyle) {
        //  super.configure(with:type)
        guard let annotation = annotation as? ClusterAnnotation else { return }

        switch type {
        case let .image(image):
            backgroundColor = .clear
            let count = annotation.annotations.count
            let scale = self.scale(for: count)

            countLabel.text = "\(count)"
            countLabel.textColor = pinTextColor

            self.image = image?.imageResize(sizeChange: CGSize(width: 50*scale, height: 50*scale))

        case let .color(color, radius):
            let count = annotation.annotations.count
            backgroundColor    = color
            var diameter = radius * 2
            switch count {
            case _ where count < 8: diameter *= 0.6
            case _ where count < 16: diameter *= 0.8
            default: break
            }
            frame = CGRect(origin: frame.origin, size: CGSize(width: diameter, height: diameter))
            countLabel.text = "\(count)"
        }
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        countLabel.text = nil
        image = nil
    }

    func updateFor(annotation: ClusterAnnotation, with type: ClusterAnnotationStyle){
        self.annotation = annotation
        configure(with: type)
    }

    private func scale(for annotationsCount: Int) -> CGFloat {
        switch annotationsCount {
        case _ where annotationsCount < 25: return  0.75
        case _ where annotationsCount < 50:  return 1
        case _ where annotationsCount < 100: return 1.25
        case _ where annotationsCount >= 100: return 1.5

        default: return 1
        }
    }

}
efremidze commented 6 years ago

Are you reloading on regionDidChangeAnimated:?

IVANKRUPSKI commented 6 years ago

Yes

Here is the code

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {

            clusterManager.reload(mapView, visibleMapRect:  mapView.visibleMapRect)

        if userIsSelectedPOS {
            clusterManager.maxZoomLevel = Constants.clusterMaxZoomLevel
            userIsSelectedPOS = false
        }
    }
efremidze commented 6 years ago

Does this happen when ur not using the RetailerClusterAnnotationView subclass?

IVANKRUPSKI commented 6 years ago

Hey!

We have found the solution. Initially we were using style.image, and this strange animation appears only with image. With style.color everything works well.