قالب وردپرس درنا توس
Home / IOS Development / IOS transition Coordinator returns zero for custom container display control in IOS8

IOS transition Coordinator returns zero for custom container display control in IOS8



As Matt said in this previous question:

So, since you can't even get a transition coordinator in a situation
where you are allowed to write a custom transition animation for a
built-in parental controls, obviously the chances of you getting it
one in a situation where you try to do your own parental show
the controller is zero

But according to the transition coordinator, it is overall:

Container display controllers can override this method, but in most cases
shouldn't need. If you override this method, you must first call super
to see if there is a suitable transition coordinator to return,
and, if it is, return it.

So I wanted to try to make my own coordinator for my own VC container.

Here's an example:

  class PropertyAnimatorTransitionCoordinator: NSObject, UIViewControllerTransitionCoordinator, UIViewControllerTransitionCoordinatorContext {

privately let parentView: UIView
privately added from ViewController: UIViewController?
privately addedViewController: UIViewController
private la animator: UIViewPropertyAnimator

// MARK: - Life cycle

init (parentView: UIView,
fromViewController: UIViewController?,
toViewController: UIViewController,
animator: UIViewPropertyAnimator) {
self.parentView = parentView
self.fromViewController = fromViewController
self.toViewController = toViewController
self.animator = animator
}

// MARK: - UIViewControllerTransitionCoordinator

func animate (alongsideTransition animation: ((UIViewControllerTransitionCoordinatorContext) -> Feid)?
completion: ((UIViewControllerTransitionCoordinatorContext) -> Invalid)? = zero) -> Bool {
var isSuccessful = false
if la animation = animation {
animator.addCompletion {[weak self] _ in
guard leave context = even other {return}
animation (context)
}
isSuccessful = true
}
if let completion = completion {
animator.addCompletion {[weak self] _ in
guard leave context = even other {return}
completion (context)
}
isSuccessful = true
}
Returns are successful
}

(UIViewControllerTransitionCoordinatorContext) -> Void), completion: ((UIViewControllerTransitionCoordinatorContext) -> Void)? = nil) -> Bool {
return animate (alongsideTransition: animation, completion: completion)
}

func notifyWhenInteractionEnds (_ trades: @escaping (UIViewControllerTransitionCoordinatorContext) -> Fat) {
animator.addCompletion {[weak self] _ in
guard leave context = even other {return}
handles (context)
}
}

func notifyWhenInteractionChanges (_ trades: @escaping (UIViewControllerTransitionCoordinatorContext) -> Feid) {
animator.addCompletion {[weak self] _ in
guard leave context = even other {return}
handles (context)
}
}

// MARK: - UIViewControllerTransitionCoordinatorContext

was isOther: Bool {
return true
}

was presentationStyle: UIModalPresentationStyle {
return .none
}

was initiallyInteractive: Bool {
return false
}

where isInterruptible: Bool {
return animator.isInterruptible
}

where is Interactive: Bool {
return animator.isUserInteractionEnabled
}

was isCancelled: Bool {
return! animator.isRunning
}

var transitionDuration: TimeInterval {
return animator.duration
}

was percent complete: CGFloat {
return animator.fractionComplete
}

was completion Velocity: CGFloat {
return 0
}

was completionCurve: UIView.AnimationCurve {
return animator.timingParameters? .cubicTimingParameters ?. animationCurve ?? .linear
}

var targetTransform: CGAffineTransform {
return the .identity
}

was containerView: UIView {
return parentView
}

func viewController (for Key Key: UITransitionContextViewControllerKey) -> UIViewController? {
change key {
case. from:
return from ViewController
case .to:
return to ViewController
default:
return zero
}
}

func view (for Key Key: UITransitionContextViewKey) -> UIView? {
change key {
case. from:
go back from ViewController? .view
case .to:
return to ViewController.view
default:
return zero
}
}
}

In my custom container I would use it like this:

  class CustomContainerViewController: UIViewController {UIViewController {

private was customTransitionCoordinator: UIViewControllerTransitionCoordinator?

override was transition coordinator: UIViewControllerTransitionCoordinator? {
if la coordinator = super.transitionCoordinator {
Returns coordinator
}
return the customTransitionCoordinator
}

override was shouldAutomaticallyForwardAppearanceMethods: Bool {
return false
}

func insertNewChild (_ viewController: UIViewController) {
la animator = UIViewPropertyAnimator (duration: 0.3, curve: .easeInOut)
customTransitionCoordinator = PropertyAnimatorTransitionCoordinator (
parentView: show,
fromViewController: null,
toViewController: viewController,
animator: animator
)
animator.addCompletion {[weak self] _ in
guard let parents = even other {return}
viewController.didMove (toParent: parents)
self? .customTransitionCoordinator = null
}
addChild (viewController)
viewController.beginAppearanceTransition (true, animated: true)
view.addSubview (viewController.view)
la target = view.bounds
viewController.view.frame = target
viewController.view.frame.origin.x = -target.width
view.layoutIfNeeded ()
animator.addAnimations {
viewController.view.frame = target
}
animator.addCompletion {[weak self] _ in
guard let parents = even other {return}
viewController.endAppearanceTransition ()
viewController.didMove (toParent: parents)
self? .customTransitionCoordinator = null
}
animator.startAnimation ()
}
}

Of course, no edge cases are handled. It is a very basic example.


Source link