【问题标题】:CAShapeLayer rotate around the centerCAShapeLayer 绕中心旋转
【发布时间】:2015-08-01 08:12:40
【问题描述】:

图层呈环形。我希望它围绕其中心旋转。这是我的代码:

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),圆环仍会围绕左上角旋转。

【问题讨论】:

    标签: swift cashapelayer cakeyframeanimation


    【解决方案1】:

    试试 ring.frame = 框架
    确保框架的中心是预期的旋转中心
    对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多