如何创建类似于Apple新闻应用程序的导航过渡? [英] How to create a navigation transition like the Apple news app?

查看:123
本文介绍了如何创建类似于Apple新闻应用程序的导航过渡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这篇文章可以创建类似于Apple新闻应用程序的导航过渡:

解最终方案

我找到了一个解决方案,可以简化源代码,也许不太优雅,但是有效。



Git已更新。

  func animateTransition(使用transitionContext:UIViewControllerContextTransitioning){
让我们演示=操作== .push

/ /确定我们要导航到的主视图和哪个详细视图。容器视图将包含用于过渡动画的视图。
let containerView = transitionContext.containerView

保护let fromView = transitionContext.view(forKey:UITransitionContextViewKey.from),
let toView = transitionContext.view(forKey:UITransitionContextViewKey.to)否则{
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
return
}

containerView.backgroundColor = fromView.backgroundColor

让mainView =呈现? fromView:toView
让detailView =呈现? toView:fromView

//确定动画细节视图的起始帧。当我们进行演示时,详细信息视图将超出缩略图框架。解雇后,详细信息视图将缩小到同一缩略图框中。
让finalFrame = presenting吗? detailView.frame:thumbnailFrame

// thumbnailFrame与
大小之间的比例因子let scaleFactorX = thumbnailFramework.size.width / detailView.frame.size.width
let scaleFactorY = thumbnailFrame .size.height / detailView.frame.size.height

如果显示{

//缩小初始帧的细节视图。详细视图将在下面缩放为CGAffineTransformIdentity。
detailView.transform = CGAffineTransform(scaleX:scaleFactorX,y:scaleFactorY)
detailView.center = CGPoint(x:thumbnailFrame.midX,y:thumbnailFrame.midY)
detailView.clipsToBounds = true $ b / b}


//设置主视图和局部视图的Alpha初始状态,以便我们可以在动画期间淡入和淡出它们。
detailView.alpha =呈现? 0:1
mainView.alpha =正在显示? 1:0

//将要转换的视图添加到包含动画的容器视图中。
containerView.addSubview(toView)
containerView.bringSubview(toFront:detailView)


//为过渡设置动画。
UIView.animate(withDuration:持续时间,延迟:0.0,usingSpringWithDamping:1,initialSpringVelocity:1.0,选项:.curveEaseInOut,动画:{
//淡入和淡出主视图和局部视图。
detailView.alpha =呈现?1:0
mainView.alpha =呈现?0:1

如果呈现{
detailView.transform = CGAffineTransform.identity
detailView.center = CGPoint(x:finalFrame.midX,y:finalFrame.midY)
}
else {
detailView.transform = CGAffineTransform(scaleX:scaleFactorX,y:scaleFactorY)
detailView.center = CGPoint(x:self.thumbnailFrame.midX,y:self.thumbnailFrame.midY)
}


}){在
transitionContext中完成.completeTransition(完成)
}
}


I found this article to create a navigation transition like the Apple news app: https://blog.rocketinsights.com/how-to-create-a-navigation-transition-like-the-apple-news-app/.

The transition is a zoom effect.

The code works great for push animation, but for pop animation (to close DetailViewController), I have a black screen instead my main viewcontroller.

As the article doesn't provide the full source code to download, I publish it on github, apply to UICollectionViewController for my needs: testZoomTransition

解决方案

I found a solution, by simplifying the source code, maybe less elegant, but effective.

Git is updated.

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    let presenting = operation == .push

    // Determine which is the master view and which is the detail view that we're navigating to and from. The container view will house the views for transition animation.
    let containerView = transitionContext.containerView

    guard let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from),
        let toView = transitionContext.view(forKey: UITransitionContextViewKey.to) else {
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
            return
    }

    containerView.backgroundColor = fromView.backgroundColor

    let mainView = presenting ? fromView : toView
    let detailView = presenting ? toView : fromView

    // Determine the starting frame of the detail view for the animation. When we're presenting, the detail view will grow out of the thumbnail frame. When we're dismissing, the detail view will shrink back into that same thumbnail frame.
    let finalFrame = presenting ? detailView.frame : thumbnailFrame

    //scale factor between thumbnailFrame and size of
    let scaleFactorX = thumbnailFrame.size.width / detailView.frame.size.width
    let scaleFactorY = thumbnailFrame.size.height / detailView.frame.size.height

    if presenting {

        // Shrink the detail view for the initial frame. The detail view will be scaled to CGAffineTransformIdentity below.
        detailView.transform = CGAffineTransform(scaleX: scaleFactorX, y: scaleFactorY)
        detailView.center = CGPoint(x: thumbnailFrame.midX, y: thumbnailFrame.midY)
        detailView.clipsToBounds = true
    }


    // Set the initial state of the alpha for the master and detail views so that we can fade them in and out during the animation.
    detailView.alpha = presenting ? 0 : 1
    mainView.alpha = presenting ? 1 : 0

    // Add the view that we're transitioning to to the container view that houses the animation.
    containerView.addSubview(toView)
    containerView.bringSubview(toFront: detailView)


    // Animate the transition.
    UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
        // Fade the master and detail views in and out.
        detailView.alpha = presenting ? 1 : 0
        mainView.alpha = presenting ? 0 : 1

        if presenting {
            detailView.transform = CGAffineTransform.identity
            detailView.center = CGPoint(x: finalFrame.midX, y: finalFrame.midY)
        }
        else {
            detailView.transform = CGAffineTransform(scaleX: scaleFactorX, y: scaleFactorY)
            detailView.center = CGPoint(x: self.thumbnailFrame.midX, y: self.thumbnailFrame.midY)
        }


    }) { finished in
        transitionContext.completeTransition(finished)
    }
}

这篇关于如何创建类似于Apple新闻应用程序的导航过渡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