animating view change with uitabbarcontroller - iphone

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

Related

Animated Splash Screen in iPhone

I need to implement animated splash screen to the iPhone application. I have seen skype application where same thing is already implemented.
Can anyone has idea how can i implement same thing in my applicatio
You can use sequence of images, here is code :
for(NSInteger i=1;i<=totalImages;i++){
NSString *strImage = [NSString stringWithFormat:#"Activity_%d",i];
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:strImage ofType:#"png"]];
[imageArray addObject:image];
}
splashImageView.animationImages = imageArray;
splashImageView.animationDuration = 0.8;
and just call startAnimation and endAnimation method of UIImageView.
OR
Its very simple...I had used it in to begin my app with splashView.Hope it vil help you.... In AppDelegate.m:
application didFinishLaunchingWithOptions:
UIImage* image=[UIImage imageNamed:#"splash.jpg"];
splashView=[[UIImageView alloc]initWithImage:image];
[window addSubview:splashView];
[window bringSubviewToFront:splashView];
[self performSelector:#selector(removeSplash) withObject:self afterDelay:2];
[window makeKeyAndVisible];
To remove splashView:
-(void)removeSplash{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:window cache:YES];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
[splashView removeFromSuperview];
[UIView commitAnimations];
[window addSubview:viewController.view];
}
We can show a .gif image in webView and it looks perfect!
Take a new UIViewController class named SplashView with XIB and then add an UIWebView with (320.0, 480.0) frame, hidden statusbar also.
In SplashView.h
#import <UIKit/UIKit.h>
#interface SplashView : UIViewController
#property(nonatomic, retain)IBOutlet UIWebView *webView;
#end
In SplashView.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *imagePath = [[NSBundle mainBundle] pathForResource: #"animated" ofType: #"gif"];
NSData *data = [NSData dataWithContentsOfFile:imagePath];
[self.webView setUserInteractionEnabled:NO];
[self.webView loadData:data MIMEType:#"image/gif" textEncodingName:nil baseURL:nil];
}
This is about SplashView class. Now come to your appdelegate's class.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
splashView = [[SplashView alloc]initWithNibName:#"SplashView" bundle:nil];
[self.window addSubview:splashView.view];
[self performSelector:#selector(changeView) withObject:nil afterDelay:3.0];
[self.window makeKeyAndVisible];
return YES;
}
-(void)changeView
{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[splashView.view removeFromSuperview];
[self.window setRootViewController:self.viewController];
}
Take a UIView & an Imageview into it. Give all your images to ImageView to animate.
-(void)viewDidLoad
{
NSArray *arrImage=[NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
nil];
imgVw.backgroundColor=[UIColor purpleColor];
imgVw.animationImages=arrImage;
imgVw.animationDuration=2.5;
imgVw.animationRepeatCount=1;
[imgVw startAnimating];
[NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:#selector(animateNext) userInfo:nil repeats:NO];
}
This will show up you application icon.
After that you will show the controls those would be hidden by default and animate them from bottom to up.
-(void)animateNext
{
lbl.hidden = NO;
btn.hidden = NO;
txt1.hidden = NO;
txt2.hidden = NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.9];
lbl.frame=CGRectMake(lbl.frame.origin.x,lbl.frame.origin.y - 150,lbl.frame.size.width,lbl.frame.size.height);
imgVw.frame = CGRectMake(imgVw.frame.origin.x, imgVw.frame.origin.y - 150, imgVw.frame.size.width, imgVw.frame.size.height);
txt1.frame = CGRectMake(txt1.frame.origin.x, txt1.frame.origin.y - 150, txt1.frame.size.width, txt1.frame.size.height);
txt2.frame = CGRectMake(txt2.frame.origin.x, txt2.frame.origin.y - 150, txt2.frame.size.width, txt2.frame.size.height);
btn.frame = CGRectMake(btn.frame.origin.x, btn.frame.origin.y - 150, btn.frame.size.width, btn.frame.size.height);
[UIView commitAnimations];
}
Hope this help...
Try this
Appdelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UIImageView *splashView;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
- (void)startupAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
// Make this interesting.
splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
splashView.image = [UIImage imageNamed:#"Default.png"];
[self.window addSubview:splashView];
[self.window bringSubviewToFront:splashView];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.window cache:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(startupAnimationDone:finished:context:)];
splashView.alpha = 0.0;
splashView.frame = CGRectMake(-60, -85, 440, 635);
[UIView commitAnimations];
return YES;
}
- (void)startupAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[splashView removeFromSuperview];
}
you need to start your app with a viewcontroller,with an uiimageview in it..Create a series of .png images to be subjected to the UIImageView check how to animate array of images in uiimageview. Further to dismiss it once animation over you would need to implement a protocol that will inform your starting first viewcontroller of your app to dismiss the animation
- (void) welcomeScreen
{
//Welcome Screen
UIImageView* welcome = [[[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)]autorelease];
welcome.image = [UIImage imageNamed:#"img.png"];
[window addSubview:welcome];
[window bringSubviewToFront:welcome];
//Animation Effects (zoom and fade)
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:window cache:YES];
[UIView setAnimationDelegate:welcome];
[UIView setAnimationDidStopSelector:#selector(removeFromSuperview)];
//set transparency to 0.0
welcome.alpha = 0.0;
//zoom effect
welcome.frame = CGRectMake(-60, -60, 440, 600);
[UIView commitAnimations];
}
yes simple in AppDelegate class first defile imageview like bellow..
#interface AppDelegate : UIResponder
{
UIImageView *splashView;
}
and in .m file...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
splashView.image = [UIImage imageNamed:#"Default"];
[self.window addSubview:splashView];
[self performSelector:#selector(loadViewIphone) withObject:nil afterDelay:2.0];
}
[self.window makeKeyAndVisible];
return YES;
}
-(void)loadViewIphone
{
[splashView removeFromSuperview];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:kCATransitionFade];
[animation setDuration:0.5];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:
kCAMediaTimingFunctionEaseInEaseOut]];
[[self.window layer] addAnimation:animation forKey:#"transitionViewAnimation"];
}
i hope this help you..
:)

