码迷,mamicode.com
首页 > 编程语言 > 详细

自定义presentViewController的转场动画(Swift)

时间:2016-04-19 15:44:41      阅读:363      评论:0      收藏:0      [点我收藏+]

标签:

原创Blog,转载请注明出处

我的StackFlow
技术分享


前言:

iOS默认的presentViewController的切换动画是从底部推入,消失是从顶部推出。但是,因为iOS系统默认的是适配所有转场上下文的。而针对特定的转场上下文,我们能做出更好的效果。

Tips:所谓的转场上下文,就是转场的开始View和结束View,以及对应的ViewController


目标效果

最终的效果
技术分享


准备工作

首先写出一个CollectionView,每个Cell是一个图片,由于本文的核心是如何转场,所以CollectionView的部分略过。写完了之后,是这样的效果

点击某一个CollectionView Cell查看大图,再点击大图图片消失

技术分享技术分享

这个最初的项目,可以在这里下载

CSDN下载


如何实现自定义转场动画

iOS 8之后,我们可以通过设置ViewControllertransitioningDelegate来设置代理来处理转场动画。

通过文档,可以看到transitioningDelegate是一个实现UIViewControllerTransitioningDelegate协议的对象,先看看这个协议,本文主要利用以下两个方法

  • animationControllerForDismissedController
  • animationControllerForPresentedController

着两个方法的目的是,提供一个遵循UIViewControllerAnimatedTransitioning协议的对象,然后又这个对象来实际处理专场。通过名字就可以看出来,一个是处理present一个是处理dismiss。

本文的设计是让ViewController来处理转场,在didSelectItemAtIndexPath中,设置pvc的转场代理

dvc.transitioningDelegate = self

然后,写一个extension来实现协议

extension ViewController:UIViewControllerTransitioningDelegate{
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return nil
    }
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return nil
    }
}

这时候,再运行项目,会发现没有任何变化,还是默认的转场方式。因为我们还没有提供实际的动画 。当上述两个代理方法返回nil的时候,系统会使用默认的方式


实现UIViewControllerAnimatedTransitioning

新建一个文件,命名为Animator.swift,然后新建一个类,处理Present的转场(Dismiss类似),实现UIViewControllerAnimatedTransitioning协议

class PresentAnimator: NSObject,UIViewControllerAnimatedTransitioning{

这时候,会报错没有实现协议,然后,我们添加协议方法,这时候的Animator.swift如下

import Foundation
import UIKit

class PresentAnimator: NSObject,UIViewControllerAnimatedTransitioning{
    let duration = 0.5 //动画的时间
    var originFrame = CGRectZero //点击Cell的frame
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return duration
    }
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

    }
}

简单介绍下这里的协议方法

  • transitionDuration,返回转场动画的时间
  • animateTransition,进行实际的转场动画,通过参数transitionContext(转场上下文)来获取转场的fromView,toView,fromViewController,toViewController。

转场的原理

技术分享

  • 转场开始的时候,自动把FromView添加到转场ContainView
  • 转场结束的时候,自动把FromView移除ContainView

所以,开发者要做的就是

  1. 把toView添加到转场ContainView中,并且定义好toView的初始位置和状态
  2. 定义好FromView和ToView的转场结束时候的状态
  3. 创建动画

实现实际的转场动画

基于上述的原理,我们修改实际的动画

     func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let containView = transitionContext.containerView()
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!

        let finalFrame = toView.frame

        let xScale = originFrame.size.width/toView.frame.size.width
        let yScale = originFrame.size.height/toView.frame.size.height
        toView.transform = CGAffineTransformMakeScale(xScale, yScale)
        toView.center = CGPointMake(CGRectGetMidX(originFrame), CGRectGetMidY(originFrame))

        containView?.addSubview(toView)
        UIView.animateWithDuration(duration, animations: { () -> Void in
            toView.center = CGPointMake(CGRectGetMidX(finalFrame), CGRectGetMidY(finalFrame))
            toView.transform = CGAffineTransformIdentity
            }) { (finished) -> Void in
                transitionContext.completeTransition(true)
        }
    }

然后,在ViewController.swfit中,添加一属性

    let presentAnimator = PresentAnimator()

修改didSelectItemAtIndexPath

    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let dvc = DetailViewController()
        dvc.image = UIImage(named: "image.jpg")
        dvc.transitioningDelegate = self
        let cell =  collectionView.cellForItemAtIndexPath(indexPath) as! FullImageCell
        presentAnimator.originFrame = cell.convertRect(cell.imageview.frame, toView: nil)
        self.presentViewController(dvc, animated: true, completion: nil)
    }

修改代理方法

    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return presentAnimator
    }

这时候的动画效果如Gif
技术分享


同理,为dismiss添加转场动画

在Animator.swift中添加一个新的类

class DismisssAnimator:NSObject,UIViewControllerAnimatedTransitioning{
    let duration = 0.6
    var originFrame = CGRectZero
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return duration
    }
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let containView = transitionContext.containerView()
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)! //Collection View
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)! //全屏的imageview
        let xScale = originFrame.size.width/toView.frame.size.width
        let yScale = originFrame.size.height/toView.frame.size.height
        containView?.addSubview(toView)
        containView?.bringSubviewToFront(fromView)
        UIView.animateWithDuration(duration, animations: { () -> Void in
            fromView.center = CGPointMake(CGRectGetMidX(self.originFrame), CGRectGetMidY(self.originFrame))
            fromView.transform = CGAffineTransformMakeScale(xScale, yScale)
            }) { (finished) -> Void in
                transitionContext.completeTransition(true)
        }
    }
}

然后,在ViewController.swift中didSelectItemAtIndexPath设置presentAnimator.frame下一行添加

 dismissAnimator.originFrame = cell.convertRect(cell.imageview.frame, toView: nil)

其中,dismissAnimator是ViewController的一个属性

    let dismissAnimator = DismisssAnimator()

然后,在代理方法中,返回

 func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return dismissAnimator;
    }

最终的效果
技术分享


完整工程的下载

CSDN下载(一会补上链接)

自定义presentViewController的转场动画(Swift)

标签:

原文地址:http://blog.csdn.net/hello_hwc/article/details/51188423

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!