简体   繁体   English

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

[英]Transition between two views works on iOS8 but not on iOS9

I've been trying for a few hours now to get the transition animation work between two views.我已经尝试了几个小时来让两个视图之间的过渡动​​画工作。 The problem that appears all the time that when I get the code to work on ios8.1 it stops working on ios9.1 and the other way around.一直出现的问题是,当我让代码在 ios8.1 上工作时,它停止在 ios9.1 上工作,反之亦然。 The problem is simply that the view I want to transition to disappears.问题很简单,我想转换到的视图消失了。 I know that there were a lot of topics like that but none of the answers worked for me, it always helped for one version of ios and for the other one it would not work.我知道有很多这样的主题,但没有一个答案对我有用,它总是对一个版本的 ios 有帮助,而对于另一个版本则不起作用。 I've read somewhere that the problem with the animations not working as they should is that I use AutoLayout and one of the solutions for that was to use layer transforms instead thats why I ended with a version of code that works on iOS8 but now doesn't work on iOS9... Here is the code that I use and works for iOS8我在某处读到动画无法正常工作的问题是我使用 AutoLayout 而解决方案之一是使用图层转换而不是这就是为什么我以适用于 iOS8 的代码版本结束但现在没有不适用于 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)
}

And here is what worked for iOS9 In this version I saw that the view would dissapear in iOS8 when toView.transform = CGAffineTransformIdentity was called, I assume its because of autolayout.这是适用于 iOS9 的方法在这个版本中,我看到当 toView.transform = CGAffineTransformIdentity 被调用时,视图会在 iOS8 中消失,我认为这是因为自动布局。 I've tried checking for nil views and adding them again but it still wouldn't work.我试过检查 nil 视图并再次添加它们,但它仍然不起作用。

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
}

Can somebody give me some tips what to try now?有人可以给我一些提示,现在该尝试什么吗? Because I'm out of ideas and as I said all the topics I've checked would solve a problem for one ver of iOS and crash the other one :/因为我没有想法,而且正如我所说,我检查过的所有主题都可以解决一个版本的 iOS 的问题,并使另一个版本崩溃:/

So after few more hours of trying I got the code to work on both iOS8 and iOS9.因此,经过几个小时的尝试,我让代码可以在 iOS8 和 iOS9 上运行。

I'm posting the working code for anyone who may have similar problem to mine and wasn't able to do anything about it我正在为可能与我有类似问题但无能为力的任何人发布工作代码

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

Before animation I had to set the frame from the transitionContext using :initialFrameForViewController and during animations I had to set the final frames from transitionContext using :finalFrameForViewController .在动画之前,我必须使用:initialFrameForViewControllertransitionContext设置帧,在动画期间我必须使用:finalFrameForViewControllertransitionContext设置最终帧。 It prevented the view from disappearing, probably it finally got the right frame values.它阻止了视图消失,可能它最终获得了正确的帧值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM