CALayer框架不适合UIView

时间:2018-10-30 15:51:52

标签: ios swift calayer uibezierpath

我有一个应该持有CALayer的UIView。由于某种原因(我一定不了解CALayer和UIView的工作原理),它无法正常工作。

它是这样的:

My Firebasefirestore database structure

这是我放置视图的代码:

let xPosition = self.viewCenterAnimation.frame.origin.x
let yPosition = self.viewCenterAnimation.frame.origin.y
let position = CGPoint(x: xPosition, y: yPosition)

self.circularProgressBar = CircularProgressBar(radius: 50, position: position, innerTrackColor: UIColor.watermelonDark(), outerTrackColor: UIColor.watermelonLight(), lineWidth: 20)
self.viewCenterAnimation.layer.addSublayer(self.circularProgressBar)

这是用于创建CALayer和路径的代码:

public init(radius: CGFloat, position: CGPoint, innerTrackColor: UIColor, outerTrackColor: UIColor, lineWidth: CGFloat) {
    super.init()

    circularPath = UIBezierPath(arcCenter: .zero, radius: radius, startAngle: 0, endAngle: .pi * 2, clockwise: true)
    outerTrackShapeLayer = CAShapeLayer()
    outerTrackShapeLayer.path = circularPath.cgPath
    outerTrackShapeLayer.position = position
    outerTrackShapeLayer.strokeColor = outerTrackColor.cgColor
    outerTrackShapeLayer.fillColor = UIColor.clear.cgColor
    outerTrackShapeLayer.lineWidth = lineWidth
    outerTrackShapeLayer.strokeEnd = 1
    outerTrackShapeLayer.lineCap = kCALineCapRound
    outerTrackShapeLayer.transform = rotateTransformation
    addSublayer(outerTrackShapeLayer)

    innerTrackShapeLayer = CAShapeLayer()
    innerTrackShapeLayer.strokeColor = innerTrackColor.cgColor
    innerTrackShapeLayer.position = position
    innerTrackShapeLayer.strokeEnd = progress
    innerTrackShapeLayer.lineWidth = lineWidth
    innerTrackShapeLayer.lineCap = kCALineCapRound
    innerTrackShapeLayer.fillColor = UIColor.clear.cgColor
    innerTrackShapeLayer.path = circularPath.cgPath
    innerTrackShapeLayer.transform = rotateTransformation
    addSublayer(innerTrackShapeLayer)
}

很显然,我希望将圆完美地放置在白色的UIView中。

谢谢!

0 个答案:

没有答案