Is it possible to make a custom push view controller animation like the image? The basic idea is to push the view normally, but without moving the current view (that calls the pushViewController...).
Sure, that can be done. I made a project where I did it like this:
-(void)slideInController:(RDSlideController *) next {
next.presentingVC = self;
next.view.frame = CGRectMake(self.view.frame.origin.x + 320, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
[self.view.window addSubview:next.view];
[UIView animateWithDuration:slideTime animations:^{
next.view.frame = self.view.frame;
} completion:^(BOOL finished) {
self.view.window.rootViewController = next;
}];
}
The view controllers that I slid in were subclasses of RDSlideController (so I could have the presentingVC property and some other things), but that's not really necessary. Basically, you just instantiate the new controller, set its view's frame to be off screen to the right, add that view to the window, then animate the frame to the current view's frame. Finally, you switch out the view controllers so that the new one is now the root view controller of the window.
We can achieve this by adding subview and animating view which we want to add.
When we are adding give animation to move next view from left edge to right edge. And vise versa with removing view.
Related
I am trying to emulate the way TweetBot/NetBot animates the tabBar in after a push from the tableView of Accounts action. When the view is fully pushed, only then does the taBar animate in from the bottom. I have tried all sorts of hide/show methods and all seem to fail when it comes to the "show" part.
Does anyone have a suggestion as to how this can be done?
First of all, I presume you are not using a UITabViewController since it cannot be pushed into a UINavigationController stack, so I think you are using a standalone UITabBar embedded in a UIViewController. Is this assumption right?
Try with this code (I didn't try it).
- (void)viewDidAppear {
[super viewDidAppear];
// Calls showTabBar method after SOME_DELAY. You can also call directly [self showTabBar] if you want zero delay.
[self performSelector:#selector(showTabBar) afterDelay:SOME_DELAY];
}
- (void)showTabBar {
// Before the animation begins, your UITabBar must be outside the view controller's view frame.
CGRect tabBarFrame = CGRectMake(0,
CGRectGetHeight(self.view.bounds),
CGRectGetWidth(self.view.bounds),
CGRectGetHeight(self.tabBar.frame);
self.tabBar.frame = tabBarFrame;
// Let's start with the animation, setting a new frame for tab bar inside an animation block
[UIView animateWithDuration:ANIMATION_DURATION animations:^{
// Change origin Y. It assumes that the height of self.tabBar is right, otherwise put the height you want instead of CGRectGetHeight(self.tabBar.frame).
tabBarFrame.origin.y = CGRectGetHeight(self.view.bounds) - CGRectGetHeight(self.tabBar.frame);
self.tabBar.frame = tabBarFrame;
}];
}
I am switching between views using view animations, its works but I am having an issue with interface orientation.
I have two views on window.
authenticationViewCont
mainViewCont
Both have a button, when button clicked on authenticationViewCont I remove it and show mainViewCont and vice versa.
Once I addSubview the authenticationViewCont.view and putting device in portrait mode then removed it by removeFromSuperview then I change device orientation to landscape in my hands then again addSubview the authenticationViewCont. It first displayed animating in portrait and changing orientation after animation.
-(void)mainToAuthentication {
CGRect originalFrame = authenticationViewCont.view.frame;
CGRect modifiedFrame = originalFrame;
modifiedFrame.origin.y = originalFrame.size.height;
// made view out from screen
authenticationViewCont.view.frame = modifiedFrame;
// add sub view on top of other views
[self.window addSubview:authenticationViewCont.view];
// transiting view from bottom to center of screen
[UIView animateWithDuration:0.5
animations:^{ authenticationViewCont.view.frame = originalFrame; }
completion:^(BOOL finished){ mainViewCont.view removeFromSuperview; }];
}
-(void)authenticationToMain {
CGRect originalFrame = mainViewCont.view.frame;
CGRect modifiedFrame = originalFrame;
modifiedFrame.origin.y = -originalFrame.size.height;
// made view out from screen
mainViewCont.view.frame = modifiedFrame;
// add sub view on top of other views
[self.window addSubview:mainViewCont.view];
// transiting view from top to center of screen
[UIView animateWithDuration:0.5
animations:^{ mainViewCont.view.frame = originalFrame; }
completion:^(BOOL finished){ authenticationViewCont.view removeFromSuperview; }];
}
How can I make it to display in current interface orientation instead of old interface orientation in which it was removeFromSuperview?
I think the problem here is that you are initialliy adding one viewController.view ontop of another then removing the old one.
The problem with window only expects one rootViewController. So the window will only pass rotation events onto the first controller. Meaning your second viewController will not get rotation events until the completionsBlock gets called.
The way I would get around this is to put this switching code inside of a rootViewController that is on the window. Then whenever you do your switching you can pass in the current rotation of the rootviewController and have your authenticationViewController set itself up based on the orientation you pass it
I am presenting a modal view controller. If it matters, it is scrolling up from the bottom. How can I control what portion of the screen it occupies?
EDIT: I have the following in the modal view controller. It's not helping.
- (void)viewDidLoad {
TestResultView *trv = [[TestResultView alloc]initWithTest: [Model m].currentTest];
self.view = trv;
trv.frame = CGRectMake(0, 320, 320, 160);
[trv release];
[super viewDidLoad];
}
You can modify the frame of the view controller, but if you're using UIViewController's -presentModalViewController:animated: method, the view behind will be unloaded once your modal view is finished animating onto the screen (This assumes you're on an iPhone) and you'll see a white screen where your background view should be. iOS assumes that your modal view controller will be a full-screen view controller, and dumps the other view to save memory.
If you really want to show a view over part of the screen, you should instead add the UIView (no UIViewController) to your current UIViewController's view as a subview, and then animate it onscreen yourself. I think something like this would work in your UIViewController class that will present the view:
// Add the view as a subview and position it offscreen just below the current view
UIView *myHalfView = [[UIView alloc] initWithFrame:someAppropriateFrame];
[self.view addSubview:myHalfView];
CGRect offScreenFrame = myHalfView.bounds;
offScreenFrame.origin = CGPointMake(0.0, CGRectGetMaxY(self.view.frame));
// Now animate the view upwards
[UIView beginAnimations:nil context:nil];
// Move the view upwards the height of your sliding view so it's entirely onscreen
myHalfView.center = CGPointMake(myHalfView.center.x, myHalfView.center.y - myHalfView.bounds.size.height);
[UIView commitAnimations];
[myHalfView release];
For bonus points, you could fade the view in by setting
myHalfView.alpha = 0.0;
before the UIView animation block, and setting
myHalfView.alpha = 1.0;
inside the block after animating the center property.
When you're done, you can do something similar but in reverse to slide the view offscreen. You can add an animationDidStop selector to the UIView animation block to be notified when the view has slid off screen so that you can remove it from the view hierarchy.
From an aesthetic point of view, you should also be careful how you do this since having a view slide up is a standard behavior, and if your view looks like a normal view but stops halfway, users may feel (even briefly) that the app has frozen. They'll figure it out, but it will leave a bad feeling about your app if not handled carefully. Mainly, I would avoid using standard full-screen cues like including a UINavigationController at the top of your view to help users understand what's going on. Half-sheets tend to be UIActionSheets on the iPhone, so think in that direction.
That is nice, the above accepted answer explains a nice hack to present subViews which feel like ModalViews, but what if it is an iPad, and i can indeed give it a modalViewController which doesnt cover the entire screen.
In case of iPads, I dont think the underneath view will be unloaded. ( because there are options where we can present the modalView on iPads, which dont cover the entire screen )
ModalViewController in the end is a controller itself, and like any other controller has a root view, whose properties can be editted, if we can get hold of it.
Here is what will give you a custom frame of the ModalView :
MyViewController *viewController = [[MyViewController alloc] init];
viewConroller.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:viewController animated:YES];
//superView of viewController's view is modalViewController's view, which we were after
viewController.view.superview.frame = CGRectMake(x,y,w,h);
//x y w h - can have desired values.
I would add to #dsaw's answer that the superview of the modal view does not seem to rotate its coordinate system in landscape mode. Here is the code that I used in my own app:
MyViewController* modalVC = [[MyViewController alloc] init];
modalVC.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:modalVC animated:NO];
CGRect r = CGRectMake(self.view.bounds.size.width/2 - 236,
self.view.bounds.size.height/2 - 130,
472, 260);
r = [self.view convertRect:r toView:modalVC.view.superview.superview];
modalVC.view.superview.frame = r;
While the superview may not rotate itself with the iPad, it does seem to do the right thing and keep the modal view centered if I rotate the iPad after showing the modal view.
At some part of my code I am using this line
[[self navigationController] pushViewController:myController animated:YES];
This works perfectly and pushes a view, coming from bottom, over the current one, covering the last one completely.
I am wondering if there's a way to make it cover just part of screen. Let's say, just the half bottom of the screen...
Is it possible? I have tried to change the controller's view frame but the size kept coming full screen.
thanks.
Instead of using a new view controller modally, you could add a new subview to your existing view, using the same view controller.
You can do the "slide in" animation with something like:
[self.view addSubview: newView];
CGRect endFrame = newView.frame; // destination for "slide in" animation
CGRect startFrame = endFrame; // offscreen source
// new view starts off bottom of screen
startFrame.origin.y += self.view.frame.size.height;
self.newImageView.frame = startFrame;
// start the slide up animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.3];
newView.frame = endFrame; // slide in
[UIView commitAnimations];
You can do it in a limited fashion with a modal view controller. Check out the presentation options available under UIModalPresentationStyle in the apple docs.
You will need to be on iOS 3.2 or above to do a modal view controller.
In my test application (im learning) i have 2 view controllers.
on the first view i have button "go to second view".
what i want to do :
when user click the "go to second view", the first view move left and go out of the screen
and the second view will appear from the right and replace the first view.
now, this animation is happen when pushing and poping with navigate controller.
my question :
how can i do the same animation, without nav controller ?
You can add a view off screen to the right, and then you can animate it to a new frame on-screen. Another common use for this method is to animate modal menu views from the bottom. You can also animate other properties of the view, such as the alpha value to make a view disappear/reappear.
// the size of the screen minus the Status Bar
#define SCREEN_FRAME [[UIScreen mainScreen] applicationFrame]
// add the full-screen view offscreen to the right
CGRect frame = CGRectMake(SCREEN_FRAME.size.width,
SCREEN_FRAME.origin.y,
SCREEN_FRAME.size.width,
SCREEN_FRAME.size.height);
UIView *view = [[[UIView alloc]initWithFrame:frame]autorelease];
[self.view addSubview view];
// this is the frame the view will end on after the animation
CGRect newFrame = CGRectMake(SCREEN_FRAME.origin.x,
SCREEN_FRAME.origin.y,
SCREEN_FRAME.size.width,
SCREEN_FRAME.size.height);
// animate the transition
[UIView beginAnimations:nil context: nil];
[UIView setAnimationDuration: .5];
view.frame = newFrame;
[UIView commitAnimations];