使用UIViewControllerAnimatedTransitioning扩展单元动画

时间:2018-07-02 14:27:48

标签: ios swift animation cell

我有一个非常复杂的UITableViewCell(stackViews等),我想使用UIViewControllerAnimatedTransitioning

进行扩展以过渡到详细视图

当我将该单元格添加到containerView中时,就会出现问题,因为它使它从tableView中消失而无法恢复(我能找到)

    private func animatePush(using transitionContext: UIViewControllerContextTransitioning) {
        let containerView = transitionContext.containerView

        guard let fromView = transitionContext.view(forKey: .from),
            let toView = transitionContext.view(forKey: .to),
            let fromViewController = transitionContext.viewController(forKey: .from),
            let tableView = (fromViewController as? MyViewController)?.tableView ,
            let selectedIndexPath = tableView.indexPathForSelectedRow,
            let cell = tableView.cellForRow(at: selectedIndexPath),
            let fullCell = cell as? MyCell else {
                return
        }

//        let archive = NSKeyedArchiver.archivedData(withRootObject: fullCell.roundedView)
//        guard let roundViewObject = NSKeyedUnarchiver.unarchiveObject(with: archive),
//         let roundViewCopy = roundViewObject as? UIView else {
//            return
//        }
//
//        roundViewCopy.addConstraints(fullCell.roundedView.constraints)

        let fromCellView:UIView = fullCell.roundedView
        let originalFrame = fromCellView.frame

        var fromFrame: CGRect!
        let toFrame = toView.frame
        fromFrame = containerView.convert(fromCellView.frame, from: cell)
        fromCellView.frame = fromFrame
        toView.frame = fromFrame

        containerView.addSubview(toView)
        containerView.addSubview(fromView)
        containerView.addSubview(fromCellView)

        UIView.animate(withDuration: 0.4, animations: {
            fromCellView.frame = toFrame
            toView.frame = toFrame
            fromView.alpha = 0
            fromCellView.alpha = 0
        }) { (completed) in
            fromView.alpha = 1
            fromCellView.alpha = 1

            //tableView.reloadData()
            //fromView.setNeedsLayout()
            //fromView.layoutIfNeeded()
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)

        }
    }

效果很好,直到我回去,然后那个牢房不在那里。

我尝试将子视图添加到表视图中,并使用NSKeyedArchiver复制cellView,所以我不使用相同的引用(但是约束被删除并且结果不符合预期),到目前为止没有任何效果。

不能使用快照,因为随着扩展标签变得太大,我想保留大小。

谢谢。

0 个答案:

没有答案