【问题标题】:Center CAShapeLayer中心 CAShapeLayer
【发布时间】:2020-02-11 10:31:08
【问题描述】:

我尝试在 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)
    }

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

【问题讨论】:

    标签: swift core-graphics calayer cashapelayer


    【解决方案1】:

    寻找解决方案

    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 的大小,然后设置了它的位置及其工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多