gpt4 book ai didi

swift - 中心 CAShapeLayer

转载 作者:行者123 更新时间:2023-11-30 10:24:14 26 4
gpt4 key购买 nike

我尝试在 UIImageView 中居中 CAShapeLayer 但没有成功,我找到了解决方案,将其(CAShapeLayer)添加到 UIVIew,然后将 UIVIew 添加到 ImageView 并成功!!!

这是好的做法吗?任何建议

func Draw(Fn:Bool) {

let theCenter = CGPoint(x: img.bounds.midX, y: img.bounds.midY)

// Create Layer To Add The CAShapeLayer On it So I can Center it
let DrawingLayer = UIView(frame: CGRect(x: img.bounds.midX, y: img.bounds.midY, width: 140, height: 80))
DrawingLayer.center = theCenter
img.addSubview(DrawingLayer)

// Create The Drawing
let shapeLayer = CAShapeLayer()
shapeLayer.path = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 140, height: 80)).cgPath
shapeLayer.fillColor = UIColor.clear.cgColor

shapeLayer.lineWidth = 3.0


// Add Drawing to UIView
DrawingLayer.layer.addSublayer(shapeLayer)
// Add UIView To ImageView
img.addSubview(DrawingLayer)
}

我正在使用这段代码:

func DrawArmor(Fn:Bool) {

let shapeLayer = CAShapeLayer()
shapeLayer.path = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 140, height: 80)).cgPath
shapeLayer.fillColor = UIColor.clear.cgColor

shapeLayer.lineWidth = 3.0
if (Fn) {
shapeLayer.name = "Armor Friendly"
shapeLayer.strokeColor = UIColor.blue.cgColor
}else{
shapeLayer.name = "ArmorEnemy"
shapeLayer.strokeColor = UIColor.red.cgColor
}

img.layer.addSublayer(shapeLayer)
}

但是绘图显示在右下角,这意味着 anchor 不是中心

enter image description here

最佳答案

寻找解决方案

shapeLayer.frame = CGRect(x: img.bounds.midX, y: img.bounds.midY, width: 140, height: 80)
shapeLayer.position = CGPoint(x: img.bounds.midX, y: img.bounds.midY)

我设置了 CAShapeLayer 的大小,然后设置它的位置及其工作

关于swift - 中心 CAShapeLayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60166845/

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