Swift 动画 —— controller转场动画

本文介绍了如何使用UIViewControllerAnimatedTransitioning和UIViewControllerTransitioningDelegate在Swift中实现控制器转场动画。通过创建PresentViewControllerAnimation类,实现login button沿Y轴顺时针旋转90度的入场动画,以及DismissViewControllerAnimation类完成退出动画。同时,利用SwipeInteractiveTransition处理滑动手势,实现平滑的交互体验。
摘要由CSDN通过智能技术生成

Controller的转场动画主要是通过UIViewControllerAnimatedTransitioning 和 UIViewControllerTransitioningDelegate 来完成。
实现效果:
请添加图片描述
首先,创建一个class PresentViewControllerAnimation,继承自NSObject,遵守UIViewControllerAnimatedTransitioning。然后创建一个初始化方法,声明一个originFrame来保存初始化的frame。这里遵守UIViewControllerAnimatedTransitioning之后需要实现两个方法,一个是transitionDuration,一个是animateTransition。transitionDuration是代表动画时长,那么animateTransition里面就是动画的实现。这里

  • login button 沿y轴顺时针旋转90度。
  • 以login button 的大小在该位置snapshot,但初始位置在y轴上逆时针90度,这样让 login button 旋转看起来连续。
  • 然后沿着y轴顺时针旋转90度。
  • 放大snapshot填满屏幕。

动画完了之后,将toview 显示出来,将snapshot移除,将 login button 位置还原。记得还需要调用transitionContext.completeTransition方法,否则页面会卡着不动。

class PresentViewControllerAnimation: NSObject,UIViewControllerAnimatedTransitioning {
    private let originFrame: CGRect

    init(originFrame: CGRect) {
        self.originFrame = originFrame
    }
    
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 2.0
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        guard let fromVC = transitionContext.viewController(forKey: .from) as? LoginViewController,
            let toVC = transitionContext.viewController(forKey: .to) as? GiftViewController,
            let snapshot = toVC.view.snapshotView(afterScreenUpdates: true)
            else {
                return
        }

        let containerView = transitionContext.containerView
        let finalFrame = transitionContext.finalFrame(for: toVC)

        snapshot.frame = originFrame
        snapshot.layer.cornerRadius = originFrame.width / 2
  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值