5

I have animation code in a class that extends the UIView:

// Start Animation Block
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:[self superview] cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];

//int nextView = currentView + 1;
// Animations
[[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
//And so I would do: [[self superview exchangeSubviewAtIndex:currentView withSubviewAtIndex:nextView];
//Doesn't work because curentView is not global... with each instance of a class it's reset to what it's instanciated to. 
//currentView++;

// Commit Animation Block
[UIView commitAnimations];

This animation code works fine for two views. But I am trying to do this for many views. I tried by adding in a global variable (nextview index and currentView index), however this doesn't work since each custom view class has it's own instance and these variables aren't shared.

Does anyone know how I can accomplish what I want to do?

Thanks!

Edit: I would be able to do this, if I could use [self superview] to access the index of the currentview and of the nextview. Are there some methods to do this?

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
rksprst
  • 6,471
  • 18
  • 54
  • 81

4 Answers4

5

Apple have a pretty good tutorial on view transitions here. Other than the way they implement them you can also look at using a UINavigationController which manages a bunch of views and transitioning between them.

Essentially you need to manage the transition in the superview rather than the view - the superview can know who it's currently displaying and who's next.

Max Stewart
  • 3,573
  • 26
  • 28
2

If you have pointers to the views you're trying to switch between, just call -removeFromSuperview on the "from" view and -addSubview:(the "to" view) on the superview. The transition doesn't rely on any particular method call; more or less any form of the "swap two views" approach will be animated properly.

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
1

For those who find this question who are supporting iOS 4.0 and higher, Apple has provided a built-in API for this task:

See: transitionFromView:toView:duration:options:completion:

Matt James
  • 491
  • 3
  • 11
0

I think a Linked List of views might be what you should think about implementing.

Nick Stinemates
  • 41,511
  • 21
  • 59
  • 60
  • I have an array of views that I create in the appdelegate and I add a different array to each tab view controller. Then the controller uses that array to create uiviews with animation to toggle between them. I'm having trouble with the animation toggling... – rksprst Oct 26 '08 at 21:45
  • There's no need to implement a linked list. `NSArray` is perfectly fine for that. If you're really concerned about the performance, read [this article](http://ridiculousfish.com/blog/?p=27). – Colin Barrett Oct 27 '08 at 04:22