Guest User

Untitled

a guest
Jul 29th, 2012
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. Animate a UIButton into place from off screen when app loads
  2. -(void)viewWillAppear:(BOOL)animated
  3. {
  4. CGPoint newLeftCenter = CGPointMake( 15.0f + myCocoButton.frame.size.width / 2.0f, myCocoButton.center.y);
  5. [UIView beginAnimations:nil context:nil];
  6. [UIView setAnimationDuration:4.0f];
  7. myCocoButton.center = newLeftCenter;
  8. [UIView commitAnimations];
  9.  
  10. }
  11.  
  12. @implementation MyViewController {
  13. CGPoint _button0TrueCenter;
  14. CGPoint _button1TrueCenter;
  15. CGPoint _button2TrueCenter;
  16. CGPoint _button3TrueCenter;
  17. }
  18.  
  19. static void moveButtonAndSaveCenter(UIButton *button, CGPoint offscreenCenter, CGPoint *trueCenter) {
  20. *trueCenter = button.center;
  21. button.center = offscreenCenter;
  22. }
  23.  
  24. - (void)viewWillAppear:(BOOL)animated {
  25. [super viewWillAppear:animated];
  26. if (animated) {
  27. moveButtonAndSaveCenter(self.button0, CGPointMake(-100, 100), &_button0TrueCenter);
  28. moveButtonAndSaveCenter(self.button1, CGPointMake(420, 100), &_button1TrueCenter);
  29. moveButtonAndSaveCenter(self.button2, CGPointMake(-100, 200), &_button2TrueCenter);
  30. moveButtonAndSaveCenter(self.button3, CGPointMake(420, 200), &_button3TrueCenter);
  31. }
  32. }
  33.  
  34. static void animateButton(UIButton *button, CGPoint newCenter, NSTimeInterval delay) {
  35. [UIView animateWithDuration:.25 delay:delay options:0 animations:^{
  36. button.center = newCenter;
  37. } completion:nil];
  38. }
  39.  
  40. - (void)viewDidAppear:(BOOL)animated {
  41. [super viewDidAppear:animated];
  42. if (animated) {
  43. animateButton(self.button0, _button0TrueCenter, 0);
  44. animateButton(self.button1, _button1TrueCenter, 0.2);
  45. animateButton(self.button2, _button2TrueCenter, 0.4);
  46. animateButton(self.button3, _button3TrueCenter, 0.6);
  47. }
  48. }
  49.  
  50. - (void)viewDidAppear:(BOOL)animated
  51. {
  52. [super viewDidAppear:animated];
  53.  
  54. CGAffineTransform transform = CGAffineTransformMakeTranslation(0.f, -200.f);
  55.  
  56. [self.buttons enumerateObjectsUsingBlock:^(UIButton *button, NSUInteger idx, BOOL *stop) {
  57.  
  58. button.transform = transform; // This translates the view's off the top of the screen
  59.  
  60. [UIView animateWithDuration:0.25f
  61. delay:0.25f * idx
  62. options:UIViewAnimationCurveEaseOut // No point doing easeIn as the objects are offscreen anyway
  63. animations:^{
  64. button.transform = CGAffineTransformIdentity;
  65. } completion:nil];
  66.  
  67. }];
  68. }
Advertisement
Add Comment
Please, Sign In to add comment