如何在包含视图的旋转变换期间保持文本水平

时间:2019-01-18 19:37:09

标签: ios swift image-rotation catransform3d

制作自定义UI控件,例如旋转轮,如下所示: enter image description here

您可以看到标签位于“半径”上,但是我需要将所有标签水平放置,并在旋转容器视图时保持相同的方式

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

    let font = UIFont.systemFont(ofSize: 12)
    let attributes = [
        NSAttributedString.Key.font: UIConstants.CaptionFont,
        NSAttributedString.Key.foregroundColor: UIColor.accent
    ]

    for path in pathsForIndicators {
        let cgPath = path.cgPath
        let layer = CATextLayer()
        layer.frame = CGRect(x: cgPath.boundingBox.midX + offset + 22,
                             y: cgPath.boundingBox.midX + 10, width: 40, height: 40)
        layer.position = CGPoint(x: cgPath.boundingBox.midX + offset + 22,
                                 y: cgPath.boundingBox.midY + 10)
        layer.font = font
        let value = 0 // TODO
        let title = String(describing: value)
        let attributedString = NSAttributedString(string: title, attributes: attributes)
        layer.string = attributedString
        layer.isWrapped = false
        layer.alignmentMode = CATextLayerAlignmentMode.center
        layer.foregroundColor = indicatorColor.cgColor
        layer.shouldRasterize = true
        layer.needsDisplayOnBoundsChange = false
        layer.needsDisplay()
        layer.displayIfNeeded()
        textLayer.addSublayer(layer)
    }
    textLayer.backgroundColor = UIColor.clear.cgColor
    textLayer.bounds = bounds
    textLayer.position = centerPoint
    layer.addSublayer(textLayer)