Push-Pop kind Animation for UIViewController without using NavigationController - iphone

I want to implement Push/Pop kind of animation in my application. Currently in my application, I'm simply adding & removing viewcontroller using addsubview & removesubview without any animation.
Adding navigation controller will be a major change in application as it will change whole structure of application. Is there any way to implement such kind of animation with using navigation controller.

Try this
first give your subview a frame
secondview.frame=CGRectMake(330, 0, 320, 460);
Then when you are adding it
[self.view addSubView:secondview];
[UIView beginAnimations:#"bringViewDown" context:nil];
[UIView setAnimationDuration:0.2];
firstview.frame=CGRectMake(-330, 0, 320, 460);
secondview.frame=CGRectMake(0, 0, 320, 460);
[UIView commitAnimations];
Hope this helps.....

- (void)viewDidLoad
{
[super viewDidLoad];
viewSecond.frame = CGRectMake(330, 0, 320, 548);
}
- (IBAction)onBtnClick:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
viewFirst.frame = CGRectMake(-330, 0, 320, 548);
viewSecond.frame = CGRectMake(0, 0, 320, 548);
[UIView commitAnimations];
}
- (IBAction)onBtnClick2:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
viewFirst.frame = CGRectMake(0, 0, 320, 548);
viewSecond.frame = CGRectMake(330, 0, 320, 548);
[UIView commitAnimations];
}

Implement custom container controller refer Session 102 - Implementing UIViewController Containment in WWDC 2011

Add subview with push animation
//push animation
CATransition *transition = [CATransition animation];
transition.duration = 0.5;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.view.layer addAnimation:transition forKey:nil];
[self addSubview:subView];
and remove subview with pop animation
//pop animation
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[self.superview layer] addAnimation:animation forKey:#"SlideView"];
[self removeFromSuperview];

Related

How to add a subview with a presentmodalview type animation?

I have an application in which I need to
add a subview to a view with presentmodalview type animation
But in
CATransition
I didn't find any type of transition like that. Also in
UItransitionstyle
Can anybody guide me on this?
If you only want to use CATransition
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionMoveIn];
[animation setSubtype:kCATransitionFromTop];
[animation setDuration:0.6f];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:
kCAMediaTimingFunctionEaseInEaseOut]];
[subview.layer addAnimation:animation forKey:#"someTransition"];
Change the frame of the subview insite the UIView Animation, like as follows,
initially set the
subView.frame=CGRectMake(0,self.view.frame.size.height,your_width,your_height);
then set the required frame position inside the animation
[UIView animateWithDuration:0.25 delay:0 options: UIViewAnimationCurveEaseOut animations:^ {
// set subView's frame
} completion:^(BOOL finished) {
}
];
Try this code
[self.view bringSubviewToFront:yoursubview];
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionFade];
[animation setDuration:1.00];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:
kCAMediaTimingFunctionEaseIn]];
[yoursubview.layer addAnimation:animation forKey:#"fadeTransition"];
For more info check these links
Code on GitHub
Implement Changes Between Two Views In iPhone
add subview with animation
Try to use UIView animation block:
yourView.frame = CGRectMake(0, yourSuperView.frame.size.height, yourView.frame.size.width, yourView.frame.size.height);
[yourSuperView addSubview:yourView];
[UIView animateWithDuration:1.0 animations:^{
yourView.frame = CGRectMake(0, 0, yourView.frame.size.width, yourView.frame.size.height);
} completion:^(BOOL finished) {
//Task to do on Animation completion.
}];
You change the animation duration according to your convince. Also you can use following:
[UIView animateWithDuration:1.0 animations: {
//your Implemntation
}];
or
[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationCurveEaseIn animations:^{
} completion:^(BOOL finished) {
}];
Please check the UIView class reference for animation option in UIViewAnimationOptions section and detail of methods.

Cannot make a smooth animation in UIView

I have add a view animation to my view on button click, the view will slide to right side on first click and will come back on second click..(just like in Facebook iPhone app)
But the animation is not so smooth, there are some jerkings and all. But when it reaches the final (x, y) they are in correct position, when the view is moving the animation doesn't look so professional.Help me to correct this
and one question,is there any way to animate the below tabbar with the same event.
(You can see in images that the tabbar is not moved with animation)
Adding the code here!
- (IBAction)ShowView:(id)sender
{
if (n==0)
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
self.view.center = CGPointMake(410, 205);
[[self.view layer] addAnimation:animation forKey:#"SwitchToNoti"];
n=1;
}else
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
self.view.center = CGPointMake(160,205);
[[self.view layer] addAnimation:animation forKey:#"SwitchTohome"];
n=0;
}
try this
- (IBAction)ShowView:(id)sender {
if (n==0)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[self.view setCenter:CGPointMake(self.view.center.x + 100, self.view.center.y)];
[UIView commitAnimations];
n=1;
}else
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[self.view setCenter:CGPointMake(self.view.center.x - 100, self.view.center.y)];
[UIView commitAnimations];
n=0;
}
}
You can use the following code to show the animation :-
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.40];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.view.frame = TestRect;
[UIView commitAnimations];
You need to make a CGRect named TestRect here and see if this solves your problem.

