Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 是否有任何方法可以使用MKUserLocation移动MKCircle覆盖,而无需轻弹或闪烁?_Swift_Mkmapview_Swift5 - Fatal编程技术网

Swift 是否有任何方法可以使用MKUserLocation移动MKCircle覆盖,而无需轻弹或闪烁?

Swift 是否有任何方法可以使用MKUserLocation移动MKCircle覆盖,而无需轻弹或闪烁?,swift,mkmapview,swift5,Swift,Mkmapview,Swift5,目前,我正在使用此代码绘制圆 func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { if let overlay = overlay as? MKCircle { let circleRenderer = MKCircleRenderer(overlay: overlay) circleRenderer.fillColor =

目前,我正在使用此代码绘制圆

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)
    }
}
电流输出:

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

有两种选择:

  • 我发现,一般来说,如果在删除旧覆盖之前添加新覆盖,闪烁效果会减弱

  • > P>您可以考虑将圆定义为自定义注释视图,而不是覆盖。这样,您只需调整
    坐标
    ,而无需添加/删除

    通过将圆放在注释中,它本身是无缝的,并且用户跟踪:

    我在中途关闭了用户跟踪,所以您可以看到这两种模式

    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
        }
    }