How to draw UIBezierPath identical to MKPolyline in a UIView

后端 未结 2 968
情话喂你
情话喂你 2021-01-06 07:47

Currently I am tracking my location on an MKMapView. My objective is to draw a bezier path identical to an MKPolyline created from tracked locations.

What I have att

2条回答
  •  青春惊慌失措
    2021-01-06 08:36

    Your extensions work fine. The problem may be in the code that adds the layer to the view (which you do not show).

    I'd suggest that you simplify your project, for example use predefined array of points that definitely fit to your view. For example, for a view that is 500 pixels wide and 300 pixels high, you could use something like:

    let points = [
        CGPoint(x: 10, y: 10),
        CGPoint(x: 490, y: 10),
        CGPoint(x: 490, y: 290),
        CGPoint(x: 10, y: 290),
        CGPoint(x: 10, y: 10)
    ]
    

    Use colors that are clearly visible, like black and yellow for your stroke and fill.

    Make sure that your path is correctly added to the view, for example:

    let path = UIBezierPath(points: points)
    
    let shapeLayer = CAShapeLayer(path: path, lineColor: UIColor.blue, fillColor: UIColor.lightGray)
    
    view.layer.addSublayer(shapeLayer)
    

    Inspect the controller that contains the view in Xcode's Interface Builder. In the debug view hierarchy function:

提交回复
热议问题