I am trying to slide between two child controllers that get loaded into a container view. I have found out about the sliding effect in CATransition. However, it doesn't slide the way I want it to. If I am sliding from right to left, I want the current controller to also slide to the left. So, it basically creates a scrolling effect using sliding. How can I achieve that? Here is my current code.
var transition = CATransition();
transition.duration = 0.25;
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut);
transition.type = kCATransitionPush
if source.prevItem > source.pageControl.currentPage {
transition.subtype = kCATransitionFromLeft
} else {
transition.subtype = kCATransitionFromRight
}
dest.view.layer.addAnimation(transition, forKey: kCATransition)
source.transitionFromViewController(
source.currentChildController!,
toViewController: dest,
duration: 0.5,
options: UIViewAnimationOptions.CurveEaseInOut,
animations: nil,
completion: { finished in
source.currentChildController?.removeFromParentViewController()
dest.didMoveToParentViewController(source)
source.currentChildController = dest
}
);
This implementation only brings the destination controller over the current controller even though the documentation for Push transition states: "The layer’s content pushes any existing content as it slides into place. The “Common Transition Subtypes” are used with this transition."
Related
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
This question already has answers here:
iOS Segue - Left to Right -
(12 answers)
Closed 6 years ago.
I want to change the default transition of navigation controller i.e.
FROM
left to right
TO
right to left
when pushing a view controller and when I press back button then again change the default transition of navigation controller i.e.
FROM
right to left
TO
left to right
self.navigationController?.pushViewController(currentViewController, animated: false)
use this code sample:
let transition = CATransition()
transition.duration = 0.8
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionFromRight // kCATransitionFromLeft
self.navigationController?.view.layer.addAnimation(transition, forKey: nil)
self.navigationController?.pushViewController(currentViewController, animated: yes)
I have implemented in my code today a CATransition of type cube,
as I seen here, CATransition can be added to self.view.layer,
so my first question is if I can use my CATransition for switching between viewControllers and not just UIViews?
if I can't do it,
please look at this image:
http://up411.siz.co.il/up1/hnmim2mtjodt.png
as you can see, while the transition is in process, I see the same view of both faces of the cube,
I'd like it to be that one face presents view A and the other one presents view B
here is the code if needed :
self.view.layer.addAnimation(createTransition("Right"), forKey: "kCATransition")
func createTransition(side:String) -> CATransition{
var transition = CATransition()
transition.delegate = self
transition.duration = 0.6
transition.timingFunction = CAMediaTimingFunction(name: "easeInEaseOut")
transition.type = "cube"
transition.subtype = "from\(side)"
return transition
}
You have to add the same transition to the view layer of the second view controller's view as well.
Apple documentation in the Core Animation Programming Guide:
To perform a transition animation, you create a CATransition object and add it to the layers involved in the transition.
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];
What I'm trying to do is push in from the left side with a custom animation using CATransitions. In my normal code I want to use duration of 0.3, but in the example below I'm using 6.0 just so I can see the actual problem during the transition.
And it is really obvious that there is a fade going on when transitioning to the new view. The new view coming in fades from it's background color to the actual view, whereas the view that is sliding off screen fades from the actual view to it's background color. It's making for some odd looks when doing the push animation to transition.
Below is the code that I'm using for the transition. settingsController is the controller I'm trying to animate to.
let transition = CATransition()
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.duration = 6.0
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft
self.navigationController?.view.layer.addAnimation(transition, forKey: nil)
UIView.transitionFromView(self.view, toView: settingsViewController.view, duration: 0, options: UIViewAnimationOptions.TransitionNone, completion: nil)
self.navigationController?.pushViewController(settingsViewController, animated: false)
And as mentioned, the actual sliding animation works as I want it to, however there is this fade that happens that just makes it not look right . I've been searching google and stackoverflow and have not seen a solution posted, so I'm wondering if anyone has any idea how to get rid of this fade, or even if there is a work around that "simulates" this behavior so the user experience looks as I want it to.
I've tried making the background colors of both views the same and it still doesn't look right.
The usual way to use a sliding CATransition is to give the transitioning view/layer a superview that clips to bounds (or a superlayer that masks to bounds). That way, the sliding view/layer is not visible outside the superview's bounds, and we never see the fade.