I am trying to create an animation. When I click on an image, the view expands the from this frame to the view of the UIViewController to the bounds of whole screen on pushing it on the navigationController and shrinking to the same size while popping it back. I was able to create the pushing animation but unsuccessful in my attempt to create the popping animation.
// Code in the controller which pushes the controller on the stack
self.navigationController.view.center = center;
self.navigationController.view.frame = frame;
self.navigationController.view.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
// Code in the viewDidAppear of controller being pushed to the stack
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
self.navigationController.view.transform = CGAffineTransformIdentity;
self.navigationController.view.frame = [[UIScreen mainScreen] bounds];
self.navigationController.view.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
}
completion:nil];
This works properly but I tried the following code for pop animation in the viewDidDisappear, where initialFrame is the frame on which I am tapping to open the UIViewController.
// Code for viewDidDisappear
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
self.view.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
self.view.frame = self.initialFrame;
self.view.center = CGPointMake(self.initialFrame.size.width/2 + self.initialFrame.origin.x, self.initialFrame.size.height/2 + self.initialFrame.origin.y);}
completion:nil];
I am not able to make the pop animation work. Please help me out.
Related
I have an app in which I use a lot of animations with ease in/out curves. I use this function in all cases: UIView animateWithDuration:delay:options:animations:completion
All these animations are working ok, but now I am trying to add one to a drawer that pops in and out, and for some reason this particular animation is always linear:
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.activityBar.view.frame = CGRectMake(0, self.activityBar.view.frame.origin.y, self.activityBar.view.frame.size.width, 20);
} completion:nil];
Why is this animation linear, while other animations with the same option are curved?
This is the view hierarchy for self.activityBar.view
-UIViewController
-UIViewController
-UIViewController (animation code lives here)
-UIViewController (activityBar)
-UIView (activityBar.view)
//Set old Frame for activityBar Here
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.view layoutIfNeeded];
//Update to the new frame
self.activityBar.view.frame = CGRectMake(0, self.activityBar.view.frame.origin.y, self.activityBar.view.frame.size.width, 20);
} completion:^(BOOL finished) {
}];
With the following code I'm showing a view with an animation on a button click.
UIViewController *modalView = self.pageViewController;
[self.view addSubview:modalView.view];
[UIView animateWithDuration:1 animations:^{
CGRect currentRect = modalView.view.frame;
currentRect.origin.y = 650.0f;
currentRect.size.height = 295.0f;
[modalView.view setFrame:currentRect];
[modalView.view removeFromSuperview];
}];
[self.view addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
This works fine for first time button click. When I'm pressing the button again and again this is not animating anymore. It's animating for the first time only. Is there any way to do so?
Thanks in Advance!
The cause is that modalView.view's frame initially if different from
currentRect.origin.y = 650.0f;
currentRect.size.height = 295.0f;
so when changes its frame animation is happens.
But next time when you perform button's action modalView.view's current frame and that frame you sets are same. So there is nothing to animate at all.
Also, you should move [modalView.view removeFromSuperview]; from animation block to completion block
[UIView animateWithDuration:1 animations:^{
CGRect currentRect = modalView.view.frame;
currentRect.origin.y = 650.0f;
currentRect.size.height = 295.0f;
[modalView.view setFrame:currentRect];}
completion:^(BOOL finished) {
[modalView.view removeFromSuperview];
}];
I have the following code for my iPhone app in Xcode 4.5:
[UIView transitionWithView:self.view
duration:5.0f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.view = self.view;
} completion:NULL];
The code works exactly how I want except for 1 thing. Where I have self.view = self.view; I'd like a brief moment before this line where the view clears or even changes to nothing but a black screen, and then transition into the next loaded self.view. The reason is that some of my screens are very similar and the user might think the view didn't reload.
self.view = self.view can not compose animation
set self.view.alpha to zero then do animations.
self.view.alpha = 0.0f;
[UIView transitionWithView:self.view
duration:5.0f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.view.alpha = 1.0f;
} completion:NULL];
In my iPhone app, I have two views on a main view controller , one is normal view 'a' with some buttons and other is scrollView 'b'. When I tap on button in view 'a', I want to show scroll view with animations like the scroll view has to come up from the back of view 'a', but it comes up from the front of view 'a'.
I used the following code for animating the scroll view.
CGRect Frame = scrollView.frame;
if(Frame.origin.y == 420){
Frame.origin.y = 298;
[UIView animateWithDuration:0.5 animations:^{
scrollView.frame = Frame;
}];
}else{
Frame.origin.y = 420;
[UIView animateWithDuration:0.5 animations:^{
scrollView.frame = Frame;
}];
How to implement the animation to scroll view to show it from top of view 'a'.
Try to move "b" to the back first:
[self.view sendSubviewToBack:scrollview_b];
I found solution for my problem.
Solution :
CGRect Frame = scrollView.frame;
if(Frame.origin.y == 420){
Frame.origin.y = 298;
[UIView animateWithDuration:0.5 animations:^{
scrollView.frame = Frame;
[self.view insertSubview:scrollView belowSubview:view_a];
}];
}else{
Frame.origin.y = 420;
[UIView animateWithDuration:0.5 animations:^{
scrollView.frame = Frame;
[self.view insertSubview:scrollView belowSubview:view_a];
}];
}
I'm try to popViewcontroller with transform scale animation
according this code when it begin transform it present the black screen instead of the Parent View
how to fix this ?
[UIView animateWithDuration:0.5f
delay:0.0f
options:UIViewAnimationCurveEaseInOut
animations:^{
self.view.alpha = 1.0f;
self.view.transform = CGAffineTransformMakeScale(0.5f, 0.5f);
} completion:^(BOOL finished) {
[[self navigationController] popViewControllerAnimated:NO];
}];
This is the expected behavior, because the view of the previous controller is not in the view hierarchy until popViewControllerAnimated: method is called and you call it after the animation finishes.
I don't think adding subviews directly to the view of the navigation controller is a good idea, but the following code should work for you.
UINavigationController* navigationController;
CGRect frame;
//keep a reference to the navigation controller as
//[self navigationController] won't work after pop is called
navigationController = [self navigationController];
//remember the frame of the view relative to navigation controller's view
frame = [navigationController.view convertRect:self.view.frame fromView:self.view.superview];
//pop this controller, this will add the view of the
//previous controller into the view hierarchy
[navigationController popViewControllerAnimated:NO];
self.view.frame = frame;
//add this view on top of the previous one
[navigationController.view addSubview:self.view];
[UIView animateWithDuration:0.5f
delay:0.0f
options:0
animations:^{
self.view.alpha = 0.0f;
self.view.transform = CGAffineTransformMakeScale(0.5f, 0.5f);
} completion:^(BOOL finished) {
[self.view removeFromSuperview];
}];
By the way, UIViewAnimationCurveEaseInOut is not a correct constant for the options parameter. You should use the constants that start with UIViewAnimationOption for this method.