+ (void) AnimateSwitchingWithParent: (UIViewController *) ParentController From: (UIViewController *) From To: (UIViewController* ) To {
To.view.frame = ParentController.view.bounds;
[UIView transitionFromView:From.view toView:To.view
duration:1
options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
ParentController.view = To.view;
[To viewWillAppear:true];
}
this is my function to make animation that will be called if want to change view from 1 view to another view, but I have problem, I have a viewController named filter, at there I have Button called Reset that will reset all of content inside
but to show that the reset have done, I want to call
[self AnimateSwitchingWithParent:self From:self To:self];
but the result is My View was blank. how can it be? any one have another way?
If you have to flip the current view controller's view while resetting content, use the transitionWithView:duration:options:animations:completion: method.
Example
[UIView transitionWithView:self.view
duration:1.0f
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^(void) {
[self resetStuff];
}
completion:nil];
Side Note
You current implementation in the question will be useful to switching views in the view hierarchy. Say From.view is the subview of ParentController.view then the statement below will replace From.view with To.view as the subview of ParentController.view.
[UIView transitionFromView:From.view toView:To.view
duration:1
options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
You definitely don't need to do this,
ParentController.view = To.view;
Doing that will replace the ParentController's view which might be different from what you intended and definitely in contradiction to the transition animation on the line before.
You must definitely not be calling,
[To viewWillAppear:true];
directly.
To my understanding you should be able to remove the last two lines.
Related
I have a navigation controller with a couple of view controllers in it. In the first view controller (the app's initial one) I perform an animation, which moves two UIImageViews around, and fades in some additional ui elements. Now, when I push the next view controller and then go back to the initial one, the transformations are gone and the image views are exactly where they were when loaded from the storyboard. The additional ui elements, however, are still visible and so is the text I've entered in some UITextFields. So the view controller is not entirely reset to its initial state but somehow the performed animations are undone. Can anybody tell me whether this is regular behavior and - if so - how I can preserve the transformations?
A little more disclosure: I'm simply setting off a regular animation upon hitting a button...
[UIView animateWithDuration:0.4
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
self.firstView.center = firstCenter;
self.secondView.center = secondCenter;
}
completion:^(BOOL finished) {
// Upon complection fade in additional ui elements
[UIView animateWithDuration:0.4
animations:^{
self.firstElement.alpha = 1.0;
self.secondElement.alpha = 1.0;
self.thirdElement.alpha = 1.0;
}
completion:^(BOOL finished) {
[self.activityIndicator stopAnimating];
}];
}];
... and then pushing the next view controller after hitting another button.
[self.navigationController pushViewController:_nextViewController animated:YES];
As far as I can see I'm not doing anything funky here, so any help is appreciated.
I've just started objective C and I'm enhancing a current iPhone app. Previous codes were done by someone else. I'm trying to switch views back - and add a page curl animation in, but when I try to go back (removefromsuperview) my screen just goes white. I understand that I'm referring to the wrong view but I have no idea how to refer it to the right one.
-(IBAction)switchBack{
[UIView beginAnimations:#"flipview" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView: self.view.superview cache:YES];
//[self.navigationController popToRootViewControllerAnimated:YES];
[self.view removeFromSuperview];
[UIView commitAnimations];
}
The commented out popToRootViewController allows me to go back to the previous page. Any help would be really appreciated. I'm going crazy with all this views >< Thanks!
Try something like this:
[UIView transitionWithView:self.navigationController.view duration:1.0 options:UIViewAnimationOptionTransitionCurlDown
animations:^{
[self.navigationController popToRootViewControllerAnimated:NO];
}
completion:NULL];
that happened because you're trying to remove the view from its superview, which is not the way it is added before, don't use removeFromSuperView if you didn't use addSubview when showing the view it self.
you should add this code to the parent which calls your current view
viewNotLoggedHome.modalTransitionStyle =
UIModalTransitionStylePartialCurl;
this way you've stated a "partial curl" transition animation for the view u're pushing
and just delete all your animation block on switchBack function so it only consist of
-(IBAction)switchBack
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
Instead of usign self.view which is a reference to your superview, you need to use the name of the view you want to use.
f.e. :
- If you have view1 and view2
- you can delete view1 by using
[view1 removeFromSuperview];
Hey there StackOverflow! I've finished programming my app, and everything is working fine- so I've decided that since the function part is done, I want to start working on form. However, I have a bit of code that's giving me trouble, and I'm not sure why. My app has two views that I switch between. In viewDidLoad, I have these two lines of code:
[PlannerView setScrollEnabled:YES];
[PlannerView setContentSize:CGSizeMake(320, 735)];
and then later, I switch between the main view and planner view when a button is pressed, like this:
if (isPlannerView) {
// [self setView:MainView];
[UIView transitionFromView:PlannerView
toView:MainView
duration:0.5
options:UIViewAnimationOptionTransitionFlipFromRight
completion:^(BOOL finished){
[self setView:TimerView];
}];
isPlannerView = NO;
} else {
[UIView transitionFromView:MainView
toView:PlannerView
duration:0.5
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:^(BOOL finished){
}];
// [self setView:PlannerView];
isPlannerView = YES;
}
Now, when I use hte commented line of code (self setview), it will scroll with no problems. However, when I use UIView transitionFromView, it no longer scrolls. Any idea what is going wrong here?
The documentation for transitionFromView:toView:duration:options:completion: says:
This method modifies the views in their view hierarchy only. It does
not modify your application’s view controllers in any way. For
example, if you use this method to change the root view displayed by a
view controller, it is your responsibility to update the view
controller appropriately to handle the change.
So make sure that viewDidLoad: is actually being called. It also states:
During an animation, user interactions are temporarily disabled for
the views being animated. (Prior to iOS 5, user interactions are
disabled for the entire application.) If you want users to be able to
interact with the views, include the
UIViewAnimationOptionAllowUserInteraction constant in the options
parameter.
So make sure that user-interaction is enabled on the view after the animation is finished.
completion:^(BOOL finished){
[PlannerView setUserInteractionEnabled:YES];
}];
Source: UIView Class Reference
following situation.
i have an app that uses a UINavigationController for navigating.
For a push of a special Navigation Controller i want a custom Animation, a zoom out.
What i have is looking good so far, the only problem is, that the "old" Viewcontroller disappears before the animation starts, so the new viewcontroller zooms out of "nothing" instead of viewing the old viewcontroller in the background.
For better viewing i created a simple sample app: download
Does anybody know, how to animate the new viewcontroller (image3) that the old view controller(image1) stays in the background while animating (image 2)?
/* THIS CANNOT BE CHANGED */
AnimatedViewController *animatedViewController = [[AnimatedViewController alloc] initWithNibName:#"AnimatedViewController" bundle:nil];
/* THIS CAN BE CHANGED */
animatedViewController.view.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView beginAnimations:#"animationExpand" context:NULL];
[UIView setAnimationDuration:0.6f];
animatedViewController.view.transform=CGAffineTransformMakeScale(1, 1);
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
/* THIS CANNOT BE CHANGED */
[self.navigationController pushViewController:animatedViewController animated:NO];
Additional information: my app isn't that simple. i use three20 framework for my app but the pushing and creating of the view controllers is simply what three20 does. i only can hook into the part between (THIS CAN BE CHANGED in my code). i cannot change the code before and after this (except with a lot of researching).
I whipped this up very quickly. The idea is to call pushViewController after the scale animation is completed.
-(IBAction)animateButtonClicked:(id)sender {
animatedViewController = [[AnimatedViewController alloc] initWithNibName:#"AnimatedViewController" bundle:nil];
animatedViewController.view.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.6
animations:^{
[self.view addSubview:animatedViewController.view];
animatedViewController.view.transform=CGAffineTransformMakeScale(1, 1);
}
completion:^(BOOL finished){
[animatedViewController.view removeFromSuperview];
[self.navigationController pushViewController:animatedViewController animated:NO];
}];
}
Ok, one way to do this (a bit ugly) is to do the animation and upon the completion of the animation, do the pushViewController. When you do the animation, you need to animate up what the about-to-be-presented view controller's screen would look like. Since the animation ends with the new view, when the new view comes to the screen, nothing should change because its the same as the just animated view.
I have the following code to do a UIView animation:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:mapView.view cache:NO];
[self addSubview:detailView];
[mapView removeFromSuperview];
[UIView commitAnimations];
It works when I dont add the detailView.
What actually happens is the equivalent of this:
[self addSubview:detailView];
[mapView removeFromSuperview];
i think you have the order wrong. you need to remove the old subview first before adding the new one.
see reference for setAnimationTransition:forView:cache:
you can also use CATransition on the layer of a view. and then use layer animations.
if you specifically want to flip the new view you can also use presentalModalViewController method of uiviewcontroller.
I have seen your code in tutorials all the time and never works for me. I always use this:
UIViewController *mainViewController = [[YourMainViewController alloc] init];
UIViewController *viewControllerToSwitchTo = [[ViewControllerToSwitchTo alloc] init];
[mainViewController presentModalViewController:viewControllerToSwitchTo animated: YES];
you can set the view changing style with:
[setModalTransitionStyle:WhateverModalTransitionStyleYouWant];
but make sure this goes before the transition method.
That code won't work. From Apple's documentation:
If you want to change the appearance
of a view during a transition—for
example, flip from one view to
another—then use a container view, an
instance of UIView, as follows:
Begin an animation block.
Set the transition on the container view.
Remove the subview from the container view.
Add the new subview to the container view.
Commit the animation block.
So the transition should be applied to self not your mapView, and your add/remove are in reverse order.
I had to set the graphics context to UIGraphicsGetCurrentContext()