两个视图之间的转换适用于 iOS8 但不适用于 iOS9

Transition between two views works on iOS8 but not on iOS9

我已经尝试了几个小时来让两个视图之间的过渡动​​画正常工作。一直出现的问题是,当我让代码在 ios8.1 上工作时,它在 ios9.1 上停止工作,反之亦然。问题很简单,我想过渡到的视图消失了。我知道有很多这样的主题,但 none 的答案对我有用,它总是对一个版本的 ios 有帮助,而对另一个版本则不起作用。 我在某处读到动画无法正常工作的问题是我使用了 AutoLayout 而解决方案之一是使用图层转换而不是这就是为什么我以适用于 [=24= 的代码版本结束] 但现在不适用于 iOS9... 这是我使用并适用于 iOS8

的代码
 class ChannelDetailsCollectionViewController: UICollectionViewController {
     @IBAction func backButtonPressed(sender: AnyObject) {
            self.navigationController?.popViewControllerAnimated(true)
        }
    }

class UIAnimatedNavigationControllerDelegate: NSObject, UINavigationControllerDelegate {
func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    if operation == .Push {
        return SlideInAnimator()
    } else if operation == .Pop {
        return SlideInDismissAnimator()
    } else {
        return nil
    }
}


 func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

    let containerView = transitionContext.containerView()!
    let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
    let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!

    let duration = self.transitionDuration(transitionContext)

    containerView.addSubview(toView)

    let transition = CATransition()
    transition.startProgress = 0
    transition.endProgress = 1
    transition.speed  = 3.0
    transition.type = kCATransitionPush
    transition.subtype = kCATransitionFromLeft
    transition.duration = 1.0

    fromView.layer.addAnimation(transition, forKey:"transition")
    toView.layer.addAnimation(transition, forKey:"transition")

    fromView.hidden = true
    toView.hidden = false
    transitionContext.completeTransition(true)
}

这是对 iOS9 有效的方法 在这个版本中,我看到当 toView.transform = CGAffineTransformIdentity 被调用时视图会在 iOS8 中消失,我认为这是因为自动布局。我已经尝试检查是否有零观看次数并再次添加它们,但仍然无法正常工作。

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
    let container = transitionContext.containerView()
    let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
    let fromView = fromViewController!.view
    let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
    let toView = toViewController!.view

    toView.transform = getSlideTransform(toView, offset: -container!.frame.width)
    fromView.transform = getSlideTransform(toView, offset: 0.0)
    container!.addSubview(toView)
    container!.addSubview(fromView)


    let duration = self.transitionDuration(transitionContext)

    UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.8, options: [], animations: {

        toView.transform = CGAffineTransformIdentity
        fromView.transform = self.getSlideTransform(fromView, offset: fromView.frame.width+100)
        }, completion: { finished in

            transitionContext.completeTransition(true)
    })
}


private func getSlideTransform(view: UIView, offset: CGFloat) -> CGAffineTransform {

    let size = view.frame.size
    var offSetTransform = CGAffineTransformMakeTranslation(offset, 0)
    offSetTransform = CGAffineTransformScale(offSetTransform, 0.9, 0.9)

    return offSetTransform
}

有人可以给我一些提示,告诉我现在可以尝试什么吗?因为我没有想法,正如我所说的,我检查过的所有主题都会解决 iOS 一个版本的问题并使另一个版本崩溃:/

因此,经过几个小时的尝试,我得到了适用于 iOS8 和 iOS9 的代码。

我正在为任何可能遇到与我类似的问题但无能为力的人发布工作代码

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
    let container = transitionContext.containerView()
    let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
    let fromView = fromViewController!.view
    let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
    let toView = toViewController!.view

    let duration = self.transitionDuration(transitionContext)

toView.frame = transitionContext.initialFrameForViewController(toViewController!)
fromView.frame = transitionContext.initialFrameForViewController(fromViewController!)

toView.transform = getSlideTransform(toView, offset: -container!.frame.width)
fromView.transform = getSlideTransform(toView, offset: 0.0)
container!.addSubview(toView)


UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.8, options: [], animations: {
fromView!.removeFromSuperview()
toView.transform = CGAffineTransformIdentity
fromView.transform = self.getSlideTransform(fromView, offset: fromView.frame.width)
toView.frame = transitionContext.finalFrameForViewController(toViewController!)
fromView.frame = transitionContext.finalFrameForViewController(fromViewController!)

    }, completion: { finished in

transitionContext.completeTransition(true)
})
}

在动画之前我必须使用 :initialFrameForViewControllertransitionContext 设置帧,在动画期间我必须使用 :finalFrameForViewControllertransitionContext 设置最终帧。它防止视图消失,可能它最终获得了正确的帧值。