UIView Animation slide in - ios

I am trying to get my UIView to slide in from the right in the viewDidLoad method here's what I've got.
CGRect menuFrame = self.menu.frame;
menuFrame.origin.y = menuFrame.size.height+200;
[UIView animateWithDuration:1
delay:0.05
options: UIViewAnimationCurveEaseIn
animations:^{
self.menu.frame = menuFrame;
}
completion:^(BOOL finished){
NSLog(#"Animation Complete!");
}];
I am not sure what has gone wrong here I appreciate any help greatly.

This isn't what you asked, but it is worth saying anyway: viewDidLoad is a bad place for this code. viewDidLoad is called because the view controller has obtained its view, not because that view has appeared or will soon appear in the interface. It might soon appear in the interface, which is why your code has seemed to work up until now, but it might not.
The correct place for this code is probably viewWillAppear or viewDidAppear. You probably want to call this code only once, though, the first time the view appears. If you want to prevent the code from being called, because you've already animated menu into view on an earlier viewDidAppear call, simply include a conditional check to see whether menu already has a frame within the visible view.
By the way, another reason for avoiding things having to do with frames and bounds in viewDidLoad is that if your app launches into landscape and this is your root view, x and y are reversed.

It looks like you are trying to slide your menu in from the bottom as the Y position is pushed down initially by 200.
Usually you start by adding the view as a subview in its offscreen position and then set the onscreen position in the animation block.
And, make sure that you pass 1.0 as the animation duration.

use those transition.subtype for different effects
CATransition* transition = [CATransition animation];
transition.duration = 0.5;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.subtype = kCATransitionFromLeft; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
//transition.subtype = kCATransitionFromTop; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
[appDelegate.navigation.view.layer addAnimation:transition forKey:nil];
viewController *DairyVC = [[viewController alloc] initWithNibName:#"viewController" bundle:nil];
[appDelegate.navigation pushViewController:DairyVC animated:YES];

Related

How can I create a transition between two subview iOS?

I have a problem when I want to create a transition between to views inside a container view in iOS application. I want to create an animation that makes my first view goes out sliding to the left and the second coming from the right (like switching between photos, a sort of push in Navigation Controller). I created my animation but the problem is that my subview does not disappear once reached the border of my container view but when it reaches the edge of my screen.
I used this code:
CATransition *transition = [CATransition animation];
transition.duration = animationTime;
transition.type = kCATransitionPush;
transition.subtype = transationSubtype;
[transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
transition.removedOnCompletion = YES;
[view.layer addAnimation:transition forKey:nil];
As visible in the image the red square goes out from the screen and not from its container and also the green comes inside from the screen edge and not from the container view edge. I want instead that the animation ends when reaches the container view edge.
I tried anything but I was not able to find something useful.
Try to set clipsToBounds property of the container view to YES

How to give sliding animation to a single UIView?

I have one screen, call it video screen where one video is shown with details and comments just like youtube. Now this view is slidable, means whenever user slides the screen, next or previous video comes by changing whole content view.
I am using [UIView transitionWithView] to give it sliding effect. By changing frame of content view, I am doing swiping right code like below.
[UIView transitionWithView:viewTop duration:0.4
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
viewTop.frame = CGRectMake(SCREEN_WIDTH, viewTop.frame.origin.y, viewTop.frame.size.width, viewTop.frame.size.height);
}
completion:^(BOOL Finished){
[self getNextVideoDetail];
viewTop.frame = CGRectMake(-SCREEN_WIDTH, viewTop.frame.origin.y, viewTop.frame.size.width, viewTop.frame.size.height);
[UIView transitionWithView:viewTop duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
viewTop.frame = CGRectMake(0, viewTop.frame.origin.y, viewTop.frame.size.width, viewTop.frame.size.height);
}
completion:nil
];
}];
Here, viewTop is my content view. Everything works fine but I want that whenever I slide the screen to left, before the view disappears completely, some part of the view should be shown at the right side. Because the code I wrote above will show white self.view and screen will be blank for some mili seconds.
Is it possible with only one UIView to navigate videos like giving mirror effect?
Use this:
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
transition.duration = 0.4;
[viewTop.layer addAnimation:transition forKey:nil];

UIView transitionWithView custom animation option

I have an application that first displays a login screen. Once the user logs in, this code is executed:
[UIView transitionWithView:self.view.window
duration:0.7
options:UIViewAnimationOptionTransitionNone
animations:^{
self.view.window.rootViewController = menuController;
} completion:^(BOOL finished) {
// Code to run after animation
}];
This works as expected; however, I would like to define my own custom animation option instead of the flip from left/right option.
A simple example would be sliding the new view controller onto the screen from top to bottom. How can I do this when changing the rootViewController?
Also, is there a completely different approach to animate switching the rootViewController? I am not married to this solution, it is just the closest thing I can find through Google/SO search.
Any help would be great, thanks!
Using a CATransition should be able to give you the sort of animation you're looking for. Granted, I've never actually attempted this specifically with rootViewController, but to slide the view in from top to bottom, I'd suggest trying this:
CATransition *transition = [CATransition animation];
transition.duration = 1.0;
transition.type = kCATransitionFromBottom;
transition.subtype = kCATransitionFade;
CALayer *layer = self.view.window.rootViewController.view.layer;
[layer addAnimation:transition forKey:kCATransition];
For that change to happen you must first add the ViewController and its view to the parent viewController. A simple trick could be add it "on the roof" of your window (starting Y position = -self.view.bounds.size.height). And then use
[UIView animateWithDuration:
delay:
options:
animations:^{}
completion:^(BOOL finished){
self.view.window.rootViewController = menuController;
}];
Then simply inside the animation brackets create the animation you want.

How to animate view after using UIModalPresentationCurrentContext

I was looking around for a solution on how to wrap a view in a dialog in iOS when I came across this post, which has this line:
vc.modalPresentationStyle = UIModalPresentationCurrentContext;
It solves my problem of basically creating/imitating a dialog but it does not animate upon transition as mentioned in the post. So what is the simplest way to get the slide up animation?
ps.I would ask this as a sub question in that post but I do not have 50 rep comment :(
Well, once your view has been shown, you can do pretty much any animation you want in it. You can do a simple [UIView animateWithDuration] kind of deal, but I would personally use a CATransition for this, it's relatively simple.
The Way of the QuartzCore
First, I'm gonna assume that the view you're presenting is transparent, and there's another view inside that behaves as the dialog. The view controller that will be presented, let's call it PresentedViewController and holds the dialog property for the view within.
PresentedViewController.m
(Needs to be link against QuartzCore.h)
#import <QuartzCore/QuartzCore.h>
#implementation PresentedViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (animated)
{
CATransition *slide = [CATransition animation];
slide.type = kCATransitionPush;
slide.subtype = kCATransitionFromTop;
slide.duration = 0.4;
slide.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
slide.removedOnCompletion = YES;
[self.dialog.layer addAnimation:slide forKey:#"slidein"];
}
}
Getting Fancy
The good thing about this, is that you can create your own custom animations, and play around with other properties.
CABasicAnimation *animations = [CABasicAnimation animationWithKeyPath:#"transform"];
CATransform3D transform;
// Take outside the screen
transform = CATransform3DMakeTranslation(0, self.view.bounds.size.height, 0);
// Rotate it
transform = CATransform3DRotate(transform, M_PI_4, 0, 0, 1);
animations.fromValue = [NSValue valueWithCATransform3D:transform];
animations.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
animations.duration = 0.4;
animations.fillMode = kCAFillModeForwards;
animations.removedOnCompletion = YES;
animations.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0 :0.0 :0 :1];
[self.dialog.layer addAnimation:animations forKey:#"slidein"];
Here, the view will be moved outside of screen by the translation, then rotated, and it will slide in, back to its original transform. I also modified the timing function to provide a smoother curve.
Consider that I just scraped the surface of what's possible with CoreAnimation, I've been on this road for three years now, and I've grown to like CAAnimation for all the things it does.
Storyboard pro-tip: You can wrap this up very nicely if you build your own custom UIStoryboardSegue subclass.

addsubview animation works only first time

I am trying add subview with animation effect using the code which work fine for the first time below.
CATransition *transition = [CATransition animation];
transition.duration = 1.0;
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromRight;
[newView.layer removeAllAnimations]
[newView.layer addAnimation:transition forKey:nil];
[self.masterview addSubview:newView];
There is a back button in newView which removes the View from the superview.
[newView removeFromSuperview];
Now when I try adding newView as subview again using the above code,its first adds the view as a subview(without animation) and again with animation.
Instead of removing newView from superview, why not you can just set a frame like
newView.frame = CGRectMake(0, -500, CGRectGetWidth(newView.frame), CGRectGetHeight(newView.frame));
so that you can just hide it from showing. Just an idea. :-)
When you remove the view from its super view it keeps its frame as it is. So, i think you have to change the newView location to be placed in the new location again.
It will work fine for all the times.But the only difference you have to find is that you need to wai for the duration of animation. It will not animate continuously. It will take a minimum time that you mentioned in the duration.

Resources