我有一个绘制圆圈的自定义视图。我将自定义视图设置为主视图,宽高比为1:1,高度等于0.5倍。 我的问题是我的圈子没有设置在视图的中心。
CirlceView的代码:
class CircleProgress: UIView {
private var trackLayer = CAShapeLayer()
override func awakeFromNib() {
super.awakeFromNib()
setupTrackLayer()
}
func setupTrackLayer() {
let centerPoint = CGPoint(x: bounds.width/2 , y: bounds.height/2)
let circularPath = UIBezierPath(arcCenter: centerPoint, radius: frame.width/2, startAngle: -CGFloat.pi / 2, endAngle: 2 * CGFloat.pi, clockwise: true)
trackLayer.lineWidth = 20
trackLayer.path = circularPath.cgPath
trackLayer.strokeColor = #colorLiteral(red: 1, green: 0.9450980392, blue: 0.462745098, alpha: 1).cgColor
trackLayer.fillColor = UIColor.clear.cgColor
trackLayer.lineCap = kCALineCapRound
layer.addSublayer(trackLayer)
}
}
答案 0 :(得分:1)
编辑:请参阅此处演示CircleView
var Once:Bool = true
override func layoutSubviews()
{
if(Once)
{
setupTrackLayer()
Once = false
}
}
或将代码放入draw(_ Rect
编辑:试试这个,问题是使用框架,使用相对于屏幕的边界
func drawTimeLeftShape() {
timeLeftShapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: self.bounds.size.width / 2.0 , y: self.bounds.size.height / 2.0), radius:
min(self.bounds.size.width/2, self.bounds.size.height/2), startAngle: -CGFloat.pi / 2, endAngle: 2 * CGFloat.pi, clockwise: true).cgPath
timeLeftShapeLayer.strokeColor = UIColor.red.cgColor
timeLeftShapeLayer.fillColor = UIColor.clear.cgColor
timeLeftShapeLayer.lineWidth = 15
self.layer.addSublayer(timeLeftShapeLayer)
}