How can I create a transition between two subview iOS? - 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

Related

How to switch different views in one view controller

How to switch different views in one view controller.I have to design a app that has five views in one view controller and the views contains some text fileds.if i fill the text fileds in first view it should enter to the another view.that another view contains text fields.
Try this in Objective-C
Switch view with animation
CATransition *transition = [CATransition animation];
transition.duration = 0.5;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.superView addAnimation:transition forKey:nil];
[self.superView addSubview:self.chieldView];
Actually you can do it by using container view with changing alpha of your (5 views) so when you are on first view its alpha becomes 1 while others will be 0 and so on

Core animation layer is acting weird when presenting a modal view controller. CATransform3D issue

I am trying to create a custom UIView transition. Basically when a certain modal view is presented then the views it is covering up move into the background.
To achieve this I am using Core animation to manipulate the CATransform3D on the layer of the view I am moving to the background then presenting the modal view on top of that.
To move the view into the background I am creating a CABasicAnimation to animate the change in the CATransform3D like this
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"transform"];
CATransform3D toTransform = CATransform3DIdentity;
toTransform.m34 = 1.0f / -500.0f;
toTransform = CATransform3DTranslate(toTransform, 0.0f, 0.0f, -40.0f);
[animation setFromValue:[NSValue valueWithCATransform3D:CATransform3DIdentity]];
[animation setToValue:[NSValue valueWithCATransform3D:toTransform]];
[animation setDuration:1.0f];
self.menuView.layer.transform = toTransform;
[self.menuView.layer addAnimation:animation forKey:#"moveBackAnimation];
self.menuView is the view I am moving back.
This works great until I add in the modal view.
RVLAddViewController *addViewController = [[RVLAddViewController alloc] initWithNibName:#"RVLAddViewController" bundle:nil];
[self presentViewController:addViewController animated:YES completion:nil];
I call this code right under the animation when a button is pressed.If this code isn't present them every thing works as expected the view looks like it is just fading into the distance and then it stays there.
When the modal view is presented then instead of gradually fading into the distance, it shoots down to the smaller size and moves into the top left hand corner.
I'll upload a sample application as soon as I can, github is down right now. edit No need -a solution has been given! /edit
I messed around with this for a while, and it looks like it has to do with auto layout. If you turn that off, it works as you would expect.

UIView Animation slide in

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];

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.

addAnimation to UINavigationController cause UINavigationController's view (layer) displayed on the topmost layer

I am trying to add a cool animation effect to the UINavigationController, however I found once I do it it will mess up the other views displayed on top of other views which suppose to be on top of it.
e.g. navivc is a UINavigationController
overlayview is a subview of navivc's superview, and placed on top of navivc.
...
CATransform3D t1 = CATransform3DIdentity;
t1.m34 = 1.0/-900;
t1 = CATransform3DScale(t1, 0.7, 0.7, 0.3);
t1 = CATransform3DRotate(t1, 15.0f*M_PI/180.0f, 1, 0, 0);
CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:#"transform" ];
//[move setFromValue:[NSNumber numberWithFloat:0.0f]];
[move setToValue:[NSNumber valueWithCATransform3D:t1]];
[move setDuration:0.3f];
move.removedOnCompletion = NO;
move.fillMode = kCAFillModeForwards;
[move setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
CAAnimationGroup *group = [CAAnimationGroup animation];
group.fillMode = kCAFillModeForwards;
group.removedOnCompletion = NO;
[group setDuration:0.3f];
[group setAnimations:[NSArray arrayWithObjects:move, nil]];
[navivc.view.layer addAnimation:move forKey:#"coolanimation"];
...
The animation is normal and correct, however now navivc's view show on top of overlayview.
Seemed the problem only happen on "transform" path, I tried other key paths, e.g. position, scale, translation etc., all those seemed to be correct. But the 3D transform messed up the view on top.
I would advise against animating a view controller's content view. Instead, create a container view inside the view controller's content view and animate that.
It isn't clear from your post where overlayView is in your view hierarchy, so it's hard to give you specific instructions on how to prevent your animation from covering your overlay view. You might try changing the zPosition of the overlayView's layer to make it appear above the layer you are animating.

Resources