有没有什么方法可以移动MKCircle叠加层和MKUserLocation而不会滑动或闪烁?

当前,我正在使用此代码绘制圆圈。

func mapView(_ mapView: MKMapView,rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if let overlay = overlay as? MKCircle {
        let circleRenderer = MKCircleRenderer(overlay: overlay)
        circleRenderer.fillColor = UIColor.black.withAlphaComponent(0.19)
        circleRenderer.lineWidth = 1
        return circleRenderer
    }
    return MKOverlayRenderer(overlay: overlay)
}

func mapView(_ mapView: MKMapView,didUpdate userLocation: MKUserLocation) {
    let circle = MKCircle(center: userLocation.coordinate,radius: self.regionRadius)
    print("\(userLocation.coordinate)")

    if (CLLocationmanager.authorizationStatus() == .denied || CLLocationmanager.authorizationStatus() == .notDetermined)  {
        mapView.removeOverlays(mapView.overlays)
    } else {
        mapView.removeOverlays(mapView.overlays)
        mapView.addOverlay(circle)
    }
}

当前输出:

有没有什么方法可以移动MKCircle叠加层和MKUserLocation而不会滑动或闪烁?

它工作正常,但圆圈闪烁且闪烁。我需要圆环的平稳移动。我知道这是iOS 13问题。

lic1234567890 回答:有没有什么方法可以移动MKCircle叠加层和MKUserLocation而不会滑动或闪烁?

有两种选择:

  1. 我发现,通常来说,如果在删除旧的叠加层之前添加新的叠加层,则闪烁效果会减弱。

  2. 您可以考虑将圆设为自定义注释视图,而不是叠加层。这样,您只需调整coordinate即可,而无需添加/删除。

通过将圆圈放入注解本身,可以实现无缝衔接,并且都可以跟踪用户:

enter image description here

我关闭了用户跟踪的一半,所以您可以看到两种模式。

class CirclePointerAnnotationView: MKAnnotationView {
    let circleShapeLayer: CAShapeLayer = {
        let shapeLayer = CAShapeLayer()
        shapeLayer.fillColor = UIColor.lightGray.withAlphaComponent(0.25).cgColor
        shapeLayer.strokeColor = UIColor.clear.cgColor
        return shapeLayer
    }()

    let pinShapeLayer: CAShapeLayer = {
        let shapeLayer = CAShapeLayer()
        shapeLayer.fillColor = UIColor.blue.cgColor
        shapeLayer.strokeColor = UIColor.clear.cgColor
        return shapeLayer
    }()

    let imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.image = UIImage(named: "woman")
        imageView.clipsToBounds = true
        return imageView
    }()

    var pinHeight: CGFloat = 100
    var pinRadius: CGFloat = 30
    var annotationViewSize = CGSize(width: 300,height: 300)

    override init(annotation: MKAnnotation?,reuseIdentifier: String?) {
        super.init(annotation: annotation,reuseIdentifier: reuseIdentifier)

        layer.addSublayer(circleShapeLayer)
        layer.addSublayer(pinShapeLayer)
        addSubview(imageView)

        bounds.size = annotationViewSize
    }

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

    override func layoutSubviews() {
        let radius = min(bounds.width,bounds.height) / 2
        let center = CGPoint(x: bounds.midX,y: bounds.midY)
        circleShapeLayer.path = UIBezierPath(arcCenter: center,radius: radius,startAngle: 0,endAngle: 2 * .pi,clockwise: true).cgPath

        let angle = asin(pinRadius / (pinHeight - pinRadius))
        let pinCenter = CGPoint(x: center.x,y: center.y - (pinHeight - pinRadius))
        let path = UIBezierPath()
        path.move(to: center)
        path.addArc(withCenter: pinCenter,radius: pinRadius,startAngle: .pi - angle,endAngle: angle,clockwise: true)
        path.close()
        pinShapeLayer.path = path.cgPath

        let imageViewDimension = pinRadius * 2 - 15
        imageView.bounds.size = CGSize(width: imageViewDimension,height: imageViewDimension)
        imageView.center = pinCenter
        imageView.layer.cornerRadius = imageViewDimension / 2
    }
}
本文链接:https://www.f2er.com/2652855.html

大家都在问