I would like to know if there is any way to make an UIModalTransitionStyle have a "push" style ?
Because I have something like this :
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let secondViewController = storyBoard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
secondViewController.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal
self.presentViewController(secondViewController, animated:true, completion:nil)
But I would like to have the simple "push" effect when I move to another viewcontroller.
Regards !
EDIT (Thanks to LoGoCSE) But still not solved
Now I have
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let secondViewController = storyBoard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
secondViewController.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal
let transition: CATransition = CATransition()
transition.duration = 0.25
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
self.view.layer.addAnimation(transition, forKey: kCATransition)
self.presentViewController(secondViewController, animated:false, completion:nil)
But, the point is, adding the view ([self.view addSubview:destinationController.view];) doesn't work.
So I tried, to presentModalView as usual (Without animation), but The transition is done on the same view, and then, the second view appear.
-This code in Objective-C works .You need to just convert it to Swift
UIViewController *sourceViewController = self ;
UIViewController *destinationController = objdestinationViewController;
CATransition* transition = [CATransition animation];
transition.duration = .25;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush; //kCATransitionMoveIn; //,kCATransitionPush, kCATransitionReveal, kCATransitionFade
transition.subtype = kCATransitionFromRight; //kCATransitionFromLeft,kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
[self.view.layer addAnimation:transition forKey:kCATransition];
[self.view addSubview:destinationController.view];
Hope it works :)
Related
So I'm trying to animate the tractions between my UIViewControllers. I want the new VC to push in from the same side that the button is located on. I have managed to push in one VC from the right, but when I try to push in from the left it will push in the same VC first then the new VC pops up.
Here is my code:
switch sender.view {
case settingsButton:
let settingScreen = SettingsVC()
settingScreen.modalPresentationStyle = .fullScreen
present(settingScreen, animated: false, completion: nil)
let transition = CATransition()
transition.duration = 0.7
transition.type = .moveIn
transition.subtype = .fromLeft
transition.timingFunction = CAMediaTimingFunction(name: .default)
view.window!.layer.add(transition, forKey: kCATransition)
case calendarButton:
let calendarScreen = CalendarVC()
calendarScreen.modalPresentationStyle = .fullScreen
self.present(calendarScreen, animated: false, completion: nil)
let transition = CATransition()
transition.duration = 0.5
transition.type = .moveIn
transition.subtype = .fromRight
transition.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
view.window!.layer.add(transition, forKey: kCATransition)
default:
break
}
You shouldn't be using CATransition to achieve this. With that, you can animate view transitions, within a single UIViewController. UIViewController transitions are a bit different.
Apple has it's own API for UIViewController transitions, but it's very cumbersome.
The best solution, in my opinion, is to use Hero library from Github.
It doesn't have a steep learning curve, and this kind of simple animations can be easily achieved.
I have 2 view controller with navigation controller. I'm pushing one firstViewController to secondViewController. i'm currently using
[self.navigationController pushViewController:detailsViewController animated:YES];
but the transition is secondView come in from the right. What i'm trying to do is secondView come in from the back and firstView fading out while secondView fading in.
Is there anyway to achieve this?
Push/Pop UIVIewController FadeIn/FadeOut in Swift
class FadeInPushSegue: UIStoryboardSegue {
var animated: Bool = true
override func perform() {
if var sourceViewController = self.sourceViewController as? UIViewController, var destinationViewController = self.destinationViewController as? UIViewController {
var transition: CATransition = CATransition()
transition.type = kCATransitionFade; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
sourceViewController.view.window?.layer.addAnimation(transition, forKey: "kCATransition")
sourceViewController.navigationController?.pushViewController(destinationViewController, animated: false)
}
}
}
class FadeOutPopSegue: UIStoryboardSegue {
override func perform() {
if var sourceViewController = self.sourceViewController as? UIViewController, var destinationViewController = self.destinationViewController as? UIViewController {
var transition: CATransition = CATransition()
transition.duration = 0.4
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionFade; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
sourceViewController.view.window?.layer.addAnimation(transition, forKey: "kCATransition")
sourceViewController.navigationController?.popViewControllerAnimated(false)
}
}
}
I have some small black flickering in the transition moment between controllers.
I have to load many things to the new controller before I show it, and I pass it some UIImage argument called "Art".
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller:ModelController = storyboard.instantiateViewController(withIdentifier: "ModController") as! ModelController
controller.artwork=art //pass this to load the new
let transition = CATransition()
transition.duration = 0.30
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
view.window!.layer.add(transition, forKey: kCATransition)
present(controller, animated: false, completion: nil)
On the new controller, I load everything after :
override func viewDidLoad() {
super.viewDidLoad()
//load scroller here
I am using CATransition for the next view to present over another from right. Now i want to control this view transition using swipe gesture so that user can swipe from left to right and next view present on screen with the thumb speed and position . So that user can feel the realness
my transition code is
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc : drawer = storyboard.instantiateViewControllerWithIdentifier("drawerID") as! drawer
let transition: CATransition = CATransition()
let timeFunc : CAMediaTimingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.duration = 0.50
transition.timingFunction = timeFunc
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight //kCATransitionFromLeft
self.navigationController!.view.layer.addAnimation(transition, forKey: kCATransition)
self.navigationController!.pushViewController(vc, animated: false)
Currently, I have a view controller presenting other view controller. What I'm trying to do is to recreate the default animation used when pushing a view controller.
My current approach is:
FirstViewController:
#IBAction private func push(sender: AnyObject) {
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SecondViewController")
let transition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
view.window?.layer.addAnimation(transition, forKey: kCATransition)
presentViewController(vc, animated: false, completion: nil)
}
SecondViewController:
#IBAction private func pop(sender: AnyObject) {
let transition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft
view.window?.layer.addAnimation(transition, forKey: kCATransition)
dismissViewControllerAnimated(false, completion: nil)
}
It is almost working, but I have a weird behaviour, I'm having a kind of black screen/flash when transitioning between view controllers. I already tried changing window.backgroundColor but it is not fixing the issue.
Thanks in advance 0_0...
The trouble is merely that what you're doing is not how to customize the animation for a present/dismiss transition. Apple has provided you with a clear, well-establish, official way to do that, and what you're doing is not it. You need to give your presented view controller a transitioningDelegate along with implementations of animationControllerForPresentedController: and animationControllerForDismissedController:, and implement the UIViewControllerAnimatedTransitioning protocol, possibly along with a custom UIPresentationController subclass.