remove view from right to left with animations - ios

I adding view when btw is clicked from left to right and want to remove this view from right to left.
Below is my code while adding view from left to right
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main"
bundle: nil];
leftMenu = (LeftMenuViewController*)[mainStoryboard
instantiateViewControllerWithIdentifier: #"menuController"];
leftMenu.view.frame = CGRectMake(-320, 0, 320-50, self.view.frame.size.height);
[self addChildViewController:leftMenu];
[self.view addSubview:leftMenu.view];
[UIView animateWithDuration:0.3 animations:^{
leftMenu.view.frame = CGRectMake(0, 0, 320-50, self.view.frame.size.height);
hoverView.hidden = NO;
} completion:^(BOOL finished) {
[leftMenu didMoveToParentViewController:self];
}];
For removing this view from right to left what i have tried is:
self.view.frame = CGRectMake(320-50, 0, self.view.frame.size.width, self.view.frame.size.height);
[self willMoveToParentViewController:nil];
[UIView animateWithDuration:0.3 animations:^{
self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
} completion:^(BOOL finished) {
[self removeFromParentViewController];
[self.view removeFromSuperview];
}];
I want to remove view from right to left .Any help how to proceed further?

You must move your subview out of frame of superview, so set x-coordinate of your subview to negative value of it's own width. This will cause your view move from right to left out of view.
[self willMoveToParentViewController:nil];
[UIView animateWithDuration:0.3 animations:^{
self.view.frame = CGRectMake(-self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
} completion:^(BOOL finished) {
[self removeFromParentViewController];
[self.view removeFromSuperview];
}];

You can remove it like this:
CGRect napkinBottomFrame = Yourview.frame;
napkinBottomFrame.origin.x = 0;
[UIView animateWithDuration:0.3 delay:0.0 options: UIViewAnimationOptionCurveEaseOut animations:^{ Yourview.frame = napkinBottomFrame; } completion:^(BOOL finished){/*done*/}];

Swift 3 and 4 version:
leftMenu.didMove(toParentViewController: nil)
UIView.animate(withDuration: 0.3,
animations: {
self.leftMenu.view.frame = CGRect(x: -self.view.frame.width, y: 0, width: self.view.frame.width, height: self.view.frame.height)
},
completion: { finished in
self.leftMenu.view.removeFromSuperview()
self.leftMenu.removeFromParentViewController()
})
P.S. leftMenu should be class variable (property)

Related

iOS - Creating a custom Transition animation

I am trying to create a custom transition between to view controllers that are linked to a Container View Controller with custom segues.
This is my attempt at creating the custom transition. However, what happens is the view animates up and down as intended. But the view controllers switch instantly. What I am trying to create is the view sliding off screen vertically, then the container switches view controllers, then it slides back up screen.
Any ideas how I can achieve this animation with a custom animation?
- (void)swapFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController
{
toViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[fromViewController willMoveToParentViewController:nil];
[self addChildViewController:toViewController];
[self transitionFromViewController:fromViewController toViewController:toViewController duration:.4 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.view.frame = CGRectMake(0, 560, self.view.frame.size.width, self.view.frame.size.height);
} completion:^(BOOL finished) {
[UIView animateWithDuration:.4 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
} completion:^(BOOL finished) {
[fromViewController removeFromParentViewController];
[toViewController didMoveToParentViewController:self];
}];
}];
}
[UIViewController transitionFromViewController:toViewController] works only when both fromViewController and toViewController are already the child controllers of the receiver.
The documentation for transitionFromViewController:toViewController: says the following:
Transitions between two of the view controller's child view controllers.
fromViewController: A view controller whose view is currently visible in the parent's view hierarchy.
toViewController: A child view controller whose view is not currently in the view hierarchy.
I didn't test it out however, I think this would work:
- (void)swapFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController
{
CGFloat width = CGRectGetWidth(self.view.frame);
CGFloat height = CGRectGetHeight(self.view.frame);
[fromViewController willMoveToParentViewController:nil];
[self addChildViewController:toViewController];
toViewController.view.frame = CGRectMake(0.0, height, width, height);
[self.view addSubView:toViewController.view];
[UIView transitionWithView:self.view
duration:0.4
options:UIViewAnimationOptionCurveLinear
animations:^{
fromViewController.view.frame = CGRectMake(0.0, height, width, height);
toViewController.view.frame = CGRectMake(0.0, 0.0, width, height);
}
completion:^(BOOL finished) {
[fromViewController removeFromParentViewController];
[fromViewController.view removeFromSuperView];
[toViewController didMoveToParentViewController:self];
}];
}

