标签:
//MARK: - 定义协议用来拿到图片起始位置;最终位置和图片
protocol PersentDelegate : class {
//起始位置
func homeRect(indexPath : NSIndexPath) ->CGRect
//展示图片的位置
func photoBrowserRect(indexPath : NSIndexPath) ->CGRect
//获取imageView(用这张临时的图片来实现动画效果)
func imageView(indexPath : NSIndexPath) ->UIImageView
}
//设置代理(代理一般都是用weak修饰)
weak var presentDelegate : PersentDelegate?
//获取转场的上下文:可以通过上下文获取到执行动画的view
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
isPresented ? animateForPresentView(transitionContext) : animateForDismissed(transitionContext)
}
//弹出
func animateForPresentView(transitionContext: UIViewControllerContextTransitioning) {
//1 取出弹出的view
let presentView = transitionContext.viewForKey(UITransitionContextToViewKey)!
//2 将弹出的view加入到contentView中
transitionContext.containerView()?.addSubview(presentView)
//3 执行动画
//3.1 获取需要执行的imageView
guard let persentDelegate = presentDelegate else {
return
}
guard let indexPath = indexPath else {
return
}
//调用方法,得到一张图片
let imageView = persentDelegate.imageView(indexPath)
//将图片添加到父控件中
transitionContext.containerView()?.addSubview(imageView)
//设置imageView的起始位置
imageView.frame = persentDelegate.homeRect(indexPath)
presentView.alpha = 0
//设置弹出动画的时候背景颜色为黑色
transitionContext.containerView()?.backgroundColor = UIColor.blackColor()
UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
//设置展示图片的位置
imageView.frame = persentDelegate.photoBrowserRect(indexPath)
}) { (_) -> Void in
//修改图片透明度
presentView.alpha = 1.0
//移除图片
imageView.removeFromSuperview()
transitionContext.containerView()?.backgroundColor = UIColor.clearColor()
//完成动画
transitionContext.completeTransition(true)
}
}
class PhotoBrowserAnimator: NSObject {
//设置角标
var indexPath : NSIndexPath?
}
//Mark: - 点击图片,弹出控制器
extension HomeViewController {
//定义为私有的
private func showPhotoBrowser( indexPath : NSIndexPath) {
//将图片的角标传入
photoBrowserAnimator.indexPath = indexPath
//设置代理
photoBrowserAnimator.presentDelegate = self
}
//MARK: - 实现协议中的方法
extension HomeViewController : PersentDelegate {
func homeRect(indexPath: NSIndexPath) -> CGRect {
//取出cell
let cell = (collectionView?.cellForItemAtIndexPath(indexPath))!
//将cell的frame值转成cell在window中的坐标
let homeFrame = collectionView!.convertRect(cell.frame, toCoordinateSpace: UIApplication.sharedApplication().keyWindow!)
//返回具体的尺寸
return homeFrame
}
}
func imageView(indexPath: NSIndexPath) -> UIImageView {
//创建imageView对象
let imageView = UIImageView()
//取出cell
let cell = (collectionView?.cellForItemAtIndexPath(indexPath))! as! HomeViewCell
//取出cell中显示的图片
let image = cell.imageView.image
//设置图片
imageView.image = image
//设置imageView相关属性(拉伸模式)
imageView.contentMode = .ScaleAspectFill
//将多余的部分裁剪
imageView.clipsToBounds = true
//返回图片
return imageView
}
func photoBrowserRect(indexPath: NSIndexPath) -> CGRect {
//取出cell
let cell = (collectionView?.cellForItemAtIndexPath(indexPath))! as! HomeViewCell
//取出cell中显示的图片
let image = cell.imageView.image
//计算方法后的图片的frame
return calculate(image!)
}
import UIKit
//MARK: - 设置图片的frame
func calculate (image : UIImage) ->CGRect {
let imageViewX : CGFloat = 0.0
let imageViewW : CGFloat = UIScreen.mainScreen().bounds.width
let imageViewH : CGFloat = imageViewW / image.size.width * image.size.height
let imageViewY : CGFloat = (UIScreen.mainScreen().bounds.height - imageViewH) * 0.5
return CGRect(x: imageViewX, y: imageViewY, width: imageViewW, height: imageViewH)
}
//MARK: - 定义协议来实现消失动画
protocol DismissDelegate : class {
//获取当前显示的图片的角标
func currentIndexPath() ->NSIndexPath
//获取imageView(用这张临时的图片来实现动画效果)
func imageView() ->UIImageView
}
class PhotoBrowserAnimator: NSObject {
//设置消失动画的代理
weak var dismissDelegate : DismissDelegate?
}
//消失
func animateForDismissed (transitionContext: UIViewControllerContextTransitioning) {
//1 取出消失的view
let dismissView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
dismissView.removeFromSuperview()
//2 执行动画
//2.1 校验(如果没有值,就直接返回)
guard let dismissDelegate = dismissDelegate else {
return
}
guard let presentDelegate = presentDelegate else {
return
}
// 2.2获取一张图片
let imageView = dismissDelegate.imageView()
// 2.3将图片添加到父控件中
transitionContext.containerView()?.addSubview(imageView)
// 2.4 获取当前正在显示的图片的角标
let indexPath = dismissDelegate.currentIndexPath()
// 2.5 设置起始位置
imageView.frame = presentDelegate.photoBrowserRect(indexPath)
//执行动画
UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
imageView.frame = presentDelegate.homeRect(indexPath)
}) { (_) -> Void in
//完成动画
transitionContext.completeTransition(true)
}
}
}
//Mark: - 点击图片,弹出控制器
extension HomeViewController {
//定义为私有的
private func showPhotoBrowser( indexPath : NSIndexPath) {
//创建控制器对象
let showPhotoBrowserVC = PhotoBrowserController()
//设置dismiss的代理
photoBrowserAnimator.dismissDelegate = showPhotoBrowserVC
}
//Mark: - 实现dismiss的代理方法
extension PhotoBrowserController : DismissDelegate {
func currentIndexPath() -> NSIndexPath {
//获取当前正在显示的cell
let cell = collectionView.visibleCells().first!
//根据cell获取indexPath
return collectionView.indexPathForCell(cell)!
}
}
func imageView() -> UIImageView {
//创建UIImageView对象
let imageView = UIImageView()
//获取正在显示的cell
let cell = collectionView.visibleCells().first! as! PhotoBrowerViewCell
//取出cell中的图片
imageView.image = cell.imageView.image
//设置图片的属性
imageView.contentMode = .ScaleAspectFill
//将多出的图片裁剪
imageView.clipsToBounds = true
//返回图片
return imageView
}
//设置弹出动画的时候背景颜色为黑色
transitionContext.containerView()?.backgroundColor = UIColor.blackColor()
transitionContext.containerView()?.backgroundColor = UIColor.clearColor()
标签:
原文地址:http://blog.csdn.net/xf931456371/article/details/51288824