How to animate a UIView

In this example we have two UIViews:

  • one backgroundView which does not change, and
  • one numberView which can be moved, for example by a pan gesture (we’re not dealing with that though)

Once the user lets go of the movable numberView, we want to reset the view back to the middle, to the same position as the backgroundView. Here’s how we do that:

    // setup animation parameters
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelay:0.1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    
    // define what we're changing
    [self.numberView setCenter:self.backgroundView.center];
    
    // start animating
    [UIView commitAnimations];

Find more details in Apple’s UI View Class Reference





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.