Step 1:
In .h file !
//view for flip action on BarButtonItem
UIView *_superView;
//Views for button to flip
UIView * barButtonSuperView, * barButtonFirstView, * barButtonSecondView;
//Buttons add on righBarbuttonItem
UIButton * primaryButton, *secondaryButton;
//Controllers for flip
FirstViewController *_firstViewController;
SecondViewController *_secondViewController;
Step 2:
In .m file !
//Constant for transition duration
#define kTransitionDuration 0.75
//Constant define for which view show on start up
#define kShowPrimaryOnStartup YES
-(void) viewDidLoad() {
//View for flip background
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor blackColor];
self.view = contentView;
[contentView release];
//add inner parent view
_superView = [[UIView alloc] initWithFrame:self.view.bounds];
_superView.backgroundColor = [UIColor clearColor];
[self.view addSubview:_superView];
//initialize subViewControllers
_firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
_secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
//initialize bar button views
CGRect frame =CGRectMake(0, 0, 30, 30);
barButtonSuperView = [[UIView alloc] initWithFrame:frame];
//primaryButton on barbutton primaryview
barButtonFirstView = [[UIView alloc] initWithFrame:frame];
primaryButton = [UIButton buttonWithType:UIButtonTypeCustom];
primaryButton.frame = frame;
[primaryButton setImage:[UIImage imageNamed:@"filename.png"] forState:UIControlStateNormal];
//[primaryButton setBackgroundColor:[UIColor blueColor]];
[primaryButton addTarget:self action:@selector(flipAction:) forControlEvents:UIControlEventTouchUpInside];
[barButtonFirstView addSubview:primaryButton];
//secondaryButton on barButton SecondaryView
barButtonSecondView = [[UIView alloc] initWithFrame:frame];
secondaryButton = [UIButton buttonWithType:UIButtonTypeCustom];
secondaryButton.frame = frame;
//[secondaryButton setBackgroundColor:[UIColor yellowColor]];
[secondaryButton setImage:[UIImage imageNamed:@"filename.png"] forState:UIControlStateNormal];
[secondaryButton addTarget:self action:@selector(flipAction:) forControlEvents:UIControlEventTouchUpInside];
[barButtonSecondView addSubview:secondaryButton];
//This view done magic for flip
UIView * barButtonSuperSuperView = [[UIView alloc] initWithFrame:frame];
barButtonSuperSuperView.backgroundColor = [UIColor viewFlipsideBackgroundColor];
[barButtonSuperSuperView addSubview:barButtonSuperView];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:barButtonSuperSuperView] autorelease];
[barButtonSuperSuperView release];
barButtonSuperSuperView = nil;
//On Start up which button you show first
if( kShowPrimaryOnStartup ) {
[_superView addSubview:[_firstViewController view]];
[barButtonSuperView addSubview:barButtonFirstView];
}else{
[_superView addSubview:[_secondViewController view]];
[barButtonSuperView addSubview:barButtonSecondView];
}
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
[_superView release], _superView = nil;
[_firstViewController release], _firstViewController = nil;
[_secondViewController release], _secondViewController = nil;
[barButtonFirstView release], barButtonFirstView = nil;
[barButtonSecondView release], barButtonSecondView = nil;
[barButtonSuperView release], barButtonSuperView = nil;
}
- (void)flipAction:(id)sender
{
//Animation Flip for UIViewControllers
[UIView beginAnimations:@"MainViewAnimation" context:NULL];
[UIView setAnimationDuration:kTransitionDuration];
[UIView setAnimationTransition:([[_firstViewController view] superview] ?
UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)
forView:_superView cache:YES];
if ([[_firstViewController view] superview])
{
[[_firstViewController view] removeFromSuperview];
[_superView addSubview:[_secondViewController view]];
}
else
{
[[_secondViewController view] removeFromSuperview];
[_superView addSubview:[_firstViewController view]];
}
[UIView commitAnimations];
//Button user interaction enabled no during flip
primaryButton.userInteractionEnabled = NO;
secondaryButton.userInteractionEnabled = NO;
//Animations Flip for UIBarButton
[UIView beginAnimations:@"BarButtonViewAnimation" context:NULL];
[UIView setAnimationDuration:kTransitionDuration];
[UIView setAnimationDelegate:self];//This should be happen when you calling selectors
[UIView setAnimationTransition:([barButtonFirstView superview] ?
UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)
forView:barButtonSuperView cache:YES];
[UIView setAnimationDidStopSelector:@selector(myTransitionDidStop:finished:context:)];
if ([barButtonFirstView superview])
{
[barButtonFirstView removeFromSuperview];
[barButtonSuperView addSubview:barButtonSecondView];
}
else
{
[barButtonSecondView removeFromSuperview];
[barButtonSuperView addSubview:barButtonFirstView];
}
[UIView commitAnimations];
}
- (void)myTransitionDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
// re-enable user interaction when the flip is completed.
primaryButton.userInteractionEnabled = YES;
secondaryButton.userInteractionEnabled = YES;
}
No comments:
Post a Comment