VDPATEL
VDPATEL

Reputation: 416

Remove line drawn on view

I am new to CoreGraphics . I am trying to create view which contains two UIImageview added in scrollview programatically. After adding it i want to connect both center with line. I have used bezier path as well as CAShapelayer. But line is drawn on UIImageview also so i want to remove line above UIImageview or send line to back to UIImageview. I have done below code.

  let path: UIBezierPath = UIBezierPath()
    path.moveToPoint(CGPointMake(personalProfile.center.x, personalProfile.center.y))
    path.addLineToPoint(CGPointMake(vwTwo.center.x, vwTwo.center.y))

    let shapeLayer: CAShapeLayer = CAShapeLayer()
    shapeLayer.path = path.CGPath
    shapeLayer.strokeColor = UIColor.blueColor().CGColor
    shapeLayer.lineWidth = 3.0
    shapeLayer.fillColor = UIColor.clearColor().CGColor

    self.scrollView.layer.addSublayer(shapeLayer)   

Please also check screenshot, i want to remove red marked portion of blue line .

enter image description here

Upvotes: 0

Views: 1078

Answers (2)

Hamish
Hamish

Reputation: 80801

You can do this simply by reducing the zPosition of your shapeLayer

This will allow the layer to be drawn underneath your two views (and far easier than trying to calculate a new start and end point of your line). If you look at the documentation for zPosition:

The default value of this property is 0. Changing the value of this property changes the the front-to-back ordering of layers onscreen. Higher values place this layer visually closer to the viewer than layers with lower values. This can affect the visibility of layers whose frame rectangles overlap.

Therefore, as it defaults to 0, and UIViews are just wrappers for CALayers, you can use a value of -1 on your shapeLayer in order to have it drawn behind your other views.

For example:

shapeLayer.zPosition = -1

Side Note

Most of the time in Swift, you don't need to explicitly supply a type when defining a variable. You can just let Swift infer it. For example:

let path = UIBezierPath()

Upvotes: 1

Venomy
Venomy

Reputation: 2244

I would see 2 options, an easy and a harder option.

  1. Move the UIImageView to the front after drawing the line, effectively hiding the line behind the UIImageView.
  2. Calculate the points at which you want the line to start and end and draw a line from these points instead of the centers.

Upvotes: 0

Related Questions