2015-08-01 8 views
1

Слой имеет форму кольца. Я хочу, чтобы он вращался вокруг своего центра. Вот мой код:CAShapeLayer вращается вокруг центра

class ClipRotateView: UIView { 

    override init(frame: CGRect) { 
    super.init(frame: frame) 
    } 

    required init(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
    } 

    func animate() { 
    let ring = CAShapeLayer() 
    let path = UIBezierPath() 
    path.addArcWithCenter(CGPoint(x: frame.width/2, y: frame.height/2), radius: frame.width/2, startAngle: CGFloat(-3 * M_PI_4), endAngle: CGFloat(-M_PI_4), clockwise: false) 
    ring.path = path.CGPath 
    ring.fillColor = nil 
    ring.strokeColor = UIColor.whiteColor().CGColor 
    ring.lineWidth = 2 
    ring.anchorPoint = CGPoint(x: 0.5, y: 0.5) 
    layer.addSublayer(ring) 

    let scaleAnimation = CAKeyframeAnimation(keyPath: "transform.scale") 
    scaleAnimation.keyTimes = [0, 0.5, 1] 
    scaleAnimation.values = [1, 0.6, 1] 

    let rotateAnimation = CAKeyframeAnimation(keyPath: "transform.rotation.z") 
    rotateAnimation.keyTimes = scaleAnimation.keyTimes 
    rotateAnimation.values = [0, M_PI, 2 * M_PI] 

    let animation = CAAnimationGroup() 
    animation.animations = [scaleAnimation, rotateAnimation] 
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) 
    animation.duration = 0.75 
    animation.repeatCount = HUGE 
    animation.removedOnCompletion = false 

    ring.addAnimation(animation, forKey: nil) 
    layer.borderWidth=1 
    layer.borderColor = UIColor.blackColor().CGColor 
    } 
} 

Даже если я установить anchorPoint (0.5, 0.5), кольцо по-прежнему вращается вокруг левого верхнего угла.

ответ

0

попробовать ring.frame = фрейм
сделать центр уверены в центре кадра предназначен вращение
работает для меня.

Смежные вопросы