我想做一些像圆形进度条,在UIView中绘制bezier路径的东西,但它看起来像PI / 2旋转.所以我需要通过PI / 2旋转它.问题是,它围绕一些非常奇怪的点旋转,我不知道如何处理它.
我试图遵循
these指令,但它没有帮助,因为我想围绕中心旋转它.
这是我的代码:
required init(coder aDecoder: NSCoder) {
ringLayer = CAShapeLayer()
ringLayerBackground = CAShapeLayer()
super.init(coder: aDecoder)
ringLayer.lineCap = kCALineCapButt
ringLayer.fillColor = nil
ringLayerBackground.lineCap = kCALineCapButt
ringLayerBackground.fillColor = nil
ringLayerBackground.strokeColor = UIColor(white: 0.5, alpha: 0.1).CGColor
}
override func layoutSubviews() {
layer.addSublayer(ringLayer)
layer.addSublayer(ringLayerBackground)
addSubview(imageView)
let rectForRing = CGRectInset(bounds, lineWidth/2, lineWidth/2)
//ringLayer.setAffineTransform(CGAffineTransformMakeRotation(5))
ringLayer.transform = CATransform3DRotate(ringLayer.transform, CGFloat(-M_PI/20), 0.0, 0.0, 1.0)
//Tried both ways
ringLayer.path = CGPathCreateWithEllipseInRect(rectForRing, nil)
ringLayerBackground.path = CGPathCreateWithEllipseInRect(rectForRing, nil)
super.layoutSubviews()
}
我知道有很多类似的问题,但他们没有帮助.
谢谢!
UPDATE
找到神奇的数字,适用于iPhone 6:140
let o = 140.0
ringLayer.transform = CATransform3DTranslate(ringLayer.transform, CGFloat(o), CGFloat(o), 0)
ringLayer.transform = CATransform3DRotate(ringLayer.transform, CGFloat(-M_PI/2), 0.0, 0.0, 1.0)
ringLayer.transform = CATransform3DTranslate(ringLayer.transform, CGFloat(-o), CGFloat(-o), 0)
工作得很好,但我无法猜出这个数字来自哪里.
UPDATE
解决了它.这是ringLayer.position中的东西,需要将其设置为视图的中心.之后,我为bezier路径制作了rect,因为它的中心与ringLayer.position相同.因此代码变得复杂,来自@chrisco的解决方案更加适用.
最佳答案 要回答您的问题,但不是您的直接问题:
var progreessLayer = CAShapeLayer()
// add layer to your view etc
var path = UIBezierPath(arcCenter:center,
radius:100, startAngle:CGFloat(-M_PI_2),
endAngle:CGFloat(3 * M_PI_2), clockwise:true)
progreessLayer.path = path.CGPath
progreessLayer.lineWidth = 10
progreessLayer.strokeColor = UIColor.blackColor().CGColor
progreessLayer.fillColor = nil