gpt4 book ai didi

ios - 删除在 View 上绘制的线

转载 作者:行者123 更新时间:2023-11-29 01:03:10 24 4
gpt4 key购买 nike

我是 CoreGraphics 的新手。我正在尝试创建包含以编程方式添加到 ScrollView 中的两个 UIImageview 的 View 。添加后我想用线连接两个中心。我使用了贝塞尔曲线路径以及 CAShapelayer。但是线也在 UIImageview 上绘制,所以我想删除 UIImageview 上方的线或将线发送回 UIImageview。我已经完成了下面的代码。

  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)

另请检查屏幕截图,我想删除蓝线的红色标记部分。

enter image description here

最佳答案

您可以简单地通过减少 shapeLayer

zPosition 来做到这一点

这将允许在您的两个 View 下方绘制图层(并且比尝试计算线的新起点和终点容易得多)。如果你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.

因此,因为它默认为 0,而 UIViews 只是 CALayers 的包装器,您可以在您的上使用 -1 的值shapeLayer 以便将其绘制在您的其他 View 后面。

例如:

shapeLayer.zPosition = -1

旁注

大多数时候,在 Swift 中,您不需要在定义变量时显式提供类型。你可以让 Swift 推断它。例如:

let path = UIBezierPath()

关于ios - 删除在 View 上绘制的线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36768650/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com