CAShapeLayer围绕中心旋转

时间:2015-08-01 08:12:41

标签: swift cashapelayer cakeyframeanimation

该层具有环形。我希望它围绕它的中心旋转。这是我的代码:

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 个答案:

没有答案