10:50 AM

UIView Flip Transition !!

Posted by pradeep T

These are the UIView parameters that can be animated:
frame
bounds
center
transform
alpha


[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:kTransitionDuration];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];

[self.view addSubview:newView];
[self.view setAlpha:0.0];
[UIView commitAnimations];


/**************************************

-(IBAction)swap:(id)sender{
//If you need to position anything and not have it animated add it before the beginAnimations block

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.6]; //The time in seconds the animation should last.

// In Interface Builder I set the tag of the first button to 1 and the second to 2.
// You could create seperate functions for both bottoms but in this case it's not needed.

if ([sender tag] == 1){
[view1 removeFromSuperview];
[window addSubview:view2];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:window cache:YES];
}
else{
[view2 removeFromSuperview];
[window addSubview:view1];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:window cache:YES];
}

[UIView commitAnimations];
}


/****************************************

2


I think the only thing you're looking for is:

UIView's

+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache;
and

UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
These animation transitions can only be used within an animation block. The transition is set on the container view and then the old view is swapped out for the new view, then the animation is committed.

Like:

CGContextRef context = UIGraphicsGetCurrentContext();

[UIView beginAnimations:nil context:context];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:yourContainerView cache:YES];
[yourContainerView exchangeSubviewsAtIndex:0 withSubviewAtIndex:1];
[UIView commitAnimations];


- (void)loadFlipsideViewController {

FlipsideViewController *viewController = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
self.flipsideViewController = viewController;
[viewController release];

// Set up the navigation bar
UINavigationBar *aNavigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
aNavigationBar.barStyle = UIBarStyleBlackOpaque;
self.flipsideNavigationBar = aNavigationBar;
[aNavigationBar release];

UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(toggleView)];
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"Test123"];
navigationItem.rightBarButtonItem = buttonItem;
[flipsideNavigationBar pushNavigationItem:navigationItem animated:NO];
[navigationItem release];
[buttonItem release];
}


- (IBAction)toggleView {
/*
This method is called when the info or Done button is pressed.
It flips the displayed view from the main view to the flipside view and vice-versa.
*/
if (flipsideViewController == nil) {
[self loadFlipsideViewController];
}

UIView *mainView = mainViewController.view;
UIView *flipsideView = flipsideViewController.view;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:([mainView superview] ? UIViewAnimationTransitionFlipFromRight : UIViewAnimationTransitionFlipFromLeft) forView:self.view cache:YES];

if ([mainView superview] != nil) {
[flipsideViewController viewWillAppear:YES];
[mainViewController viewWillDisappear:YES];
[mainView removeFromSuperview];
[infoButton removeFromSuperview];
[self.view addSubview:flipsideView];
[self.view insertSubview:flipsideNavigationBar aboveSubview:flipsideView];
[mainViewController viewDidDisappear:YES];
[flipsideViewController viewDidAppear:YES];

} else {
[mainViewController viewWillAppear:YES];
[flipsideViewController viewWillDisappear:YES];
[flipsideView removeFromSuperview];
[flipsideNavigationBar removeFromSuperview];
[self.view addSubview:mainView];
[self.view insertSubview:infoButton aboveSubview:mainViewController.view];
[flipsideViewController viewDidDisappear:YES];
[mainViewController viewDidAppear:YES];
}
[UIView commitAnimations];
}

/*************************************************
// 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];

1 comments:

Viswanath said...

Thanks very help full

viswa.