The story of one view controller that wanted to show up beautifully
There was a modest view controller
VCYellow
. And he had no picture, no text, not even a tiny business logic. He lived a normal view-controller life.
ancient scrolls how to use the spell and began to prepare.
I wrote myself a cheat sheet with the spell itself, so as not to forget:
extension VCYellow: UIViewControllerTransitioningDelegate {
func animationController (forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return AnimatorPresent (startFrame: self.startFrame)
}
func animationController (forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return AnimatorDismiss (endFrame: self.startFrame)
}
}
In it, he carefully painted that for the show you need to use the animator AnimatorPresent , and when closing AnimatorDismiss .
Well, as a help to both animators it was decided to transfer the frame of the main button from VCMain
And then he himself was morally adjusted. Because without a right attitude, as is known, no magic works:
override func viewDidLoad () {
super.viewDidLoad ()
self.modalPresentationStyle = .custom
self.transitioningDelegate = self
}
He asked his friend VCMain Present yourself to check how the magic works and it worked no way
It turned out that AnimatorPresent and AnimatorDismiss do not appear by themselves.
It was already late to stop, and our hero decided to create the necessary animators. I dig in the necessary section ancient scrolls and learned that to create an animator, two things are enough.
First, you need to set the time allocated for the animation:
func transitionDuration (using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return ???r3r3341.}
and secondly designate the animation itself:
func animateTransition (using transitionContext: UIViewControllerContextTransitioning) {
//1
guard let vcTo = transitionContext.viewController (forKey: .to),
let snapshot = vcTo.view.snapshotView (afterScreenUpdates: true) else {
return
}
//2
let vContainer = transitionContext.containerView
//3
vcTo.view.isHidden = true
vContainer.addSubview (vcTo.view)
//4
snapshot.frame = self.startFrame
vContainer.addSubview (snapshot)
UIView.animate (withDuration: 0.? animations: {
//5
Snapshot.frame = (transitionContext.finalFrame (for: vcTo))
}, Completion: {success in
//6
VcTo. view.isHidden = false
snapshot.removeFromSuperview ()
transitionContext.completeTransition (true)
})
}
-
- Pull out the presented view controller (in our case VCYellow) and take it. Photo is needed to simplify the animation.
- Get a view on which will be animated witchcraft. Let's call it the context.
- Hook the final controller's view to the context and hide it. Show
- it was decided after the animation is over.
- Prepare a picture for animation. Reduce to the initial size and throw the context.
- Split the picture into the entire screen, thereby animating the presentation process.
- After the end of the animation show the real view of the final controller,
- get rid of the pictures and report that the action is over.
As a result, there was such an animator for the show:
import UIKit
class AnimatorPresent: NSObject, UIViewControllerAnimatedTransitioning {
let startFrame: CGRect
init (startFrame: CGRect) {
self.startFrame = startFrame
}
func transitionDuration (using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return ???r3r3341.}
func animateTransition (using transitionContext: UIViewControllerContextTransitioning) {
guard let vcTo = transitionContext.viewController (forKey: .to),
let snapshot = vcTo.view.snapshotView (afterScreenUpdates: true) else {
return
}
let vContainer = transitionContext.containerView
vcTo.view.isHidden = true
vContainer.addSubview (vcTo.view)
snapshot.frame = self.startFrame
vContainer.addSubview (snapshot)
UIView.animate (withDuration: 0.? animations: {
.snapshot.frame = (transitionContext.finalFrame (for: vcTo))
}, Completion: {success in
.vcTo.view.isHidden = false
Snapshot.removeFromSuperview ()
TransitionContext.completeTransition (true)
})
}
}
And after that it was easy to write an animator for hiding, which does about the same thing, but on the contrary:
import UIKit
class AnimatorDismiss: NSObject, UIViewControllerAnimatedTransitioning {
let endFrame: CGRect
init (endFrame: CGRect) {
self.endFrame = endFrame
}
func transitionDuration (using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return ???r3r3341.}
func animateTransition (using transitionContext: UIViewControllerContextTransitioning) {
guard let vcTo = transitionContext.viewController (forKey: .to),
let vcFrom = transitionContext.viewController (forKey: .from),
let snapshot = vcFrom.view.snapshotView (afterScreenUpdates: true) else {
return
}
let vContainer = transitionContext.containerView
vContainer.addSubview (vcTo.view)
vContainer.addSubview (snapshot)
vcFrom.view.isHidden = true
UIView.animate (withDuration: 0.? animations: {
? snapshot.frame = self.endFrame
}, Completion: {success in
TransitionContext.completeTransition (true)
})
}
}
Having finished all the dodings,
VCYellow
again asked his friend
VCMain
present yourself and a miracle!
Magic worked! Dream
VCYellow
has come true! Now he can show up and hide as he wants and nothing will limit his imagination!
The project-example can be downloaded here
The article I used for inspiration is here
It may be interesting
weber
Author29-09-2018, 19:30
Publication DateDevelopment / Programming
Category- Comments: 0
- Views: 260
entegrasyon programları
entegrasyon programları
Corvus Health provides medical training services as well as recruiting high quality health workers for you or placing our own best team in your facility. Check Out: Health Workforce Recruitment
I.T HATCH offers a wide range of IT services including remote access setup, small business servers, data storage solutions, IT strategy services, and more. Check Out: IT strategy services