I'm working on an iPhone/iPad application and trying to switch views through a function call, not by a button. I've seen lots of sources switching views triggered by a button, but there's nothing that explains how to switch views triggered by a function. I tried to do this on my own, but it failed. Here's the code that I tried:
- (void)viewDidLoad
{
[super viewDidLoad];
if (self.webview == nil) {
self.webview = [[MainViewController alloc] init];
/* webview initialized with storyboard */
if (self.view.superview == nil) {
[self.view insertSubview:self.webview.view atIndex:0];
}
[storyboard release];
}
}
This is viewDidLoad of the viewController.m. First, it shows WebView.
- (void)changeView {
self.normview = [[AlternateViewController alloc] init];
/* normview initialized with storyboard */
[UIView beginAnimations:#"View Flip" context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[self.webview viewWillDisappear:YES];
[self.webview viewDidDisappear:YES];
[self.webview.view removeFromSuperview];
[self.view insertSubview:self.normview.view atIndex:0];
[self.normview viewWillAppear:YES];
[self.normview viewDidAppear:YES];
[UIView commitAnimations];
}
And when this changeView function (in a viewController.m) is called, It should change the view from webview to normview (actually, it works fine when the same code is triggered by a button). But when I call this function in other file (not viewController.m), such as
ViewController *viewcontroller = [[ViewController alloc] init];
[viewcontroller changeView];
It doesn't work. Anyone can give a clue to solve this or an alternative way? (ps. I'm testing on iPad.)
You can switch views with this function:
YourView *screen = [[YourView alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
[screen release];
You can set the modaltransitionstyle to your willings.
Don't forget to import your headerfile at the top of your class.
#import "YourView.h"
Related
My first view contains a button and after pressing a button, i will end up with the second view. what I have done is
FirstViewController.m
- (void)pushToWishList {
SecondViewController *controller = [[SecondViewController alloc] initWithNibName:#"SecondView" bundle:nil ];
[UIView beginAnimations:#"flipping view" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[self.view addSubview:controller.view];
[UIView commitAnimations];
}
SecondViewController is an UIViewController such that :
SecondViewController.h :
#interface SecondViewController :
UIViewController
SecondViewController.m
self.navigationController.navigationBarHidden = NO;
self.navigationItem.hidesBackButton = NO;
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
What is happening is the top navigation is disppearing and the UITableView in my second view can not be scrolled up and down. My program is crashed whenever I try to do this.
Please advice me on this issue. Any comments are welcomed here.
First off its easier to not addSubview: just use this code to turn your page:
- (IBAction)myCart:(id)sender; {
viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:viewController animated:YES];
}
This will allow your tableView to scroll! Also, your method return type is void, if you are using a button to call this method, you need to change the return type to IBAction which works for buttons.
I am wanting to animate my view change, however I am not sure where to put my animation since I have a UITabBarController controlling which view I am on and what I can switch too.. This is being declared in my appdelegate.m file as per the xcode template.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Or their is the area in each viewcontroller where everything gets loaded... Im thinking it might be better to initialize the animation in here.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Super", #"Super");
self.tabBarItem.image = [UIImage imageNamed:#"SuperIcon"];
}
return self;
}
As I have not done alot of animation with views before I am woundering where I could declare this peice of code that will hopefully animate my view change when a UITabBarButton is touched....
[UIView transitionWithView:containerView
duration:0.2
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{ [fromView removeFromSuperview]; [containerView addSubview:toView];
completion:NULL];
or have I missed the boat completely and is their somewhere else I should be doing this?
Try the following code in the viewWillAppear method
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self.view cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView commitAnimations];
This worked for me for a tabBar controller based app.
Try that animation in code in viewWillAppear method
Sample on GitHub
I have a strange problem.
I have Navigation-based app with two UIViewControllers and Curl effect for transition between these.
I add to bar button and add custom action:
-(IBAction)pushPage
{
NSLog(#"push page");
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[UIView beginAnimations:#"animation" context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.navigationController.view cache:NO];
[self.navigationController pushViewController:secondView animated:NO];
[UIView commitAnimations];
[secondView release];
}
In SecondView I have a button with action:
-(IBAction)back
{
[imageAnimationIssueAppDelegate backPage];
}
Method backPage in AppDelegate:
-(void)backPage
{
NSLog(#"backPage");
[UIView beginAnimations:#"animation" context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.navigationController.view cache:NO];
[self.navigationController popToRootViewControllerAnimated:NO];
[UIView commitAnimations];
}
PROBLEM:
On the RootView I have UIImageView with animation:
- (void)viewDidLoad
{
[super viewDidLoad];
animImage.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"2.png"],
nil];
animImage.animationDuration = 0.5;
animImage.animationRepeatCount = 0;
}
I start it in viewDidAppear:
- (void)viewDidAppear:(BOOL)animated
{
NSLog(#"First view - viewDidAppear");
[super viewDidAppear:animated];
[animImage startAnimating];
}
I stop it in viewDidDisapear:
- (void)viewDidDisappear:(BOOL)animated
{
NSLog(#"First view - viewDidDisappear");
[super viewDidDisappear:animated];
[animImage stopAnimating];
}
When I back from SecondView animation is stoped! But [animImage isAnimating] say YES!
It doesn't start witout update screen - you can click to button on the bottom and see it!
Is it bug?
Animation works without custom transitions.
When I set animated:YES for push or pop view, animation works with custom transitions between views.
Why?
Is there simple way to update displaying view to start animation?
I think you should have to write
[yellowBatterfly startAnimating];
in viewDidLoad method. and change method from
viewDidAppear to viewWillAppear and
viewDidDisappear to viewWillDisappear.
Hope It will works...
I think you have to perform the animations on the view using:
[UIView commitAnimations]
I have a UIViewController, a "switcher" that will basically just rotate a view from one to another.
It all works great, except that the view that I am transitioning to is a UIViewController which holds a UITableViewController. For some reason, when the animation "flips", the navigation bar is invisible, and once the animation completes the navigation bar just appears.
It really doesn't look good and I was wondering if anyone knew why I might be seeing this and how I could fix it?
Thanks,
--d
EDIT: Adding some code by request!
Switcher viewDidLoad method - Currently Initializing both of the ViewControllers because I thought it may help
[super viewDidLoad];
LogoView *logoController = [[LogoView alloc] init];
self.logoView = logoController;
[self.view insertSubview:logoView.view atIndex:0];
[logoController release];
MainController *vController = [[MainController alloc] init];
self.controller = vController;
[vController release];
switchTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(switchViews) userInfo:nil repeats:NO];
Switcher switchViews method
[UIView beginAnimations:#"View Flip" context:nil];
[UIView setAnimationDuration:.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
if (self.controller.view.superview == nil)
{
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[controller viewWillAppear:YES];
[logoView viewWillDisappear:YES];
[logoView.view removeFromSuperview];
[self.view insertSubview:controller.view atIndex:0];
[logoView viewDidDisappear:YES];
[controller viewDidAppear:YES];
}
[UIView commitAnimations];
MainController viewDidLoad method
CGRect frame = CGRectMake(0, 0, 320, 410);
FirstLevelController *controller = [[FirstLevelController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:controller];
navController.view.frame = frame;
navController.navigationBar.tintColor = [UIColor blackColor];
[controller release];
[self.view addSubview:navController.view];
Inside the FirstLevelController I just add the items to the table view... I've tried adding a navController.title = #"Home", but I am not even seeing the black navbar without text... it's just a big empty space.
Help is muchly appreciated!
Hah! I changed the animation "cache" from YES to NO, and it fixed it! Yay!
I am using the following code:
- (void)flip
{
MailComposerViewController *mailView = [[MailComposerViewController alloc] init];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:window
cache:YES];
[mtController.view removeFromSuperview];
//[self.window addSubview:[mailComposer view]];
[self presentModalViewController:mailView animated:YES];
[UIView commitAnimations];
[mailView release]
}
here mtController is a navigation controller (XIB file). I removed it and I add mailview, but the simulator does not show it. What am I doing wrong?
What are you trying to do? Less Vague Questions Is A Good Thing™. Are you trying to use a standard Mail compose controller and use it to flip over instead of present normally?
If so, you can do this:
MailComposerViewController *mailView = [[[MailComposerViewController alloc] init] autorelease];
mailView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:mailView animated:YES];