title: My UIView animation is jerky - how can I optimize the following code?
body: <p>On the simulator, my animation method runs smoothly. Unfortunately it seems to be very jerky running on an iPhone 4.</p>
  
  <p>I'm not 100% sure of what the best and fastest way to do animations - so any tips on how I can optimize or completely modify my code to speed up this animation would be most appreciated!</p>
  
  <p><strong>Notes:</strong> </p>
  
  <p>It's already on the main thread so does that make the 'dispatch_async(dispatch_get_main_queue())' call irrelevant or unhelpful?</p>
  
  <p>Is there a way to pre buffer this animation?</p>
  
  <p>Is there a way to see exactly which part of the code is doing most of the slowing down?</p>
  
  <p>There are only about 3 or 4 views in extensionView.subviews.</p>
  
  <p>I've compiled with -Os.</p>
  
  <pre><code>- (void)extendWithAnimation:(BOOL)animated; {
  
      isExtended = YES;
      extensionView.hidden = NO;
      topShadowView.hidden = NO;
  
      topShadowView.frame = CGRectMake(0, defaultHeight, topShadowView.frame.size.width, topShadowView.frame.size.height);
      topShadowView.layer.bounds = CGRectMake(0, 0, topShadowView.frame.size.width, 0);
      extensionView.frame = CGRectMake(0, defaultHeight - 8, DEVICE_WIDTH, extensionHeight + 8);
      extensionView.layer.bounds = CGRectMake(0, 0, DEVICE_WIDTH, 0);
  
      for (UIView *view in extensionView.subviews) {
          view.transform = CGAffineTransformMakeTranslation(0, -(extensionHeight+8));
      }
  
      dispatch_async(dispatch_get_main_queue(), ^{
  
          if (animated) {
              [UIView beginAnimations:nil context:nil];
              [UIView setAnimationDuration:0.3];
              [UIView setAnimationDelegate:self];
          }
  
          extensionView.layer.bounds = CGRectMake(0, 0, DEVICE_WIDTH, extensionHeight + 8);
          topShadowView.layer.bounds = CGRectMake(0, 0, topShadowView.frame.size.width, extensionHeight + 8);
  
          for (UIView *view in extensionView.subviews) {
              view.transform = CGAffineTransformIdentity;
          }
  
          if (animated) {
              [UIView commitAnimations];
          }
      });   
  }
  </code></pre>
  
  <p><strong>Solution:</strong></p>
  
  <p>Solved (thanks Aaron!).</p>
  
  <p>Rendered shadows kill animation big time! Don't use any core graphics shadow properties if you're planning on animating - in my case just a few shadows reduced the frame rate three fold.</p>