I have a UIViewController (call it SubUIVC) that was loaded via my main UIViewController (call it MainUIVC) using my showNewView method and when I am done with the SubUIVC, I return to MainUIVC using my goBack method.
What I'd like to do is execute some code in MainUIVC once the goBack method is completed and my MainUIVC.view has been loaded.
Can anyone help me with doing this? Below are the two methods I made reference to above.
-(void) showNewView:(UIViewController*)showView{
UIView *currentView = self.view;
// get the the underlying UIWindow, or the view containing the current view
UIView *theWindow = [currentView superview];
UIView *newView = showView.view;
// remove the current view and replace with newView
[theWindow addSubview:newView];
// set up an animation for the transition between the views
CATransition *animation = [CATransition animation];
[animation setDuration:WIPE_ANIMATION_DURATION];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[theWindow layer] addAnimation:animation forKey:#"SwitchToNewView"];
}
-(IBAction)goBack:(id)sender{
UIView *currentView = self.view;
// get the the underlying UIWindow, or the view containing the current view
UIView *theWindow = [currentView superview];
// remove the current view and replace with superview
[currentView removeFromSuperview];
// set up an animation for the transition between the views
CATransition *animation = [CATransition animation];
[animation setDuration:WIPE_ANIMATION_DURATION];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[theWindow layer] addAnimation:animation forKey:#"SwitchToNewView"];
}
Have a BOOL property in the MainController than get's set by the goBack: function.
In the main controller's viewDidAppear:, check that BOOL to see if the view's just appeared as a result of goback: being called and do your thing. Obviously you should then reset that flag.
Related
I'm changing layouts when selecting a cell in a uicollectionview and have added a custom animation to the collectionview layer
currently I'm using this code:
[self.collectionView performBatchUpdates:^{
CATransition *transition = [CATransition animation];
transition.duration = 0.5f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionMoveIn;
[self.collectionView.layer addAnimation:transition forKey:nil];
[self.collectionView setCollectionViewLayout:self.horizontalPagingLayout animated:NO];
self.galleryInHorizontalScrollMode = YES;
[self.collectionView setPagingEnabled:YES];
} completion:^(BOOL finished) {
[self reloadData];
}];
I would like to change the animation style (transition.type) such that the selected cell will "grow" (or shrink) to the size of the final cell size defined in the collectionview layout
How can this be done?
I've tried solutions as suggested here but they looked very poor
Many people use this method to add animation on switching views. When I try to add animation effect on adding a subview, the view always flash once。 The reason is that the view add subview immediately at the beginning, after that, the animation starts.
CATransition *transition = [CATransition animation];
transition.duration = 0.35;
transition.removedOnCompletion = NO;
transition.fillMode = kCAFillModeForwards;
transition.type = kCATransitionFade;
transition.subtype = kCATransitionFromBottom;
transition.delegate = self;
[[animationView layer] addAnimation:transition forKey:#"switchView"];
OtherViewController *otherController = [[OtherViewController alloc]init:cityID];
self.myOtherViewController = otherController;
[otherController release];
[self.view insertSubview:myOtherViewController.view atIndex:1];
What's the reasons of problem? How to implement the animation of loading new view without flash?
After modified fillMode to kCAFillModeBoth, it works.
transition.fillMode = kCAFillModeForwards;
I have three views on same viewController and I need to switch beetween them with transitions. Which is the best method to do it? Thank you
Well I am not sure if you have made the question clear .I Guess you want to show the views one at a time.In that case :-
1>You can add the views in a horizontal UIScrollView with its frame size equal to the screen or whatever area you want(width should be equal to width of screen..shown 1 at a time) along with a UIPageControl.
2>The other way is to use the Methods:-
[self.view bringSubViewToFront:yourSubView];
[self.view sendSubViewToBack:yourSubView ];
according to your requirements.
3>You can use Animation to switch between views.
[UIView animateWithDuration:0.7
delay:1.2
options: UIViewAnimationCurveEaseOut
animations:^{
CGRect frame=assignAframeToSwitch;
yourView.frame = frame;
}
completion:^(BOOL finished){
NSLog(#"Done!");
}];
OR
_Weak CATransition *transition;
transition = [CATransition animation];
transition.delegate=self;
transition.duration = 0.75;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[[viewcontroller layer] addAnimation:transition forKey:#"transition"];
CATransition is the best way to animate transition
CATransition* trans = [CATransition animation];
[trans setType:kCATransitionPush];
[trans setDuration:0.5];
[trans setSubtype:kCATransitionFromBottom];
// code to change the view//
CALayer *layer = rootViewController.view.layer;
[layer addAnimation:trans forKey:#"Transition"];
Hope it helps.
if you use this bellow code for animation for transitions and then just need to hide and show the view on button click event...
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:kCATransitionFromBottom];
[animation setDuration:1.0];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:
kCAMediaTimingFunctionEaseInEaseOut]];
[[yourViewControllers layer] addAnimation:animation forKey:#"transitionViewAnimation"];
hope,this help you..
:)
The fastest easiest way to do this is with the built in block based UIView transition animations. Just look at the documentation for UIView. Its way simpler than the CATransition methods.
In the Google Maps app on iPhone, the toolbar in the buttom is still visible after the modal with a partial curl transition is preformed. When I setup a modal seque in my storyboard with a partial curl, the new view controller is hiding the toolbar from the parant view.
How can I setup a partial curl like the Google Maps app?
Try this code.This will be helpful to you.PLEASE DON'T HESITATE TO TRY THIS CODE.
You will need to import a QuartzCore.framework.
locationMapView is your MKMapView and curlView is your second UIView
- (IBAction)curlButtonPressed:(id)sender {
if (isCurlStarted == NO) {
[UIView animateWithDuration:1.0
animations:^{
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setDuration:0.7];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:#"default"]];
animation.type = #"pageCurl";
animation.fillMode = kCAFillModeForwards;
animation.endProgress = 0.65;
[animation setRemovedOnCompletion:NO];
[locationMapView.layer addAnimation:animation forKey:#"pageCurlAnimation"];
[locationMapView addSubview:curlView];
;}
];
isCurlStarted = YES;
}else{
[self curlDownPressed:curlDownButton];
}
}
- (IBAction)curlDownPressed:(id)sender {
[UIView animateWithDuration:1.0
animations:^{
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setDuration:0.7];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:#"default"]];
animation.type = #"pageUnCurl";
animation.fillMode = kCAFillModeForwards;
animation.startProgress = 0.35;
[animation setRemovedOnCompletion:NO];
[locationMapView.layer addAnimation:animation forKey:#"pageUnCurlAnimation"];
[curlView removeFromSuperview];
;}
];
isCurlStarted = NO;
}
You'll probably need to go with OpenGL ES for this one. Core Animation exposes the ability to work with layers, even in 3-D, but all layers are just rectangles and they are manipulated as such. You can animate the flipping of a layer about an axis, even with a perspective distortion, but the kind of curving you want to do is more complex than you can manage using the Core Animation APIs.
You might be able to split your image up into a mesh of tiny layers and manipulate each using a CATransform3D to create this curving effect, but at that point you might as well be using OpenGL ES to create the same effect.
I Am creating an application and i want to achieve push effect but without using navigation controller.Does someone knows a core animation source code to achieve this? I have a view hierarchy like this.A main controller which has an header image with 4 buttons on it.I mean it will be displayed all the time whatever view in front of screen is.So i use add subview methods.Now in some view i want to navigate further when cliking on table view cell but unabvle to figure out how to achieve that navigation animation?
here is the code to adding subviews....
-(IBAction)ShowDashBoardContentView
{
if(ButtonDashBoard.selected==YES)
[ButtonDashBoard setSelected:YES];
else
{
[ButtonDashBoard setSelected:YES];
[ButtonFreebies setSelected:NO];
[self.FreebiesViewMainObject.view removeFromSuperview];
[ButtonConnect setSelected:NO];
[self.ConnectContentViewObject.view removeFromSuperview];
[ButtonDeals setSelected:NO];
[self.DealsViewMainObject.view removeFromSuperview];
}
if(DashBoardContentView==nil)
{
DashBoardContent *vController =[[DashBoardContent alloc] initWithNibName:#"DashBoardContent" bundle:[NSBundle mainBundle]];
vController.view.frame=CGRectMake(0, 90, 320, 370);
self.DashBoardContentView=vController;
[vController release];
}
[self.DashBoardContentView viewWillAppear:YES];
[self.view addSubview:[self.DashBoardContentView view]];
[self BringSubviewsToFront];
}
-(IBAction)ShowConnectContentView
{
if(ButtonConnect.selected==YES)
[ButtonConnect setSelected:YES];
else
{
[ButtonConnect setSelected:YES];
[ButtonFreebies setSelected:NO];
[self.FreebiesViewMainObject.view removeFromSuperview];
[ButtonDashBoard setSelected:NO];
[self.DashBoardContentView.view removeFromSuperview];
[ButtonDeals setSelected:NO];
[self.DealsViewMainObject.view removeFromSuperview];
}
if(ConnectContentViewObject==nil)
{
ConnectContentView *vController =[[ConnectContentView alloc] initWithNibName:#"ConnectContentView" bundle:[NSBundle mainBundle]];
vController.view.frame=CGRectMake(0, 90, 320, 370);
self.ConnectContentViewObject=vController;
[vController release];
}
[self.ConnectContentViewObject viewWillAppear:YES];
[self.view addSubview:[self.ConnectContentViewObject view]];
[self BringSubviewsToFront];
}
-(IBAction)ShowDealsView
{
if(ButtonDeals.selected==YES)
[ButtonDeals setSelected:YES];
else
{
[ButtonDeals setSelected:YES];
[ButtonFreebies setSelected:NO];
[self.FreebiesViewMainObject.view removeFromSuperview];
[ButtonDashBoard setSelected:NO];
[self.DashBoardContentView.view removeFromSuperview];
[ButtonConnect setSelected:NO];
[self.ConnectContentViewObject.view removeFromSuperview];
}
if(DealsViewMainObject==nil)
{
DealsViewMain *vController =[[DealsViewMain alloc] initWithNibName:#"DealsViewMain" bundle:[NSBundle mainBundle]];
vController.view.frame=CGRectMake(0, 90, 320, 370);
self.DealsViewMainObject=vController;
[vController release];
}
[self.DealsViewMainObject viewWillAppear:YES];
[self.view addSubview:[self.DealsViewMainObject view]];
[self BringSubviewsToFront];
}
-(IBAction)ShowFreebiesView
{
if(ButtonFreebies.selected==YES)
[ButtonFreebies setSelected:YES];
else
{
[ButtonFreebies setSelected:YES];
[ButtonDeals setSelected:NO];
[self.DealsViewMainObject.view removeFromSuperview];
[ButtonDashBoard setSelected:NO];
[self.DashBoardContentView.view removeFromSuperview];
[ButtonConnect setSelected:NO];
[self.ConnectContentViewObject.view removeFromSuperview];
}
if(FreebiesViewMainObject==nil)
{
FreebiesViewMain *vController =[[FreebiesViewMain alloc] initWithNibName:#"FreebiesViewMain" bundle:[NSBundle mainBundle]];
vController.view.frame=CGRectMake(0, 90, 320, 370);
self.FreebiesViewMainObject=vController;
[vController release];
}
[self.FreebiesViewMainObject viewWillAppear:YES];
[self.view addSubview:[self.FreebiesViewMainObject view]];
[self BringSubviewsToFront];
}
-(void)BringSubviewsToFront
{
[self.view bringSubviewToFront:HeaderView];
[self.view bringSubviewToFront:ButtonDeals];
[self.view bringSubviewToFront:ButtonDashBoard];
[self.view bringSubviewToFront:ButtonConnect];
[self.view bringSubviewToFront:ButtonFreebies];
}
Now the DealsViewMainObject contains a tableview and i need to navigate further from here.also while removing view from superview how to animate like popviewcontroller animated?
BTW thanks for the answer Felixyz.
You can use a CATransition, along these lines:
// Get index of view to be removed (oldSubView)
NSUInteger index;
for(index = 0; [subViews objectAtIndex:index] != oldSubView; ++index) {}
// Remove old view
[oldSubView removeFromSuperview];
// Inser new view at right index
[self insertSubview:newSubView atIndex:index];
// Create the transition
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft;];
[animation setDuration:0.5f];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[self layer] addAnimation:animation forKey:#"pushIn"];
You don't have to tell the transition which views to push, it will do it automatically (since you removed and inserted views).
Have a look at this example code from Apple: how to perform transitions between two views using built-in Core Animation transitions.
EDIT (in response to comments):
This code should be used inside a UIView subclass. If you want to use it in a UIViewController subclass, you have to change self to self.view everywhere. You would simply put this code into a separate method with a signature like this:
-(void)replaceView:(UIView*)oldSubView withView:(UIView*)newSubView;
You put that method into your class, and call it whenever you want to replace one view with another. You need to have a reference to these views, obviously.
My best guess is that you should replace this code:
[self.view addSubview:[self.FreebiesViewMainObject view]];
[self BringSubviewsToFront];
with
[self replaceView:self.rootView withView:freebiesViewMainObject.view];
But you have to work on this a bit more to get all the pieces to fit together. Really, looking at some good example code like the one I linked to, and really understanding it, is a lot better then to get a few code snippets off of SO and try to throw them into your code.
You could use a UINavigationController anyway, with its property navigationBarHidden set to YES, and use your custom UIView to call pushViewController:animated: and popViewControllerAnimated: or popToRootViewControllerAnimated: to drive your animations.
If the only thing you want is the slide-in and slide-out animations, I think this would be much easier to write.