简体   繁体   English

CALayer 围绕中心旋转

[英]CALayer Rotation around center

I wanna do something like round progress bar, drawing bezier path in UIView, but it appears rotated by PI/2.我想做一些像圆形进度条、在 UIView 中绘制贝塞尔曲线之类的事情,但它似乎由 PI/2 旋转。 So I need to rotate it by PI/2.所以我需要通过 PI/2 旋转它。 Problem is, that it is rotating around some very strange point, and I don't know how to deal with it.问题是,它正在围绕一些非常奇怪的点旋转,我不知道如何处理。 I tried to follow these instructions, but it doesn't help, because I wanna rotate it around center.我试图按照这些说明进行操作,但无济于事,因为我想围绕中心旋转它。 Here is my code:这是我的代码:

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()
    }

Here is what I'm getting这是我得到的

I know there are a lot of similar question, but they don't help.我知道有很多类似的问题,但它们无济于事。 Thanks!谢谢!

UPDATE Found magic number, that works on iPhone 6: 140更新发现幻数,适用于 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)

Works fine, but I can't guess where that number comes from.工作正常,但我无法猜测这个数字来自哪里。

UPDATE Solved it.更新解决了它。 It was thing in ringLayer.position , need to set it to the centre of view.它是ringLayer.position中的东西,需要将其设置为视图的中心。 After that, I made rect for bezier path so, that its center is the same with ringLayer.position .之后,我为贝塞尔路径制作了 rect ,使其中心与ringLayer.position相同。 So code became complex, solution from @chrisco is much more applicable.所以代码变得复杂,@chrisco 的解决方案更适用。

To answer your problem, but not your immediate question:要回答您的问题,但不是您的直接问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM