title: Rotate NSImageView at its Center to Make it Spin
body: <p><em>Swift 4, macOS 10.13</em></p>
  
  <p>I have read a variety of answers on SO and still can't get an <code>NSImageView</code> to spin at its center instead of one of its corners.</p>
  
  <p>Right now, the image looks like this (<strong>video</strong>): <a href="http://d.pr/v/kwiuwS" rel="nofollow noreferrer">http://d.pr/v/kwiuwS</a></p>
  
  <p>Here is my code:</p>
  
  <pre><code>//`loader` is an NSImageView on my storyboard positioned with auto layout
  
  loader.wantsLayer = true
  
  let oldFrame = loader.layer?.frame
  loader.layer?.anchorPoint = CGPoint(x: 0.5, y: 0.5)
  loader.layer?.position = CGPoint(x: 0.5, y: 0.5)
  loader.layer?.frame = oldFrame!
  
  let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
  rotateAnimation.fromValue = 0.0
  rotateAnimation.toValue = CGFloat(-1 * .pi * 2.0)
  rotateAnimation.duration = 2
  rotateAnimation.repeatCount = .infinity
  
  loader.layer?.add(rotateAnimation, forKey: nil)
  </code></pre>
  
  <p>Any ideas what I am still missing?</p>