gpt4 book ai didi

ios - 清除 CAShapeLayer 中的所有内容

转载 作者:行者123 更新时间:2023-11-28 11:59:28 30 4
gpt4 key购买 nike

我有一个在 AVCaptureVideoPreviewLayer 顶部绘制矩形的函数。这个矩形需要在触发时刷新,并绘制一个新的矩形。最好是从 CAShapeLayer 中删除所有内容,还是只删除该层并添加一个新层?我也不知道该怎么做,但我的尝试如下。

func showRectangle(recLayer: CAShapeLayer, vnRectangleObservation: VNRectangleObservation) -> Void{

// new CAShapeLayer
let newLayer = CAShapeLayer.init()

let rectangle = UIBezierPath.init()
rectangle.move(to: CGPoint.init(x: vnRectangleObservation.topLeft.x, y: vnRectangleObservation.topLeft.y))
rectangle.addLine(to: CGPoint.init(x: vnRectangleObservation.topRight.x, y: vnRectangleObservation.topRight.y))
rectangle.addLine(to: CGPoint.init(x: vnRectangleObservation.bottomRight.x, y: vnRectangleObservation.bottomRight.y))
rectangle.addLine(to: CGPoint.init(x: vnRectangleObservation.bottomLeft.x, y: vnRectangleObservation.bottomLeft.y))

rectangle.close()

newLayer.opacity = 0.4
newLayer.path = rectangle.cgPath
newLayer.fillColor = UIColor.orange.cgColor

// replace current layer containing old rectangle
recLayer.replaceSublayer(recLayer, with: newLayer)

}

最佳答案

您可以遍历所有子层并删除它们。

for sublayer in yourLayer.sublayers ?? [] {
sublayer.removeFromSuperlayer()
}

只需使用 addSublayer 添加新的:

let newSublayer = CAShapeLayer()
yourLayer.addSublayer(newSublayer)

关于ios - 清除 CAShapeLayer 中的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50352167/

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