【问题标题】:How to centre my CAShapeLayer within UIView - Swift? [duplicate]如何在 UIView - Swift 中居中我的 CAShapeLayer? [复制]
【发布时间】:2018-09-03 20:33:20
【问题描述】:

我正在尝试使用 UIViewController 创建加载进度圈弹出窗口。我在尝试将我的圈子放在作为父视图的子视图的 UIView 中时遇到问题。请问有人可以建议吗?

我正在尝试使用 UIViewController 创建加载进度圈弹出窗口。我在尝试将我的圈子放在作为父视图的子视图的 UIView 中时遇到问题。请问有人可以建议吗?

 let shapeLayer = CAShapeLayer()

let percentageLabel: UILabel = {
    let label = UILabel()
    label.text = "Start"
    label.textAlignment = .center
    label.font = UIFont.boldSystemFont(ofSize: 32)
    return label
}()


 override func viewDidLoad() {
    super.viewDidLoad()

    createObserver()

    self.poUpOutlet.addSubview(percentageLabel)
    percentageLabel.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
    percentageLabel.center = self.poUpOutlet.center

    let trackLayer = CAShapeLayer()

    let circularPath = UIBezierPath(arcCenter: .zero, radius: 50, startAngle: 0, endAngle: 2 * CGFloat.pi, clockwise: true)
    trackLayer.path = circularPath.cgPath

    trackLayer.strokeColor = UIColor.lightGray.cgColor
    trackLayer.lineWidth = 10
    trackLayer.fillColor = UIColor.clear.cgColor
    trackLayer.lineCap = kCALineCapRound
    trackLayer.position = self.poUpOutlet.center

    self.poUpOutlet.layer.addSublayer(trackLayer)

    shapeLayer.path = circularPath.cgPath

    shapeLayer.strokeColor = UIColor.red.cgColor
    shapeLayer.lineWidth = 10
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.lineCap = kCALineCapRound
    shapeLayer.position = self.poUpOutlet.center

    shapeLayer.transform = CATransform3DMakeRotation(-CGFloat.pi / 2, 0, 0, 1)

    shapeLayer.strokeEnd = 0

    self.poUpOutlet.layer.addSublayer(shapeLayer)
}

【问题讨论】:

    标签: ios swift cashapelayer


    【解决方案1】:

    替换3次

    self.poUpOutlet.center
    

    在您的代码中:

    self.poUpOutlet.convert(self.poUpOutlet.center, from: self.poUpOutlet.superview)
    

    那么它应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      • 2016-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多