- Animate a UIButton into place from off screen when app loads
- -(void)viewWillAppear:(BOOL)animated
- {
- CGPoint newLeftCenter = CGPointMake( 15.0f + myCocoButton.frame.size.width / 2.0f, myCocoButton.center.y);
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:4.0f];
- myCocoButton.center = newLeftCenter;
- [UIView commitAnimations];
- }
- @implementation MyViewController {
- CGPoint _button0TrueCenter;
- CGPoint _button1TrueCenter;
- CGPoint _button2TrueCenter;
- CGPoint _button3TrueCenter;
- }
- static void moveButtonAndSaveCenter(UIButton *button, CGPoint offscreenCenter, CGPoint *trueCenter) {
- *trueCenter = button.center;
- button.center = offscreenCenter;
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- if (animated) {
- moveButtonAndSaveCenter(self.button0, CGPointMake(-100, 100), &_button0TrueCenter);
- moveButtonAndSaveCenter(self.button1, CGPointMake(420, 100), &_button1TrueCenter);
- moveButtonAndSaveCenter(self.button2, CGPointMake(-100, 200), &_button2TrueCenter);
- moveButtonAndSaveCenter(self.button3, CGPointMake(420, 200), &_button3TrueCenter);
- }
- }
- static void animateButton(UIButton *button, CGPoint newCenter, NSTimeInterval delay) {
- [UIView animateWithDuration:.25 delay:delay options:0 animations:^{
- button.center = newCenter;
- } completion:nil];
- }
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
- if (animated) {
- animateButton(self.button0, _button0TrueCenter, 0);
- animateButton(self.button1, _button1TrueCenter, 0.2);
- animateButton(self.button2, _button2TrueCenter, 0.4);
- animateButton(self.button3, _button3TrueCenter, 0.6);
- }
- }
- - (void)viewDidAppear:(BOOL)animated
- {
- [super viewDidAppear:animated];
- CGAffineTransform transform = CGAffineTransformMakeTranslation(0.f, -200.f);
- [self.buttons enumerateObjectsUsingBlock:^(UIButton *button, NSUInteger idx, BOOL *stop) {
- button.transform = transform; // This translates the view's off the top of the screen
- [UIView animateWithDuration:0.25f
- delay:0.25f * idx
- options:UIViewAnimationCurveEaseOut // No point doing easeIn as the objects are offscreen anyway
- animations:^{
- button.transform = CGAffineTransformIdentity;
- } completion:nil];
- }];
- }