title: How do I correctly calculate the delta (difference) between two CGAffineTransform matrices?
body: <p>I've been playing around with the Stanford Uni Lectures / WWDC video that shows you how to create a gesture to pinch/zoom simultaneously. It returns a <code>CGAffineTransform</code> that you then apply to the UIView.</p>
  
  <p>This all works fine, but I'm trying to make it a bit more fluid and add some deceleration to the code. I've tried to do this by storing the transformation matrix on each <code>UIGestureRecognizerStateChanged</code> call, and then when I receive <code>UIGestureRecognizerStateEnded</code> I store the delta between the last Changed and Ended matrices:</p>
  
  <pre><code>- (void)handleTransform:(TransformGestureRecognizer *)transformRecognizer
  {
      CGAffineTransform transform = transformRecognizer.transform;
  
      // cancel any previous animation updates
      [UIView cancelPreviousPerformRequestsWithTarget:self selector:@selector(decelerateView:) object:transformRecognizer.view];
  
      if(transformRecognizer.state == UIGestureRecognizerStateBegan)
      {
          transform = CGAffineTransformConcat(transformRecognizer.view.transform, transform);
          transformRecognizer.transform = transform;
      }
      else if(transformRecognizer.state == UIGestureRecognizerStateChanged)
      {
          changingTransform = transform;
      }
      else if(transformRecognizer.state == UIGestureRecognizerStateEnded)
      {
          // get transform difference
          releaseTransformDiff.a = (transform.a - changingTransform.a);
          releaseTransformDiff.b = (transform.b - changingTransform.b);
          releaseTransformDiff.c = (transform.c - changingTransform.c);
          releaseTransformDiff.d = (transform.d - changingTransform.d);
          releaseTransformDiff.tx = (transform.tx - changingTransform.tx);
          releaseTransformDiff.ty = (transform.ty - changingTransform.ty);
  
          // start updating deceleration animation
          [self performSelector:@selector(decelerateView:) withObject:transformRecognizer.view afterDelay:1.0/60.0];
      }
  
      transformRecognizer.view.transform = transform;
  }
  </code></pre>
  
  <p>Then, I have a <code>decelerateView:</code> method for animating the deceleration:</p>
  
  <pre><code>#define DECELERATION_RATE 0.9
  
  - (void)decelerateView:(UIView *)view
  {
      releaseTransformDiff.a = releaseTransformDiff.a * DECELERATION_RATE;
      releaseTransformDiff.b = releaseTransformDiff.b * DECELERATION_RATE;
      releaseTransformDiff.c = releaseTransformDiff.c * DECELERATION_RATE;
      releaseTransformDiff.d = releaseTransformDiff.d * DECELERATION_RATE;
      releaseTransformDiff.tx = releaseTransformDiff.tx * DECELERATION_RATE;
      releaseTransformDiff.ty = releaseTransformDiff.ty * DECELERATION_RATE;
  
      view.transform = CGAffineTransformMake(view.transform.a + releaseTransformDiff.a, 
                                             view.transform.b + releaseTransformDiff.b, 
                                             view.transform.c + releaseTransformDiff.c, 
                                             view.transform.d + releaseTransformDiff.d, 
                                             view.transform.tx + releaseTransformDiff.tx,
                                             view.transform.ty + releaseTransformDiff.ty);
      [self performSelector:@selector(decelerateView:) withObject:view afterDelay:1.0/60.0];
  }
  </code></pre>
  
  <p>This works sometimes, but other times it stops abruptly, and virtually never decelerates any rotation spins I do... I guess I'm not calculating the delta correctly for the two matrices. What is the correct way to do this?</p>