ios - 在 Swift 4 中的 TabBar 下方绘制关闭过渡

标签 ios swift xcode ios-animations

我正在尝试实现类似于 iOS 11 App Store 中使用的 CardView。为此,我使用了一个 GitHub 项目 ( https://github.com/PaoloCuscela/Cards ) 并对其进行了一些调整。

问题在于,当从呈现的详细信息 View 转换回初始 View (位于 TabBarController 内)时,卡片被绘制在 TabBar 的前面(参见视频 https://youtu.be/qDb3JoISTdw),这给整个转换一种感觉“故障”外观。

这是我使用的过渡类的代码:

import UIKit

class Animator: NSObject, UIViewControllerAnimatedTransitioning {


fileprivate var presenting: Bool
fileprivate var velocity = 0.6
var bounceIntensity: CGFloat = 0.07
var card: Card

init(presenting: Bool, from card: Card) {
    self.presenting = presenting
    self.card = card
    super.init()
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

    // Animation Context Setup
    let container = transitionContext.containerView
    let to = transitionContext.viewController(forKey: .to)!
    let from = transitionContext.viewController(forKey: .from)!
    container.addSubview(to.view)
    container.addSubview(from.view)


    guard presenting else {

        // Detail View Controller Dismiss Animations
        card.isPresenting = false

        let detailVC = from as! DetailViewController
        let cardBackgroundFrame = detailVC.scrollView.convert(card.backgroundIV.frame, to: nil)
        let bounce = self.bounceTransform(cardBackgroundFrame, to: card.originalFrame)

        // Blur and fade with completion
        UIView.animate(withDuration: velocity/2, delay: 0, options: .curveEaseOut, animations: {
            detailVC.blurView.alpha = 0

        }, completion: nil)
        UIView.animate(withDuration: velocity, delay: 0, options: .curveEaseOut, animations: {
            detailVC.snap.alpha = 0
            self.card.backgroundIV.layer.cornerRadius = self.card.cardRadius

        }, completion: { _ in

            detailVC.layout(self.card.originalFrame, isPresenting: false, isAnimating: false)
            self.card.addSubview(detailVC.card.backgroundIV)
            transitionContext.completeTransition(true)
        })

        // Layout with bounce effect
        UIView.animate(withDuration: velocity/2, delay: 0, options: .curveEaseOut, animations: {

            detailVC.layout(self.card.originalFrame, isPresenting: false, transform: bounce)
            self.card.delegate?.cardIsHidingDetail?(card: self.card)

        }) { _ in UIView.animate(withDuration: self.velocity/2, delay: 0, options: .curveEaseOut, animations: {

            detailVC.layout(self.card.originalFrame, isPresenting: false)
            self.card.delegate?.cardIsHidingDetail?(card: self.card)
            })
        }
        return

    }

    // Detail View Controller Present Animations
    card.isPresenting = true

    let detailVC = to as! DetailViewController
    let bounce = self.bounceTransform(card.originalFrame, to: card.backgroundIV.frame)

    container.bringSubview(toFront: detailVC.view)
    detailVC.card = card
    detailVC.layout(card.originalFrame, isPresenting: false)

    // Blur and fade with completion
    UIView.animate(withDuration: velocity/2, delay: 0, options: .curveEaseOut, animations: {
        detailVC.blurView.alpha = 1

    }, completion: nil)

    UIView.animate(withDuration: velocity, delay: 0, options: .curveEaseOut, animations: {

        self.card.transform = CGAffineTransform.identity    // Reset card identity after push back on tap
        detailVC.snap.alpha = 1
        self.card.backgroundIV.layer.cornerRadius = 0

    }, completion: { _ in

        detailVC.layout(self.card.originalFrame, isPresenting: true, isAnimating: false, transform: .identity)
        transitionContext.completeTransition(true)
    })

    // Layout with bounce effect
    UIView.animate(withDuration: velocity/2, delay: 0, options: .curveEaseOut, animations: {

        detailVC.layout(detailVC.view.frame, isPresenting: true, transform: bounce)
        self.card.delegate?.cardIsShowingDetail?(card: self.card)

    }) { _ in UIView.animate(withDuration: self.velocity/2, delay: 0, options: .curveEaseOut, animations: {

        detailVC.layout(detailVC.view.frame, isPresenting: true)
        self.card.delegate?.cardIsShowingDetail?(card: self.card)

        })
    }

}

private func bounceTransform(_ from: CGRect, to: CGRect ) -> CGAffineTransform {

    let old = from.center
    let new = to.center

    let xDistance = old.x - new.x
    let yDistance = old.y - new.y

    let xMove = -( xDistance * bounceIntensity )
    let yMove = -( yDistance * bounceIntensity )

    return CGAffineTransform(translationX: xMove, y: yMove)
}


func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
    return velocity
}

}

我还没有在 iOS 中进行过转换,希望有人能告诉我如何在这里实现我想要的。

最佳答案

UITabBarController 使用自动调整大小掩码完成其所有布局。在这种情况下,您可以捕获 tabBar 将其添加到容器 View ,执行动画然后将其添加回其 Root View 。例如,使用 Cards 动画,您可以将 animateTransition(using transitionContext: UIViewControllerContextTransitioning) 更改为:

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

    // Animation Context Setup
    let container = transitionContext.containerView
    let to = transitionContext.viewController(forKey: .to)!
    let from = transitionContext.viewController(forKey: .from)!
    container.addSubview(to.view)
    container.addSubview(from.view)

    // If going to tab bar controller
    // Add tab bar above view controllers
    // Turn off interactions
    if !presenting, let tabController = to as? UITabBarController {
        tabController.tabBar.isUserInteractionEnabled = false
        container.addSubview(tabController.tabBar)
    }


    guard presenting else {

        // Detail View Controller Dismiss Animations
        card.isPresenting = false

        let detailVC = from as! DetailViewController
        let cardBackgroundFrame = detailVC.scrollView.convert(card.backgroundIV.frame, to: nil)
        let bounce = self.bounceTransform(cardBackgroundFrame, to: card.originalFrame)

        // Blur and fade with completion
        UIView.animate(withDuration: velocity, delay: 0, options: .curveEaseOut, animations: {

            detailVC.blurView.alpha = 0
            detailVC.snap.alpha = 0
            self.card.backgroundIV.layer.cornerRadius = self.card.cardRadius

        }, completion: { _ in

            detailVC.layout(self.card.originalFrame, isPresenting: false, isAnimating: false)
            self.card.addSubview(detailVC.card.backgroundIV)

            // Add tab bar back to tab bar controller's root view
            if let tabController = to as? UITabBarController {
                tabController.tabBar.isUserInteractionEnabled = true
                tabController.view.addSubview(tabController.tabBar)
            }
            transitionContext.completeTransition(true)
        })

        // Layout with bounce effect
        UIView.animate(withDuration: velocity/2, delay: 0, options: .curveEaseOut, animations: {

            detailVC.layout(self.card.originalFrame, isPresenting: false, transform: bounce)
            self.card.delegate?.cardIsHidingDetail?(card: self.card)

        }) { _ in UIView.animate(withDuration: self.velocity/2, delay: 0, options: .curveEaseOut, animations: {

            detailVC.layout(self.card.originalFrame, isPresenting: false)
            self.card.delegate?.cardIsHidingDetail?(card: self.card)
            })
        }
        return

    }

    // Detail View Controller Present Animations
    card.isPresenting = true

    let detailVC = to as! DetailViewController
    let bounce = self.bounceTransform(card.originalFrame, to: card.backgroundIV.frame)

    container.bringSubview(toFront: detailVC.view)
    detailVC.card = card
    detailVC.layout(card.originalFrame, isPresenting: false)

    // Blur and fade with completion
    UIView.animate(withDuration: velocity, delay: 0, options: .curveEaseOut, animations: {

        self.card.transform = CGAffineTransform.identity    // Reset card identity after push back on tap
        detailVC.blurView.alpha = 1
        detailVC.snap.alpha = 1
        self.card.backgroundIV.layer.cornerRadius = 0

    }, completion: { _ in

        detailVC.layout(self.card.originalFrame, isPresenting: true, isAnimating: false, transform: .identity)
        transitionContext.completeTransition(true)
    })

    // Layout with bounce effect
    UIView.animate(withDuration: velocity/2, delay: 0, options: .curveEaseOut, animations: {

        detailVC.layout(detailVC.view.frame, isPresenting: true, transform: bounce)
        self.card.delegate?.cardIsShowingDetail?(card: self.card)

    }) { _ in UIView.animate(withDuration: self.velocity/2, delay: 0, options: .curveEaseOut, animations: {

        detailVC.layout(detailVC.view.frame, isPresenting: true)
        self.card.delegate?.cardIsShowingDetail?(card: self.card)

        })
    }

}

产生的动画如下:

enter image description here

关于ios - 在 Swift 4 中的 TabBar 下方绘制关闭过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50967397/

相关文章:

ios - 删除 CKRecord : Record delete would violate validating reference, 拒绝更新

swift - 如何在快速点击返回后隐藏键盘

ios - 您在哪里更新应用程序内购买消耗品的服务器?

ios - 从 firestore 下载一系列文档

ios - Xamarin 表单 - ios 上的菜单图标

swift - 在迭代期间使用 forEach 从集合中删除元素

swift - 我正在尝试从本地标识符中获取 Assets 并在 collectionview 中显示图像,但它会导致滚动抖动

ios - Xcode 方案的 "analyze"部分中的 "test"、 "Build"和其他复选框是什么?

xcode - 如何在 XCode 4 中为 "Debug"配置全局添加预处理器定义

xcode - 如何在同一工作区中使用不同的配置名称编译具有应用程序和库的项目?