title: Stop an UIView Animation
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/554997/cancel-a-uiview-animation">Cancel a UIView animation?</a>  </p>
  </blockquote>
  
  
  
  <p>I have the following animation and i want to stop it when i press a button. Can you please tell me how can i do it? I can't figure it out how. Thanks.</p>
  
  <pre><code>-(void)animationLoop:(NSString *)animationID finished:(NSNumber *)finished context:(void    *)context 
  {
  [UIView beginAnimations:@"Move randomly" context:nil]; 
  [UIView setAnimationDuration:1]; 
  // [UIView setAnimationBeginsFromCurrentState:NO];
  
  
  CGFloat x = (CGFloat) (arc4random() % (int) self.bounds.size.width); 
  CGFloat y = (CGFloat) (arc4random() % (int) self.bounds.size.height); 
  
  CGPoint squarePostion = CGPointMake(x, y); 
  strechView.center = squarePostion; 
  
  [UIView setAnimationDelegate:self]; 
  [UIView setAnimationDidStopSelector:@selector(animationLoop:finished:context:)];
  
  [UIView commitAnimations];
  
  } 
  </code></pre>