how to perform a slide view on a button click

in my main view controller the button action is
-(IBAction)ChangeImage
{
UIImage *guns = [UIImage imageNamed:#"guns.png"]; // load our image resource
imageView = [[UIImageView alloc] initWithImage:guns];
NSLog(#"kaam hua..??");
[self.view addSubview:imageView];
[UIView transitionFromView:self.view
toView:imageView
duration:3.0f
options:UIViewAnimationOptionTransitionCrossDissolve
completion:nil];
NSLog(#"hfhjlkhbjhdfcvkbjnlkhgj");
}
i want to view the animation in slide view
is there any method to do this
can i chnage the UIViewAnimationOptionTransitionCrossDissolve to another one to show slide view
You can use UIViewAnimationOptionCurveEaseIn in animation option,
Try this,
CGRect or=self.view.frame;
CGRect basketTopFrame = self.view.frame;
basketTopFrame.origin.x = basketTopFrame.size.width;
self.view.frame = basketTopFrame;
[UIView animateWithDuration:0.2
delay:0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
[self.view addSubview:imageView];
}
completion:^(BOOL finished){
NSLog(#"Done!");
}];

removeFromSuperview not releasing the view while using ARC

I am adding a subview to my finalview which is used to change prefrences on the app.
I add it like this from my finalview
getPrefrencesViewController = [[GetPrefrencesViewController alloc] init];
getPrefrencesViewController.view setBackgroundColor:[UIColor clearColor]];
getPrefrencesViewController.view.frame = CGRectMake(0.0, 480.0, 320.0, 480.0);
getPrefrencesViewController.prefToolBar.frame = CGRectMake(0.0, 9.0, 320.0, 45.0);
[getPrefrencesViewController.view addSubview:getPrefrencesViewController.prefToolBar];
getPrefrencesViewController.prefTableView.frame = CGRectMake(0.0, 54.0, 320.0, 435.0);
[getPrefrencesViewController.view addSubview:getPrefrencesViewController.prefTableView];
[self.navigationController.view insertSubview:getPrefrencesViewController.view aboveSubview:self.navigationController.view];
[UIView animateWithDuration:0.3
delay:0.0f
options:UIViewAnimationOptionCurveEaseIn
animations:^{
getPrefrencesViewController.view.frame = CGRectMake(0.0, 10.0, 320.0, 480.0);
} completion:^(BOOL finished) {
if (finished) {
NSLog(#"YAY!");
}
}];
}
This works fine...
When the new view is added as a subview I have a UIToolBar with two buttons save and cancel, when the user touches cancel this code is used
- (IBAction)cancleUIButton:(id)sender {
//unload portrait view
[UIView animateWithDuration:0.4
delay:0.0f
options:UIViewAnimationOptionCurveEaseIn
animations:^{
self.view.frame = CGRectMake(0.0, 480.0, 320.0, 480.0);
} completion:^(BOOL finished) {
if (finished) {
// remove subView for superView
[self.view removeFromSuperview];
}
}];
}
as you can see i add the subview, animate it upwards, then when the user presses cancel I animate the view then remove it from superview.. however if I then load it again all the values I change but not saved are still changed, meaning the view is never removed.
I am hoping someone can tell me why this view is never getting removed? any help would be greatly appreciated.

Hide both Navigation Controller and Tab Bar Controller with animation

I'm trying to hide a UITabBarController and UINavigationController simultaneously when a button is touch. I found a very nice code snippet here How to hide uitabbarcontroller but I have problem when trying to hide and animate both UINavigationController and the tabbarcontroller. I also found a lot of examples on the internet when they hide the tabbar using self.tabBarController.tabBar.hidden = YES but that only hides the button items not the black bar at the bottom.
After playing a lot around I can get to make both animate correctly because I think that it's related to the hiding of the Navigation Controller which makes the size of the whole window to change on the fly.
-(IBAction)touchImage:(id)sender {
if (isImageFullScreen) {
isImageFullScreen = NO;
[self.navigationController setNavigationBarHidden:NO animated:YES];
[UIView transitionWithView:self.view
duration:0.5
options:UIViewAnimationOptionCurveLinear
animations:^
{
hotelImageButton.frame = CGRectMake(0,20,320,92);
[self showTabBar:self.tabBarController];
}
completion:^(BOOL finished)
{
}];
} else {
isImageFullScreen = YES;
[self.navigationController setNavigationBarHidden:YES animated:YES];
[UIView transitionWithView:self.view
duration:0.5
options:UIViewAnimationOptionCurveLinear
animations:^
{
hotelImageButton.frame = CGRectMake(0,0,320,480);
[self hideTabBar:self.tabBarController];
}
completion:^(BOOL finished)
{
}];
}
}
The hideTabBar and showTabBar methods are the ones from the other post I linked above.
I also tried some other combinations but I can't make it look good. Any ideas?
Thanks in advance.
I tried that code now and I see that the UITabBar show animation doesn't take place smoothly.
I managed to make it smoother by adjusting the duration period for the tabbar showing animation to be lower.
[UIView setAnimationDuration:0.2];
Hopefully that works.
EDIT:
Please try this code, it resizes the parent view to be bigger in 1 animation transaction in such a way that only the bars are hidden and the content is shown.
- (IBAction)TestButton1:(UIButton *)sender {
if(!isAnimating){
if(isTabBarAndNavBarHidden){
[UIView transitionWithView:self.view
duration:0.5
options:UIViewAnimationOptionTransitionNone
animations:^
{
isAnimating=YES;
CGFloat statusBar_height=[[UIApplication sharedApplication] statusBarFrame].size.height;
CGFloat screen_height=[UIScreen mainScreen].bounds.size.height;
[self.tabBarController.view setFrame:CGRectMake(self.tabBarController.view.frame.origin.x, 0, self.tabBarController.view.frame.size.width, screen_height)];
[self.navigationController.navigationBar setFrame:CGRectMake(self.navigationController.navigationBar.frame.origin.x, statusBar_height, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
}
completion:^(BOOL finished)
{
isTabBarAndNavBarHidden=NO;
isAnimating=NO;
}];
}else{
[UIView transitionWithView:self.view
duration:0.5
options:UIViewAnimationOptionTransitionNone
animations:^
{
isAnimating=YES;
CGFloat statusBar_height=[[UIApplication sharedApplication] statusBarFrame].size.height;
CGFloat screen_height=[UIScreen mainScreen].bounds.size.height;
[self.tabBarController.view setFrame:CGRectMake(self.tabBarController.view.frame.origin.x, statusBar_height-self.navigationController.navigationBar.frame.size.height, self.tabBarController.view.frame.size.width, screen_height+self.navigationController.navigationBar.frame.size.height+self.tabBarController.tabBar.frame.size.height-statusBar_height)];
[self.navigationController.navigationBar setFrame:CGRectMake(self.navigationController.navigationBar.frame.origin.x, 0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
}
completion:^(BOOL finished)
{
isTabBarAndNavBarHidden=YES;
isAnimating=NO;
}];
}
}
}
This code is for iPhone 4/4S.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (self.lastContentOffset > scrollView.contentOffset.y)
{
NSLog(#"Scrolling up");
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
[self.tabBarController.tabBar setFrame:CGRectMake(0, 430, 320, 50)];
[self.navigationController.navigationBar setFrame:CGRectMake(0, 20, self.navigationController.navigationBar.frame.size.width,self.navigationController.navigationBar.frame.size.height)];
} completion:
^(BOOL finished) {
[UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
} completion:^(BOOL finished) {
//
}];
}];
}
else if (self.lastContentOffset < scrollView.contentOffset.y)
{
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
[self.navigationController.navigationBar setFrame:CGRectMake(0, -60, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
[self.tabBarController.tabBar setFrame:CGRectMake(0, 480, 320, 50)];
} completion:
^(BOOL finished) {
[UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
} completion:^(BOOL finished) {
}];
}];
NSLog(#"Scrolling Down");
}
self.lastContentOffset = scrollView.contentOffset.y;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tabBarController.tabBar setFrame:CGRectMake(0, 430, 320, 50)];
[self.navigationController.navigationBar setFrame:CGRectMake(0, 20, self.navigationController.navigationBar.frame.size.width,self.navigationController.navigationBar.frame.size.height)];
// Do any additional setup after loading the view.
}

UILabel animate the width

How do we go about animating the UILabel's width so that it increases and decreases in width only using UIView animateWithDuration
Thank you for your help (at this point i'm so frustrated as I've been trying to do so but it does not work)
You can set the frame of the UILabel inside the UIView animation block. Something like this:
CGRect originalFrame = self.address.frame;
[UIView animateWithDuration:0.3
animations:^{
self.myLabel.frame = CGRectMake(0, 0, 100, 100);
}
completion:^(BOOL finished) {
// use similar UIView animation to resize back to original
}];
Obviously the dimensions you feed in will depend on your app, and may be calculated based on the content.
try to increase and decrease the frame width with an animation as below
// original label frame CGRectMake( 0,0, 100,60 );
[UIView beginAnimations: #"moveLogo" context: nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:4];
//increasing frame width
label.frame = CGRectMake( 0,0, 400,60 );
[UIView commitAnimations];
finally did it like this with blocks in iOS 5.0
[UIView animateWithDuration:2. delay:0 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionBeginFromCurrentState animations:^{
[self.messageLabel setFrame:CGRectMake(90, 0, labelSize.width, 90)];
} completion:^(BOOL finished) {
}];
Try this
[UIView animateWithDuration:0.6 animations:^(void)
{
self.label.bounds = CGRectMake(100, 100, 200, 100);
}completion:^(BOOL finished) {
[UIView animateWithDuration:0.6 animations:^(void)
{
self.label.bounds = CGRectMake(100, 100, 100, 100);
}];
}];
I was able to do it in this way. self refers to the view in which i'm adding the label. in the init, add the label with width as 1 px. then use the below.. the UIViewAnimationOptionBeginFromCurrentState allows it to be opened like a door PHEW!
[UIView animateWithDuration:1. delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[self setFrame:CGRectMake(self.frame.origin.x-labelSize.width-PADDING, self.frame.origin.y, self.frame.size.width+labelSize.width+PADDING, self.frame.size.height)];
[self.messageLabel setFrame:CGRectMake(95, 10, labelSize.width, labelSize.height)];
self.messageLabel.alpha = 1;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1. delay:2. options:0 animations:^{
self.messageLabel.alpha = 0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1. delay:0. options:0 animations:^{
[self setFrame:CGRectMake(self.frame.origin.x+labelSize.width+PADDING, self.frame.origin.y, self.frame.size.width-labelSize.width-PADDING, self.frame.size.height)];
} completion:^(BOOL finished) {
}];
}];
}];

Resources