title: Custom CALayer Animation
body: <p>I have a UIView with a layer that is doing all the drawing for me. So now when I change a value of my view it forwards that to the layer. And when I am in a UIView-Animaiton Block I'd like that change to be animated.</p>
  
  <p>So what I currently do:</p>
  
  <pre><code>class SampleView: UIView {
      var myProperty: CGFloat {
          set {
              sampleLayer.myProperty = newValue
          }
          get {
              return sampleLayer.myProperty
          }
      }
  
      override class var layerClass: AnyClass {
          return SampleViewLayer.self
      }
  
      private var sampleLayer: SampleViewLayer {
          return layer as! SampleViewLayer
      }
  }
  
  class SampleViewLayer: CALayer {
      @NSManaged public var myProperty: CGFloat
  
      override func draw(in ctx: CGContext) {
          // Incredible beautiful drawing is happening here depending on myProperty
      }
  
      override static func needsDisplay(forKey key: String) -&gt; Bool {
          if key == "myProperty" {
              return true
          }
          return super.needsDisplay(forKey: key)
      }
  
      override func action(forKey event: String) -&gt; CAAction? {
  
          if event == "myProperty" {
  
              // 1. How do I find out if the change should be animated?
  
              let animation = CABasicAnimation(keyPath: event)
              animation.fromValue = presentation()?.value(forKey: event)
  
              // 2. How do I find out the proper timingFunction as well as the proper duration?
              animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
              animation.duration = 1.0
  
              return animation
          }
  
          return super.action(forKey: event)
      }
  }
  </code></pre>
  
  <p>And I want to be able to animate it like this:</p>
  
  <pre><code>....
  instanceOfMySampleView.myProperty = 0 // inital value should be set without animation
  UIView.animate(withDuration: 4, animations: {
      self.instanceOfMySampleView.myProperty = 10  // should animate to this value
  })
  ....
  </code></pre>
  
  <p>So the two actual questions are inside my sample code...
   1. How do I find out if the change should be animated? So how to figure out it happened inside a UIView-Animation-Block?</p>
  
  <ol start="2">
  <li>If it's animated, how to find the proper params for duration and timingFunction?</li>
  </ol>
  
  <p>Also. If I create an instance of my View in code set it's myProperty value to the initial value and then directly do a UIViewAnimationBlock, the PresentationLayer will be nil and so my animation will not work properly...</p>