静态表视图动画

时间:2016-04-14 11:00:36

标签: ios swift uitableview animation

我最近一直在尝试重现动画,但是尽管进行了所有的测试和研究,但我找不到如何处理这种动画的正确解决方案。

我已经制作了我正在谈论的动画的GIF(摘自“Captain Train”iOS应用程序):Animation GIF

它的基本功能是用各种视图替换tableView,并且非常好地动画导航栏项。

到目前为止我做了什么:

我通过创建类似这样的类来创建自定义转换控制器:

class TransitionManager: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate  {

// MARK: UIViewControllerAnimatedTransitioning protocol methods
var openingFrame: CGRect?

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

    let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
    let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
    if let containerView = transitionContext.containerView() {

        let animationDuration = self .transitionDuration(transitionContext)

        let snapshotView = toViewController.view.resizableSnapshotViewFromRect(toViewController.view.frame, afterScreenUpdates: true, withCapInsets: UIEdgeInsetsZero)
        snapshotView.frame = openingFrame!
        containerView.addSubview(snapshotView)

        toViewController.view.alpha = 0.0
        containerView.addSubview(toViewController.view)

        UIView.animateWithDuration(animationDuration, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 1.0, options: .TransitionNone,
            animations: { () -> Void in
                snapshotView.frame = fromViewController.view.frame
            }, completion: { (finished) -> Void in
                snapshotView.removeFromSuperview()
                toViewController.view.alpha = 1.0

                transitionContext.completeTransition(finished)
        })

    }
}

// return how many seconds the transiton animation will take
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
    return 0.45
}

// MARK: UIViewControllerTransitioningDelegate protocol methods
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    return self
}

// return the animator used when dismissing from a viewcontroller
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    return self
}

}

转换正常,我可以点击一个表格视图项目,然后动画过渡到另一个,让我的新视图适合单元格的边界,然后全屏显示。

当用户点击单元格时触发的功能如下:

    func presentDetailViewAnimated() {
        let transitionManager = TransitionManager()
        transitionManager.openingFrame = placeTableViewCell.frame

       let controller = self.storyboard?.instantiateViewControllerWithIdentifier("testAnimation")
    controller?.transitioningDelegate = transitionManager
       self.presentViewController(controller!, animated: true, completion: nil)   
    }

这很好,但它取代了视图。我正在寻找的动画发生在tableView中,并以某种方式替换内容,这就是我迷失的地方。

我对IOS动画没有超级经验,所以我不知道从哪里开始实现这样的结果。我甚至不知道这个视图真的是一个tableView,或者他们只是伪造它,一切都是以自定义的方式处理。

我希望我的帖子足够清楚,任何评论/想法都会受到赞赏。 :)

0 个答案:

没有答案