iOS switch between views of same viewController - cocoa-touch

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.

Related

custom animation in setCollectionViewLayout:animated: to enlarge the selected cell

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

screen flash when addsubview by CATransition

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;

How to animate removeFromSuperview

I animated the appearance of my subview with:
CATransition *transition = [CATransition animation];
transition.duration = 0.3;
transition.type = kCATransitionReveal;
[webView.layer addAnimation:transition forKey:nil];
[self.view addSubview:webView];
But now I want to remove my subView. How can I add animation to do this? Like other CATransition? When to add this? Before or after addSubview?
Well you could do the animation first and on the animationEndListener call removeFromSuperView
[UIView animateWithDuration:0.5
delay:1.0
options: UIViewAnimationOptionCurveEaseOut
animations:^{
yourView.alpha = 0;
}completion:^(BOOL finished){
[yourView removeFromSuperview];
}];

How to setup a partial curl modal not hiding toolbar?

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.

How to execute method after removefromsuperview

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.