После X> = 4 часов работы это работает без "разрывов" или других фоновых артефактов:
class AboutTransition: NSObject, UIViewControllerAnimatedTransitioning {
let presenting: Bool
let duration: NSTimeInterval
init(presenting: Bool, duration: NSTimeInterval = 0.25) {
self.presenting = presenting
self.duration = duration
}
@objc func transitionDuration(ctx: UIViewControllerContextTransitioning) -> NSTimeInterval {
return duration
}
@objc func animateTransition(ctx: UIViewControllerContextTransitioning) {
let duration = transitionDuration(ctx)
let containerView = ctx.containerView()
let fromViewController = ctx.viewControllerForKey(UITransitionContextFromViewControllerKey)!
let toViewController = ctx.viewControllerForKey(UITransitionContextToViewControllerKey)!
let fromView = fromViewController.view // 7.0 & 8.0 compatible vs. viewForKey:
let toView = toViewController.view // 7.0 & 8.0 compatible vs. viewForKey:
containerView.addSubview(fromView)
containerView.addSubview(toView)
let offScreenRight = CGAffineTransformMakeTranslation(containerView.frame.width, 0)
let offScreenLeft = CGAffineTransformMakeTranslation(-containerView.frame.width, 0)
fromView.transform = CGAffineTransformIdentity
toView.transform = self.presenting ? offScreenRight : offScreenLeft
UIView.animateWithDuration(duration, delay:0, options:UIViewAnimationOptions(0), animations: {
fromView.transform = self.presenting ? offScreenLeft : offScreenRight
toView.transform = CGAffineTransformIdentity
}, completion: { (finished: Bool) in
ctx.completeTransition(finished)
if finished {
fromView.removeFromSuperview()
fromView.frame = toView.frame // reset the frame for reuse, otherwise frame transforms will accumulate
}
})
}
}
Затем в AppDelegate.swift
:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UINavigationControllerDelegate {
lazy var window: UIWindow? = {
return UIWindow(frame: UIScreen.mainScreen().bounds)
}()
lazy var storyboard = UIStoryboard(name: "Main", bundle: nil)
lazy var nav: UINavigationController = {
var r = self.storyboard.instantiateInitialViewController() as! UINavigationController
r.delegate = self
return r
}()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window!.rootViewController = nav
self.window!.makeKeyAndVisible()
// .. other setup
return true
}
// ...
// MARK: - UINavigationControllerDelegate
func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
// Example of a SettingsViewController which pushes AboutViewController
if fromVC is SettingsViewController && toVC is AboutViewController { // Push to About
return AboutTransition(presenting: true)
} else if fromVC is AboutViewController && toVC is SettingsViewController { // Pop to Settings
return AboutTransition(presenting: false)
}
return nil
}
}
Это предполагает, что приложение использует раскадровку по умолчанию с начальным VC как UINavigationController
Обновление для swift 3/4
class LTRTransition: NSObject, UIViewControllerAnimatedTransitioning
{
let presenting: Bool
let duration: TimeInterval
init(presenting: Bool, duration: TimeInterval = Theme.currentTheme.animationDuration) {
self.presenting = presenting
self.duration = duration
}
@objc func transitionDuration(using ctx: UIViewControllerContextTransitioning?) -> TimeInterval {
return duration
}
@objc func animateTransition(using ctx: UIViewControllerContextTransitioning) {
let duration = transitionDuration(using: ctx)
let containerView = ctx.containerView
let fromViewController = ctx.viewController(forKey: UITransitionContextViewControllerKey.from)!
let toViewController = ctx.viewController(forKey: UITransitionContextViewControllerKey.to)!
guard let fromView = fromViewController.view, // 7.0 & 8.0 compatible vs. viewForKey:
let toView = toViewController.view // 7.0 & 8.0 compatible vs. viewForKey:
else {
assertionFailure("fix this")
return
}
containerView.addSubview(fromView)
containerView.addSubview(toView)
let offScreenRight = CGAffineTransform(translationX: containerView.frame.width, y: 0)
let offScreenLeft = CGAffineTransform(translationX: -containerView.frame.width, y: 0)
fromView.transform = CGAffineTransform.identity
toView.transform = self.presenting ? offScreenRight : offScreenLeft
UIView.animate(withDuration: duration, delay:0, options:UIViewAnimationOptions(rawValue: 0), animations: {
fromView.transform = self.presenting ? offScreenLeft : offScreenRight
toView.transform = CGAffineTransform.identity
}, completion: { (finished: Bool) in
ctx.completeTransition(finished)
if finished {
fromView.removeFromSuperview()
// this screws up dismissal. at least on ios10 it does fromView.frame = toView.frame // reset the frame for reuse, otherwise frame transforms will accumulate
}
})
}
}
И реализация transitioniningDelegate для VC, которая выдвигается / удаляется:
extension WhateverVCyouArePresentingFrom: UIViewControllerTransitioningDelegate
{
public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?
{
return LTRTransition(presenting: true)
}
public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
{
return LTRTransition(presenting: false)
}
}