Switching View through a function

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"

UITableView Navigation Bar "Flickers" on Animation

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!

Switching from a normal view to a TabBar Controller

I build my first iPhone application and I have a problem with switching views.
First, I have two views (login, registration) which switch via "presentModalViewController:animated:".
But if someone logged in, there must be a new kind of view. I wanna have an UITabBar at the bottom (tab bar controller). But this does not work. I tried to create a new AppDelegate, so that I can use tutorials like this one which need a AppDelegate:
http://www.youtube.com/watch?v=LBnPfAtswgw&feature=player_embedded
The switch to the new controller is done like this:
startViewController = [[StartViewController alloc] initWithNibName:#"StartView" bundle:nil];
[UIView beginAnimations:#"View Curl" context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[self.view addSubview:startViewController.view];
[UIView commitAnimations];
The screen is white, because the shown view is the UIView in my StartView.xib. There I have the new AppDelegate, File's owner, View, TabBarController. But only the UIView is loaded and not the TabBarController.
Do you have an idea how I could this problem?
Thanks & Best Regards.
I might suggest you start with a TabBarController, and if the username/password is not set, the active ViewController executes presentModalViewController:animated: to display the login/registration viewsControllers in modal mode (hidding the underliying TabBarController).
Here is some sample code for doing it programmatically.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
[window setBackgroundColor:[UIColor whiteColor]];
tabBarController = [[UITabBarController alloc] init];
aViewController = [[aViewController alloc] init];
UINavigationController *aNavController = [[[UINavigationController alloc] initWithRootViewController:aViewController] autorelease];
aNavController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
[aViewController release];
tabBarController.viewControllers = [NSArray arrayWithObjects: aNavController, nil];
// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
if(userNotLoggedIn){
[self displayLoginViewController];
}
[window makeKeyAndVisible];
}
- (void)displayLoginViewController {
LoginViewController *controller = [[LoginViewController alloc] init];
// setup controller
[self.tabBarController presentModalViewController:controller animated:NO];
[controller release];
}

UIView continues to exist after removeFromSuperview

I add a firstView in the AppDelegate :
#import "TestViewAppDelegate.h"
#import "MainViewController.h"
#implementation TestViewAppDelegate
#synthesize window;
#synthesize mainViewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
MainViewController *aController = [[MainViewController alloc] initWithNibName:#"MainView" bundle:nil];
self.mainViewController = aController;
[aController release];
self.mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
[window makeKeyAndVisible];
[window addSubview:mainViewController.view];
}
- (void)dealloc {
[mainViewController release];
[window release];
[super dealloc];
}
Then I want to switch to the secondView :
#import "MainViewController.h"
#import "SecondViewController.h"
#implementation MainViewController
#synthesize mainViewController, secondViewController;
- (IBAction)viewSwitch
{
SecondViewController *second = [[SecondViewController alloc] initWithNibName:#"SecondView" bundle:nil];
self.secondViewController = second;
[second release];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[mainViewController.view removeFromSuperview];
[self.view addSubview:secondViewController.view];
[UIView commitAnimations];
}
- (void)dealloc {
[mainViewController release];
[secondViewController release];
[super dealloc];
}
#end
And then the same thing for switching from secondView to firstView…
The problem is when I switch the view I thought to release is always visible and don't disappear.
Download the full code
[mainViewController.view removeFromSuperview];
[self.view addSubview:secondViewController.view];
What is "self" here? And why does the MainViewController class have a property named mainViewController? Exploring those questions will likely lead you to the answer here.
A better approach might be to create one top-level parent view controller that you add to the window. It should have a reference to both of your other view controllers and do the swapping instead. If you want your button inside each view to cause the swapping, you could just have your button IBAction for each post a notification that your top level view registers for and responds to.