Hi guys,
I am having troubles with transitioning between two views.
I have created a button to call switchViews. But, when I press the button the view turns white.
The view(s) creation is as follows;
And to switch the views I have the following;
Any help would be awesome as I am an iOS / XCode / Mac newbie, with solid programming experience under Windows & C++.
Trying to come to the light side!
I am having troubles with transitioning between two views.
I have created a button to call switchViews. But, when I press the button the view turns white.
The view(s) creation is as follows;
Code:
// Create main window and root view controller
self.mainWindow=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.mainWindow.backgroundColor=[UIColor whiteColor];
self.rootViewController=[[ViewController alloc] init];
self.mainWindow.rootViewController=self.rootViewController;
[self.mainWindow makeKeyAndVisible];
// Create child view controllers
self.firstViewController=[[ViewController alloc] init];
[self.rootViewController addChildViewController:self.firstViewController];
self.secondViewController=[[ViewController alloc] init];
[self.rootViewController addChildViewController:self.secondViewController];
// Get screen boundaries
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGRect bounds=self.mainWindow.bounds;
// Create view1 and assign to firstViewController
view1=[[UIView alloc]initWithFrame: bounds];
[view1 setBackgroundColor:[UIColor yellowColor]];
[self.mainWindow addSubview:view1];
self.firstViewController.view=view1;
// Create view2 and assign to secondViewController
view2=[[UIView alloc]initWithFrame: bounds];//CGRectMake(100,100,100,100)];
[view2 setBackgroundColor:[UIColor redColor]];
self.secondViewController.view=view2;
// Button to switch from view1 to view2
UIButton* OurButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[OurButton setTitle:@"Switch to 'View 2'" forState:UIControlStateNormal];
[OurButton setTitleShadowColor:[UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0f] forState:UIControlStateNormal];
[OurButton setTitleColor:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:1.0f] forState:UIControlStateNormal];
[OurButton setFrame:CGRectMake(22,screenSize.height-(44*2),screenSize.width-44,44)];
[view1 addSubview:OurButton];
[OurButton addTarget:self action:@selector(switchViews) forControlEvents:UIControlEventTouchUpInside];
And to switch the views I have the following;
Code:
- (void)switchViews
{
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
UIViewController *coming=nil;
UIViewController *going=nil;
UIViewAnimationTransition transition;
if(self.firstViewController.view.superview==nil)
{
coming=self.firstViewController;
going=self.secondViewController;
transition=UIViewAnimationTransitionFlipFromLeft;
}
else
{
coming=self.secondViewController;
going=self.firstViewController;
transition = UIViewAnimationTransitionFlipFromRight;
}
[UIView setAnimationTransition: transition forView:view1 cache:YES];
[coming viewWillAppear:YES];
[going viewWillDisappear:YES];
[going.view removeFromSuperview];
[view1 insertSubview: coming.view atIndex:0];
[going viewDidDisappear:YES];
[coming viewDidAppear:YES];
[UIView commitAnimations];
}
Trying to come to the light side!