how to manually adjust view switching speed with swipe gesture - ios

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)

Related

transition between ViewControllers not animating correct VC

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.

How to achieve plain standard slide-from-right transition without UINavigationController?

To me the transition from right to left while opening new view is the most essential part of iPhone UI. How can it be that nearly in 2020 there is no way to have it for transition between two ViewControllers? At least I could not find the way.
Due to reasons I am not using NavigationController in the app. Instead I just call self.present(newVC) to open new one and self.dismiss() to go back.
I just want the new ViewController to slide from the right, that's all.
Any hints?
It seems you want to slide from right to left.
So you can do it using a custom segue transition.
First Create CATransition extenstion than add layer to your segue .
func rightToLeft() -> CATransition {
self.duration = 0.1 //set the duration to whatever you'd like.
self.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
self.type = kCATransitionReveal
self.subtype = kCATransitionFromRight
return self
}
}
Implementation :
DispatchQueue.main.async { //make sure all UI updates are on the main thread.
nav?.view.layer.add(CATransition().rightToLeft(), forKey: nil)
nav?.pushViewController(YourViewController(), animated: false)
}
So you want just slide from right to left here is code :
let transition = CATransition()
transition.duration = 0.3
transition.type = CATransitionType.push
transition.subtype = CATransitionSubtype.fromRight
transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
view.window!.layer.add(transition, forKey: kCATransition)
let presentedVC = self.storyboard!.instantiateViewController(withIdentifier: "PresentingViewController") \\ view Controller identifire in storyboard
presentedVC.view.backgroundColor = UIColor.orange
present(presentedVC, animated: false, completion: nil)
Here is Sample project

How to segue with left to right animation and alpha 0 to 1

I just want to go to next ViewController using segue and the next ViewController will come from left to right with alpha 0 at the starting and 1 at the when next ViewController is on display.
Here is the code i tried so far without any luck,
let transition = CATransition()
transition.duration = 1.0
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
transition.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)
self.view.window!.layer.add(transition, forKey: kCATransition)
self.performSegue(withIdentifier: "login", sender: self)
You can use "Push" segue for left to write.
And write an animate in ViewDidload() in SecondVC.
self.view1.alpha = 0
UIView.animate(withDuration: 0.1) {
self.view1.alpha = 1
}

iOS presenting view controller animated as 'Push' (right-left animation)

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.

Make UIModalTransitionStyle Look like push in swift

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 :)

Resources