UIViewAnimation block UIView interaction

I have an UIViewController named MainViewController.
It's frame:
I also have an UIView named SingleSearchView which is a subview of MainViewController.
And I add this singleSearchView to the MainViewController:
self.singleSearchView = [[SearchView alloc] initWithFrame:CGRectMake(0, -480, 320, 480)];
[self.view addSubview:singleSearchView];
When I'm trying the following animation to "switch" to the singleSearchView it blocks me from any touch interaction with the singleSearchView
[UIView beginAnimations:#"searchAnimation" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:1.0f];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.singleSearchView cache:YES];
[self.view setFrame:CGRectMake(0, 480, 320, 480)];
[UIView commitAnimations];
I also tried this way:
[UIView animateWithDuration:1.5 delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
[self.view setFrame:CGRectMake(0, 480, 320, 480)];
}
completion:nil];
Thanks
From this answer to another question:
Turns out UIView's block animation by default blocks user interaction, and to get around it you need to pass UIViewAnimationOptionAllowUserInteraction as one of the options. Hopefully someone else will have some use of this information as well.
You shouldn't try to move self.view. The self.view property is special in the UIVIewControll hierarchy. You should create a subview and animate this one instead...
Figured it out!
CATransition* trans = [CATransition animation];
[trans setType:kCATransitionPush];
[trans setDuration:0.5];
[trans setSubtype:kCATransitionFromBottom];
[self.singleSearchView setFrame:CGRectMake(0, 0, 320, 480)];
CALayer *layer = self.view.layer;
[layer addAnimation:trans forKey:#"Transition"];

calling an animation?

I have placed the following code in my program
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionFade];
[animation setSubtype:kCATransitionFromLeft];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[theWindow layer] addAnimation:animation forKey:#"SwitchToView1"];
Everything works great but there is no animation when I build the project into the simulator.
Where and how do I call this animation? once I get this then I can submit it to the app store!
Do you have any views in your app or just a Window? I'm just wondering if you're adding the animation beneath everything else. In most of my apps and many of Apple's samples, there is an underlying MainWindow and all views are added up on top of that using ViewControllers or other controllers.
Also, have you thought about using the much simpler beginAnimation...commitAnimation?
If you're merely trying to animate the addition of a view and deletion of another, see my code for doing this with viewControllers:
- (void)switchTwoViews:(UIViewController *)view1 otherView:(UIViewController *)view2 cacheTheView:(BOOL) cache;
{
/*
This method is called when the info or Done button is pressed.
It flips the displayed view from the main view to the flipside view and vice-versa.
*/
UIViewController *coming = nil;
UIViewController *going = nil;
UIViewAnimationTransition transition;
[view1.view setUserInteractionEnabled: NO];
[view2.view setUserInteractionEnabled: NO];
if (view1.view.superview == nil) {
coming = view1;
going = view2;
transition = UIViewAnimationTransitionFlipFromLeft;
}
else {
coming = view2;
going = view1;
transition = UIViewAnimationTransitionFlipFromRight;
}
// [coming.view setFrame:CGRectMake(0, 0, 480, 320)];
NSArray *viewArray = [[NSArray alloc] initWithObjects:coming, going, nil];
[coming viewWillAppear:YES];
[going viewWillDisappear:YES];
[UIView beginAnimations:#"View Flip" context:viewArray]; {
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidEnd:finished:context:)];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:transition forView:self.view cache:cache];
[self.view addSubview: coming.view];
}
[UIView commitAnimations];
}
- (void) animationDidEnd:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
NSArray *viewArray = context;
[((UIViewController *)[viewArray objectAtIndex:1]).view removeFromSuperview];
[[viewArray objectAtIndex:1] viewDidDisappear:YES];
[[viewArray objectAtIndex:0] viewDidAppear:YES];
[[[viewArray objectAtIndex:0] view] setUserInteractionEnabled: YES];
[viewArray release];
}

UIButton move animated problem

how to make a UIButton when moving (animated) without shadow? i tried the code but cannot make it can someone modifify or point out the problem
-(IBAction)move{
point=CGPointMake(0,1);
for(int i=0;i<50;i++){
NSLog(#"fdfafa");
CATransition *animation = [CATransition animation];
[animation setType:#"push"];
[animation setSubtype:#"fromBottom"];
[animation setDuration:0.5];
//[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[testButton.layer addAnimation:animation forKey:kAnimationKey];
testButton.center = CGPointMake(testButton.center.x, testButton.center.y + point.y);
}
NSLog(#"%f",testButton.center.y);
}
or have other better method to make a object move animated?
I'd use something like this:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
button.center = CGPointMake(x,y);
[UIView commitAnimations];