居中 CAShapeLayer
Center CAShapeLayer
我曾尝试在 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)
}
但右下角显示的绘图表示锚点不是中心
找到解决方案
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 的大小,然后设置了它的位置及其工作
我曾尝试在 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)
}
但右下角显示的绘图表示锚点不是中心
找到解决方案
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 的大小,然后设置了它的位置及其工作